我有1個數據集(MNIST順便說一句),分成火車和測試,都具有完全相同的形狀。我在列車部分上訓練卷積自動編碼器,並使用另一個進行驗證,如fit()函數調用中所示。驗證數據上的Shape Mistmatch給Keras Model的fit()函數?
代碼工作完美(列車數據即火車模型,並提供了良好的效果),如果我刪除validation_data=(x_test,x_test)
但我必須使用validation_data,問題是,當我使用它們,第一個時期後,當損失變根據列車數據計算需要計算的測試數據,我得到一個錯誤:
Epoch 1/5 896/1000 [=========================>....] - ETA: 0s - loss: 0.6677--------------------------------------------------------------------------- InvalidArgumentError Traceback (most recent call last)
InvalidArgumentError: Tensor must be 4-D with last dim 1, 3, or 4, not [1,3,3,8,8,1] [[Node: 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])`