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

30,885

Solution 1

Try changing this line:

model.add(Dense(output_dim=NUM_CLASSES, activation='softmax'))

to

model.add(Dense(NUM_CLASSES, activation='softmax'))

I'm not experience in keras but I could not find a parameter called output_dim on the documentation page for Dense. I think you meant to provide units but labelled it as output_dim

Solution 2

The Keras Dense layer documentation is as follows:

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)

Using the following :

classifier.add(Dense(6, activation='relu', kernel_initializer='glorot_uniform',input_dim=11))

Will work as here the units means the output_dim saying that we need 6 neurons in the hidden layer. The weights are initialized with the uniform function and the input layer has 11 independent variables of the dataset (input_dim) to feed the above-hidden layer.

Solution 3

I think it's a version issue. In updated version of keras for Dense there is no "output_dim" argument.

You can see this documentation link for Dense arguments.

https://keras.io/api/layers/core_layers/dense/

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
)

So the first argument is "units", Which is mandatory.

instead of this line:

model.add(Dense(output_dim=NUM_CLASSES, activation='softmax'))

use this:

model.add(Dense(units=NUM_CLASSES, activation='softmax'))

or

model.add(Dense(NUM_CLASSES, activation='softmax'))
Share:
30,885
wafa
Author by

wafa

Updated on June 24, 2020

Comments

  • wafa
    wafa almost 4 years

    I am working in python and tensor flow but I miss 'units' argument and I do not know how to solve it, It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.

    here the code

    def createModel():
        model = Sequential()
        # first set of CONV => RELU => MAX POOL layers
        model.add(Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=inputShape))
        model.add(Conv2D(32, (3, 3), activation='relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.25))
    
        model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
        model.add(Conv2D(64, (3, 3), activation='relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.25)) 
        model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
        model.add(Conv2D(64, (3, 3), activation='relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.25))
    
        model.add(Flatten())
        model.add(Dense(512, activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(output_dim=NUM_CLASSES, activation='softmax'))
        # returns our fully constructed deep learning + Keras image classifier 
        opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
        # use binary_crossentropy if there are two classes
        model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
        return model
    
    print("Reshaping trainX at..."+ str(datetime.now()))
    #print(trainX.sample()) 
    print(type(trainX)) # <class 'pandas.core.series.Series'>
    print(trainX.shape) # (750,)
    from numpy import zeros
    Xtrain = np.zeros([trainX.shape[0],HEIGHT, WIDTH, DEPTH])
    for i in range(trainX.shape[0]): # 0 to traindf Size -1
        Xtrain[i] = trainX[i]
    print(Xtrain.shape) # (750,128,128,3)
    print("Reshaped trainX at..."+ str(datetime.now()))
    
    print("Reshaping valX at..."+ str(datetime.now()))
    print(type(valX)) # <class 'pandas.core.series.Series'>
    print(valX.shape) # (250,)
    from numpy import zeros
    Xval = np.zeros([valX.shape[0],HEIGHT, WIDTH, DEPTH])
    for i in range(valX.shape[0]): # 0 to traindf Size -1
        Xval[i] = valX[i]
    print(Xval.shape) # (250,128,128,3)
    print("Reshaped valX at..."+ str(datetime.now()))
    
    # initialize the model
    print("compiling model...")
    sys.stdout.flush()
    model = createModel()
    
    # print the summary of model
    from keras.utils import print_summary
    print_summary(model, line_length=None, positions=None, print_fn=None)
    
    # add some visualization
    from IPython.display import SVG
    from keras.utils.vis_utils import model_to_dot
    SVG(model_to_dot(model).create(prog='dot', format='svg'))