'TypeError: __init__() missing 1 required positional argument: 'units' while coding an neural net

import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from keras import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split

df = pd.read_csv("data\heart.csv")

print(df.head())
print(df.info())
print(df.describe())

input = df[[ "Sex" ,str("Age") ,"RestingBP" ,"Cholesterol"]]
output = df[['HeartDisease']]


input_train ,input_test ,output_train ,output_test = 
train_test_split(input,output,test_size=0.2,random_state=9)

print("Number of rows in input_train:", input_train.shape[0])
print("Number of rows in input_test:", input_test.shape[0])
print("Number of rows in output_train", output_train.shape[0])
print("Number of rows in output_test", output_test.shape[0])
pridictor = Sequential()

pridictor.add(Dense(16, activation='relu', input_dim=4))

pridictor.add(Dense(16, activation='relu' ))
pridictor.add(Dense(16, activation='relu' ))
pridictor.add(Dense(2, activation='relu'))

pridictor.compile(Dense(optimizer='adam', loss='categorical_crossentropy',metrics= 
['accuracy']))

To implement this code, I am getting the error is in init() method missing 1 required positional argument: 'units'. This error is coming from the last line of my code which is

pridictor.compile(Dense(optimizer='adam', loss='categorical_crossentropy',metrics= 
    ['accuracy']))


Solution 1:[1]

The problem comes from the line pridictor.compile(Dense(optimizer='adam', loss='categorical_crossentropy', metrics= ['accuracy'])).

It seems you are trying to compile your model wrongly. To compile your model you can do: pridictor.compile(optimizer="Adam", loss="categorical_crossentropy", metrics=["accuracy"]).

Please notice the omission of Dense in the syntax, pridictor.compile() function requires the optimizer and loss arguments, but instead you are providing a Dense layer as an argument, which is wrong. You can see the documentation here if you want to know more.

Solution 2:[2]

Compile is not a layer. It optimise your network with a optimiser function which in your case is Adam. You could remove "dense" from pridictor.compile and that should run.

Solution 3:[3]

It is the method and parameters, I look at the answers and your question see to your intention that you may need to make categories of large numbers of the input records. I introduce you to the Matrixes where they can help you determine pre-accuracy and accuracy by SparseCategoricalCrossentropy. ( Model complies )

[ Sample ]:

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Summary
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy', tf.keras.metrics.CategoricalAccuracy()])

[ Output ]:

1/1 [==============================] - 0s 38ms/step - loss: 4.4276 - accuracy: 0.0500 - categorical_accuracy: 0.4200 - val_loss: 4.4266 - val_accuracy: 0.0500 - val_categorical_accuracy: 0.4200
Epoch 1498/1500
1/1 [==============================] - 0s 35ms/step - loss: 4.4266 - accuracy: 0.0500 - categorical_accuracy: 0.4200 - val_loss: 4.4257 - val_accuracy: 0.0500 - val_categorical_accuracy: 0.4200
Epoch 1499/1500
1/1 [==============================] - 0s 34ms/step - loss: 4.4257 - accuracy: 0.0500 - categorical_accuracy: 0.4200 - val_loss: 4.4251 - val_accuracy: 0.0500 - val_categorical_accuracy: 0.4200
Epoch 1500/1500
1/1 [==============================] - 0s 41ms/step - loss: 4.4251 - accuracy: 0.0500 - categorical_accuracy: 0.4200 - val_loss: 4.4245 - val_accuracy: 0.0500 - val_categorical_accuracy: 0.4200

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrei Ungureanu
Solution 2 im_vutu
Solution 3 Martijn Pieters