0

I am trying to build a CNN for MNIST fashion, But not sure what is the issue here. I am getting thbis type error..

I have use Seuqential network,but MAxpooling and FLattena dn dense layers. But issue is in DEnse layer

model = Sequential() # type of DNN
model.add(Convolution2D(28, 3, 3, input_shape = (28, 28, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(output_dim = 100, activation="relu"))   ##### 128 nodes in this layer
model.add(Dense(10, activation=tf.nn.softmax))

TypeError                                 Traceback (most recent call last)
<ipython-input-45-90929a8a33e8> in <module>
  3 model.add(MaxPooling2D(pool_size=(2,2)))
  4 model.add(Flatten())
 ----> 5 model.add(Dense(output_dim = 100,kernel_initializer='he_uniform', activation="relu"))   

  6 model.add(Dense(10, activation=tf.nn.softmax))




 TypeError: __init__() missing 1 required positional argument: 'units'

What seems to be the issue here?

Antony Joy
  • 301
  • 3
  • 15

2 Answers2

3

From docs function definition of Dense is as such,

tf.keras.layers.Dense(
    units,
    activation=None,
    use_bias=True,
    kernel_initializer="glorot_uniform",
    bias_initializer="zeros",
    kernel_regularizer=None,
    bias_regularizer=None,
    activity_regularizer=None,
    kernel_constraint=None,
    bias_constraint=None,
    **kwargs
)

units is not an optional parameter. You seem to forget writing unit argument on first Dense call

tyfncn
  • 46
  • 3
  • would you be kind enough to look through a particular question...I am sitting for a day with model...https://stackoverflow.com/questions/66720811/how-to-build-a-cnn-model-for-mnist-fashion-and-test-it-with-a-another-set-of-ima – Antony Joy Mar 20 '21 at 12:13
1

Seems like, here's the solution, TypeError: __init__() missing 1 required positional argument: "units"', you passing wrong arguments:

model.add(Dense(output_dim = 100, activation="relu"))

instead of:

classifier.add(Dense(units = 128, activation='relu'))   
classifier.add(Dense(units = 1, activation='sigmoid'))

I've just checked the library, and there units like required positional argument:

    tf.keras.layers.Dense(
    units,
    activation=None,
    use_bias=True,
    kernel_initializer="glorot_uniform",
    bias_initializer="zeros",
    kernel_regularizer=None,
    bias_regularizer=None,
    activity_regularizer=None,
    kernel_constraint=None,
    bias_constraint=None,
    **kwargs
)
Vova
  • 3,117
  • 2
  • 15
  • 23
  • 2
    Yep. Looks like this to be the case. In the code shared in the line after `model.add(Flatten())` in the `Dense` function he hasn't mentioned the units. I think instead of unit's he has mistakenly used `output_dim` – Ritwik G Mar 19 '21 at 07:11
  • I have an another query... How many layers are there in total in the above model – Antony Joy Mar 19 '21 at 07:47
  • @ vova would you be kind enough to answer this particular question ? https://stackoverflow.com/questions/66720811/how-to-build-a-cnn-model-for-mnist-fashion-and-test-it-with-a-another-set-of-ima – Antony Joy Mar 20 '21 at 12:14