ValueError:将符号张量馈送到模型时,我们期望张量具有静态批大小

时间:2018-07-09 21:38:20

标签: python tensorflow keras deep-learning conv-neural-network

我是Keras的新手,在遇到此错误时,我试图使用text-classification CNN建立Python 3.6模型:

Traceback (most recent call last):
  File "model.py", line 94, in <module>
    model.fit([x1, x2], y_label, batch_size=batch_size, epochs=epochs, verbose=1, callbacks=[checkpoint], validation_split=0.2)  # starts training
  File "/../../anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 955, in fit
    batch_size=batch_size)
  File "/../../anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 754, in _standardize_user_data
    exception_prefix='input')
  File "/../../anaconda3/lib/python3.6/site-packages/keras/engine/training_utils.py", line 90, in standardize_input_data
    data = [standardize_single_array(x) for x in data]
  File "/../../anaconda3/lib/python3.6/site-packages/keras/engine/training_utils.py", line 90, in <listcomp>
    data = [standardize_single_array(x) for x in data]
  File "/../../anaconda3/lib/python3.6/site-packages/keras/engine/training_utils.py", line 23, in standardize_single_array
    'Got tensor with shape: %s' % str(shape))
ValueError: When feeding symbolic tensors to a model, we expect thetensors to have a static batch size. Got tensor with shape: (None, 50, 100)

我的模型代码在这里:

print("\nCreating Model...")
x1 = Input(shape=(seq_len1, 100), name='x1')
x2 = Input(shape=(seq_len2, 100), name='x2')
x1_r = Reshape((seq_len1, embedding_dim, 1))(x1)
x2_r = Reshape((seq_len2, embedding_dim, 1))(x2)

conv_0 = Conv2D(num_filters, kernel_size=(filter_sizes[0], 1), padding='valid', kernel_initializer='normal', activation='relu')
.
# Conv layers with different filter sizes
.    
maxpool = MaxPool2D(pool_size=(2, 1), strides=(1,1), padding='valid')

output1 = conv_0(x1_r)
output1 = maxpool(output1)
output1 = conv_1(output1)
output1 = maxpool(output1)
output1 = conv_2(output1)
output1 = maxpool(output1)
.
# Same for output2
.
concatenated_tensor = Concatenate(axis=1)([output1, output2])
flatten = Flatten()(concatenated_tensor)
.
# Dense layers
. 
# this creates a model that includes
model = Model(inputs=[x1, x2], outputs=[output])
.    
.
model.fit([x1, x2], y_label, batch_size=batch_size, epochs=epochs, verbose=1, callbacks=[checkpoint], validation_split=0.2)  # starts training

我在model.fit行中遇到此错误。这里seq_len1 = 50和seq_len2 =120。请帮助我解决此问题。

1 个答案:

答案 0 :(得分:4)

@Vatsal,请查看Keras的功能性API指南: https://keras.io/getting-started/functional-api-guide/

model.fit命令中,第一个参数是Input Layer变量,它们应该是您要拟合的实际数据。您已在倒数第二行中为模型指定了输入层。