Home > Back-end >  TypeError: __init__() missing 1 required positional argument: 'units' while coding an neur
TypeError: __init__() missing 1 required positional argument: 'units' while coding an neur

Time:04-01

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']))

so the error i am getting is init() 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']))"

CodePudding user response:

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.

CodePudding user response:

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.

  • Related