【问题标题】:Error when checking input: expected input_18 to have 3 dimensions, but got array with shape (7, 210)检查输入时出错:预期 input_18 有 3 个维度,但得到了形状为 (7, 210) 的数组
【发布时间】:2020-12-08 09:30:44
【问题描述】:

我的原始数据集的形状是 (210,8),我试图将 7 个独立变量作为 输入我的神经网络以查看它们属于哪个类/类别。 类/类别是目标变量。

我已经分离了自变量并将它们作为数组存储在'df_test'中

df = pd.read_csv('https://raw.githubusercontent.com/siiddd/WheatSeeds/master/Wheat.csv')

features = ['Area', 'Perimeter', 'Compactness', 'Length of Kernel','Width of Kernel', 'Asymmetric Coeff.', 'Length of Kernel Groove']

dftoArray = df[features].to_numpy()
df_test = dftoArray.reshape(7,210)

model = keras.Sequential()

model.add(keras.Input(shape = (7, )))

model.add(keras.layers.Dense(500, activation = 'relu'))
model.add(keras.layers.Dense(1, activation = 'sigmoid'))

model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])

model.fit(df_test, df['Class'], epochs = 10, validation_split = 0.10)  

这给了我错误:

检查输入时出错:预期 input_18 有 3 个维度,但是 得到形状为 (7, 210) 的数组

【问题讨论】:

    标签: python tensorflow keras neural-network


    【解决方案1】:

    我认为您在重塑 DataFrame 时犯了一个错误。正如您所说,数据由 210 个样本组成,每个样本具有 8 个特征,即数据的形状必须为 ( 210 , 8 )。现在,从df 中选择所需的列后,您需要将数据重塑为( 210 , 7 ) 而不是( 7 , 210 )。进行此更改,

    df_test = dftoArray.reshape( 210 , 1 )
    

    形状( 210 , 7 )( 7 , 210 ) 有很大的不同。形状( 7 , 210 ) 是指由 7 个样本组成的数据集,每个样本有 210 个特征。事实并非如此。

    【讨论】:

      猜你喜欢
      • 2020-06-09
      • 2020-02-27
      • 2020-07-01
      • 2020-06-08
      • 1970-01-01
      • 2019-11-11
      • 2018-12-26
      • 2019-09-03
      • 1970-01-01
      相关资源
      最近更新 更多