Benson Jin
Benson Jin

Reputation: 31

What does "ValueError: When feeding symbolic tensors to a model, we expect the tensors to have a static batch size" mean?

I am using keras to build a neural network for predicting diabetes. However I encountered a ValueError: When feeding symbolic tensors to a model, we expect the tensors to have a static batch size.

I tried changing the input shapes but I am still stuck.

num_classes = 2
from keras.layers import Input, Dense
from keras.models import Model

# This returns a tensor
inputs = Input(shape=(784,))

# a layer instance is callable on a tensor, and returns a tensor
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='sigmoid')(x)

# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
          loss='binary_crossentropy',
          metrics=['accuracy'])
model.fit(x,y)  # starts training

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

Upvotes: 1

Views: 3109

Answers (2)

W. Lin
W. Lin

Reputation: 1

Because x is not the training data when you feed the model (x,y), I fix your code as following:

model.fit(x_train,y_train)  # starts training

Upvotes: 0

Primusa
Primusa

Reputation: 13498

Because of these lines x is a Layer object

x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)

The model should be fitted on actual data but instead you pass in a Layer object:

model.fit(x,y)  # starts training

To simply put it your x, which is a Layer object, is a symbolic tensor and keras tries to treat it as a data tensor but fails.

To fix this just make sure that the x that you're passing in is indeed your x training data.

Upvotes: 3

Related Questions