tensorflow中shape,get_shape,reshape,set_shape的对比

本文介绍了TensorFlow中shape、get_shape、reshape和set_shape的区别。shape返回的是一个tensor,get_shape返回tuple。reshape创建新tensor,set_shape用于设置placeholder的shape。
摘要由CSDN通过智能技术生成

shape和get_shape

import tensorflow as tf

x1 = tf.placeholder(tf.int32,shape=[2,2])
print(tf.shape(x1))
print(x1.get_shape())

输出结果:

"C:\Program Files\Anaconda3\python.exe" D:/pycharmprogram/tensorflow_learn/shape_learn/shape_get_shape.py
Tensor("Shape:0", shape=(2,), dtype=int32)
(2, 2)

Process finished with exit code 0

shape返回值是一个tensor,而get_shape返回的是一个tuple.

reshape和set_shape

import tensorflow as tf

x1 = tf.placeholder(tf.int32)
x2=tf.reshape(x1,[2,2])
print(tf.shape(x1))

sess = tf.Session()
print(sess.run(tf.shape(x2), feed_dict={x1:[0,1,2,3]}))
print(sess.run(tf.shape(x2), feed_dict={x1: [[0, 1], [2, 3]]}))

reshape生成一个新的shape,x2跟x1是一个不同的tensor

import tensor
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
介绍一下这段代码的Depthwise卷积层def get_data4EEGNet(kernels, chans, samples): K.set_image_data_format('channels_last') data_path = '/Users/Administrator/Desktop/project 5-5-1/' raw_fname = data_path + 'concatenated.fif' event_fname = data_path + 'concatenated.fif' tmin, tmax = -0.5, 0.5 #event_id = dict(aud_l=769, aud_r=770, foot=771, tongue=772) raw = io.Raw(raw_fname, preload=True, verbose=False) raw.filter(2, None, method='iir') events, event_id = mne.events_from_annotations(raw, event_id={'769': 1, '770': 2,'770': 3, '771': 4}) #raw.info['bads'] = ['MEG 2443'] picks = mne.pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False) epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=False, picks=picks, baseline=None, preload=True, verbose=False) labels = epochs.events[:, -1] print(len(labels)) print(len(epochs)) #epochs.plot(block=True) X = epochs.get_data() * 250 y = labels X_train = X[0:144,] Y_train = y[0:144] X_validate = X[144:216, ] Y_validate = y[144:216] X_test = X[216:, ] Y_test = y[216:] Y_train = np_utils.to_categorical(Y_train - 1) Y_validate = np_utils.to_categorical(Y_validate - 1) Y_test = np_utils.to_categorical(Y_test - 1) X_train = X_train.reshape(X_train.shape[0], chans, samples, kernels) X_validate = X_validate.reshape(X_validate.shape[0], chans, samples, kernels) X_test = X_test.reshape(X_test.shape[0], chans, samples, kernels) return X_train, X_validate, X_test, Y_train, Y_validate, Y_test kernels, chans, samples = 1, 3, 251 X_train, X_validate, X_test, Y_train, Y_validate, Y_test = get_data4EEGNet(kernels, chans, samples) model = EEGNet(nb_classes=3, Chans=chans, Samples=samples, dropoutRate=0.5, kernLength=32, F1=8, D=2, F2=16, dropoutType='Dropout') model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) checkpointer = ModelCheckpoint(filepath='/Users/XXX/baseline.h5', verbose=1, save_best_only=True) class_weights = {0: 1, 1: 1, 2: 1, 3: 1} fittedModel = model.fit(X_train, Y_train, batch_size=2, epochs=100, verbose=2, validation_data=(X_validate, Y_validate), callbacks=[checkpointer], class_weight=class_weights) probs = model.predict(X_test) preds = probs.argmax(axis=-1) acc = np.mean(preds == Y_test.argmax(axis=-1)) print("Classification accuracy: %f " % (acc))
05-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值