I'm building a deep neural network and I keep getting "TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given"

I'm building a deep neural network and I keep getting "TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given"

我正在尝试开发一个深度神经网络,我想在其中根据多个输入预测单个参数。但是,如标题中所述,我遇到了错误,我不确定为什么。我什至没有在我的代码中调用 __init__() 方法,所以我很困惑为什么它会给我这个错误。

这是我到目前为止编写的代码,并产生了以下错误。如果有任何帮助,我将不胜感激!

import pandas as pd
import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras import models
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

d = pd.read_csv(r"AirfoilSelfNoise.csv")
x = d.iloc[:, 0:5] #frequency [Hz], angle of attack [deg], chord length [m], free-stream velocity [m/s], suction side displacement thickness [m], input
y = d.iloc[:, 5] #scaled sound pressure level [dB], output
df = pd.DataFrame(d, columns=['f', 'alpha', 'c', 'U_infinity', 'delta', 'SSPL'])

xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.2, random_state=42)

mod = keras.Sequential(
    keras.layers.Dense(30, input_shape=(5,), activation='relu'),
    keras.layers.Dense(25, activation='relu'),
    keras.layers.Dense(1, activation='sigmoid')
)

mod.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
mod.fit(xtrain, ytrain, epochs=50)```

TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given

您忘记在 Sequential 函数中添加括号。使用您的代码,它将所有层作为不同的输入参数。但是,第一个参数需要是所需层的列表。你的情况:

mod = keras.Sequential([
    keras.layers.Dense(30, input_shape=(5,), activation='relu'),
    keras.layers.Dense(25, activation='relu'),
    keras.layers.Dense(1, activation='sigmoid')]
)