我有一个模型M,它有两个输入:x_train1和x_train2。经过大量的转换后,这些输入被拼接成一个单一的数组x1_x2。之后,它被输入到一个自编码器中,输出应该是x1_x2。但是当我尝试拟合模型时,我得到了以下错误:
ValueError: When feeding symbolic tensors to a model, we expect the tensors to have a static batch size. Got tensor with shape: (None, 2080)
我知道问题出在如何指定我的预期输出上。我能够使用一个虚拟数组如np.zeros((96, 2080))来运行代码,但无法通过设置内部层的输出来运行。
我使用以下方式来拟合模型:
autoencoder.fit([x_train1, x_train2], autoencoder.layers[-7].output, epochs=50, batch_size=8, shuffle=True, validation_split=0.2)
我如何让Keras理解预期输出应该是形状为(输入图像数量,2080)的内部层的输出?
回答:
我会这样做:从Keras导入Model
类,并创建一个额外的模型。
from tensorflow.python.keras.models import Model# model = your existing modelnew_model = Model( inputs = model.input, outputs = model.get_layer(name_of_desired_output_layer).output)
就这样,现在你可以使用你的新模型并进行训练了。