gpt4 book ai didi

python - ValueError : Error when checking input: expected dense_26_input to have shape (45781, )但得到形状为(2,)的数组

转载 作者:行者123 更新时间:2023-11-30 09:43:44 25 4
gpt4 key购买 nike

我的输入只是一个包含 45781 行和两列的 csv 文件。我正在尝试在神经网络上训练我的数据,但是当我尝试拟合模型时,它会抛出错误。

ValueError: Error when checking input: expected dense_26_input to have shape (45781,) but got array with shape (2,)

我尝试实现此链接给出的解决方案:

Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (339732, 29)

但我仍然无法运行代码。这是我的代码:

X = df.iloc[:, 0:2].values
y = df.iloc[:, 2].values

df_sklearn = df.copy()

lb_make = LabelEncoder()
df_sklearn['Type'] = lb_make.fit_transform(df['Type'])
df_sklearn.head() #Results in appending a new column to df

df_onehot = df.copy()
df_onehot = pd.get_dummies(df_onehot, columns=['Type'], prefix = ['Type'])

df_onehot_sklearn = df.copy()

lb = LabelBinarizer()
lb_results = lb.fit_transform(df_onehot_sklearn['Type'])
lb_results_df = pd.DataFrame(lb_results, columns=lb.classes_)

result_df = pd.concat([df_onehot_sklearn, lb_results_df], axis=1)


X_train, X_test, y_train, y_test = train_test_split(X, lb_results_df, test_size = 0.4)

classifier = Sequential()
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 45781))
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu'))
classifier.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

classifier.fit(X_train, y_train, batch_size = 10, nb_epoch = 100)

最佳答案

这些行通常是您的训练示例,模型希望您省略此维度。因此,如果有 45781 个示例,每个示例有 2 列,则 input_dim 应为 2。此外,如果要堆叠层,则可以省略 output_dim 参数,但必须指定单位(神经元数量)。

最后一个密集层需要具有与 y 维度相同数量的神经元(在您的例子中为 20)。

下面是一个带有虚拟数据的示例:

X_train = np.random.random((300,2))
y_train = np.random.random((300,20))

classifier = Sequential()
classifier.add(Dense(units = 6, init = 'uniform', activation = 'relu', input_dim = 2))
classifier.add(Dense(units = 6, init = 'uniform', activation = 'relu'))
classifier.add(Dense(units = 20, init = 'uniform', activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
classifier.summary()

classifier.fit(X_train, y_train, batch_size=10, nb_epoch=100)

关于python - ValueError : Error when checking input: expected dense_26_input to have shape (45781, )但得到形状为(2,)的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55428085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com