Search code examples
tensorflowkerastf.keraskeras-layer

name 'input_shape' not defined in the Dense() function


For creating a simple convolutional neural network I have imported the keras from tensorflow but it says input_shape is undefined.

# creating a simple cnn with 2 layers input/output 
# from tensorflow.python.keras.engine.sequential import Sequential
# from tensorflow.python.keras.layers import Dense
from tensorflow import keras
from keras.models import Sequential

model = keras.Sequential([
    keras.layers.Dense(10, input_shape(784,), activation='sigmoid')
])


model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics={'accuracy'}
)

model.fit(X_train_flattened, y_train, epochs=5)

I have also tried importing keras.layers separately but no use. The error:

NameError                                 Traceback (most recent call last)
<ipython-input-4-db449261e881> in <module>
      6 
      7 model = keras.Sequential([
----> 8     keras.layers.Dense(10, input_shape(784,), activation='sigmoid')
      9 ])
     10 

NameError: name 'input_shape' is not defined

Solution

  • Generally, if you want to specify a keyword parameter, you need to use an equal sign that you are currently missing, like so keras.layers.Dense(10, input_shape=(784,), activation='sigmoid')