ValueError: When feeding symbolic tensors to a model, we expect the tensors to have a static batch size

霸气de小男生 提交于 2020-02-25 04:06:12

问题


I am new to Keras and I was trying to build a text-classification CNN model using Python 3.6 when I encountered this error :

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)

My code for the model is here :

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

I encounter this error in the model.fit line. Here seq_len1 = 50 and seq_len2 = 120. Please help me resolve this.


回答1:


@Vatsal, please have a look at the functional API guide for Keras: https://keras.io/getting-started/functional-api-guide/

In your model.fit command, your first arguments are the Input Layer variables, these should be your actual data that you are trying to fit. You have specified your input layers to the Model in the penultimate line.



来源:https://stackoverflow.com/questions/51254382/valueerror-when-feeding-symbolic-tensors-to-a-model-we-expect-the-tensors-to-h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!