【发布时间】:2017-10-27 01:47:48
【问题描述】:
我有 1 个数据集 (MNIST btw),分为训练和测试,两者的形状完全相同。我在训练部分训练了一个卷积自动编码器,并在下面的 fit() 函数调用中使用另一个进行验证。
如果我删除validation_data=(x_test,x_test),代码可以完美运行(即模型训练火车数据并提供良好的结果)
但是我必须使用validation_data,问题是当我使用它们时,在第一个时期之后,当损失在训练数据上计算并且需要为测试数据计算时,我得到一个错误:
纪元 1/5 896/1000 [=========================>....] - ETA:0s - 损失: 0.6677------------------------------------------------ -------------------------- InvalidArgumentError Traceback(最近调用 最后)
InvalidArgumentError:张量必须是 4-D,最后一个暗淡为 1、3 或 4,而不是 [1,3,3,8,8,1] [[节点:conv2d_3/kernel_0_1 = ImageSummary[T=DT_FLOAT, bad_color=Tensor, max_images=3
我该如何解决?
(x_train, _), (x_test, _) = mnist.load_data()
print("+++++++++++++++shape of x_train " , x_train.shape)
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
# adapt this if using `channels_first` image data format
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
# adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))
#TODO remove after i have solved the problem with the dim mismatch when using the validation dataset
x_train = x_train[range(1000),:,:,:]
x_test = x_test[range(1000),:,:,:]
# execute this in terminal to start tensorboard and let it watch the given logfile
# tensorboard --logdir=/tmp/autoencoder
tensorboardPath = os.path.join(os.getcwd(),"tensorboard")
tensorBoard = TensorBoard(log_dir=tensorboardPath,write_graph=True,write_images=True,histogram_freq=1, embeddings_freq=1, embeddings_layer_names=None)
checkpointer = ModelCheckpoint(filepath=os.path.join(os.getcwd(),"tensorboard"), verbose=1, save_best_only=True)
autoencoder.fit(x_train, x_train,
epochs=5,
batch_size=128,
shuffle=True,
validation_data=(x_test,x_test),
callbacks=[tensorBoard, checkpointer])`
【问题讨论】:
标签: python tensorflow keras keras-layer