在构建深度学习模型时,我很难找到错误所在,但在设置输入层输入形状时通常会遇到问题。
这是我的模型:
model = Sequential([
Dense(32, activation='relu', input_shape=(1461, 75)),
Dense(32, activation='relu'),
Dense(ytrain.size),])
它返回以下错误:
ValueError: Error when checking input: expected dense_1_input to have 3
dimensions, but got array with shape (1461, 75)
该数组是来自 kaggle 房价竞赛的训练集,我的数据集有 75 列和 1461 行。我的数组是二维的,那么为什么需要 3 维呢?我尝试在第一个密集层之前添加一个冗余的第三维或展平数组,但错误只是变成:
ValueError: Input 0 is incompatible with layer flatten_1: expected
min_ndim=3, found ndim=2
你如何确定输入大小应该是多少,为什么它期望的维度看起来如此随意?
作为参考,我附上了我的其余代码:
xtrain = pd.read_csv("pricetrain.csv")
test = pd.read_csv("pricetest.csv")
xtrain.fillna(xtrain.mean(), inplace=True)
xtrain.drop(["Alley"], axis=1, inplace=True)
xtrain.drop(["PoolQC"], axis=1, inplace=True)
xtrain.drop(["Fence"], axis=1, inplace=True)
xtrain.drop(["MiscFeature"], axis=1, inplace=True)
xtrain.drop(["PoolArea"], axis=1, inplace=True)
columns = list(xtrain)
for i in columns:
if xtrain[i].dtypes == 'object':
xtrain[i] = pd.Categorical(pd.factorize(xtrain[i])[0])
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
for i in columns:
if xtrain[i].dtypes == 'object':
xtrain[i] = le.fit_transform(xtrain[i])
ytrain = xtrain["SalePrice"]
xtrain.drop(["SalePrice"], axis=1, inplace=True)
ytrain = ytrain.values
xtrain = xtrain.values
ytrain.astype("float32")
size = xtrain.size
print(ytrain)
model = Sequential(
[Flatten(),
Dense(32, activation='relu', input_shape=(109575,)),
Dense(32, activation='relu'),
Dense(ytrain.size),
])
model.compile(loss='mse', optimizer='adam')
model.fit(xtrain, ytrain, epochs=10, verbose=1)
任何建议都会非常有帮助!
谢谢你。