我的输入只是一个带有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:检查输入时出错:预期density_26_input具有形状(45781),但数组形状为(2,),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55428085/

10-12 18:11