【问题标题】:Error when checking target: expected dense_2 to have shape (3,) but got array with shape (1,)检查目标时出错:预期 dense_2 的形状为 (3,) 但得到的数组的形状为 (1,)
【发布时间】:2020-05-27 07:40:02
【问题描述】:

我更改了数据类型,但无法解决错误。

我尝试过 One-Hot Encoding,但它也不起作用。

不知道怎么回事:(

seed = 0
np.random.seed(seed)
tf.set_random_seed(seed)

df = pd.read_csv('HW01_dataset_tae.txt', sep=',' ,header=None, names = ["Native", "Instructor", "Course", "Semester", "Class Size", "Evaluation"])

dataset = df.values # dataframe to int64
X = dataset[:,0:5] # attribute
Y_Eva = dataset[:,5] # class 

e = LabelEncoder()
e.fit(Y_Eva)
Y = e.transform(Y_Eva)

K = 10 
kFold = StratifiedKFold(n_splits=K, shuffle=True, random_state=seed) 

accuracy = []

for train_index, test_index in kFold.split(X,Y):
    model = Sequential() 
    model.add(Dense(16, input_dim=5, activation='relu')) 
    model.add(Dense(10, activation='relu')) 
    model.add(Dense(3, activation='softmax'))
    model.compile(loss='mean_squared_error', 
              optimizer='adam', 
              metrics=['accuracy'])
    model.fit(X[train_index], Y[train_index], epochs=100, batch_size=2)

错误;检查目标时出错:预期 dense_2 的形状为 (3,),但得到的数组的形状为 (1,)

在此处检测到; model.fit(X[train_index], Y[train_index], epochs=100, batch_size=2).

我喊什么?

【问题讨论】:

    标签: python keras


    【解决方案1】:

    我解决了这个问题。

    在这段代码中,

    model.fit(X[train_index], Y[train_index], epochs=100, batch_size=2)
    

    'Y[train_index]' 中的行数必须是三,因为类是三。

    由于每个 Y[train_index] 只有一行,因此出现错误。

    所以,我使用 One-Hot Encoding 并像这样更改代码。

    e = LabelEncoder()
    e.fit(Y_Eva)
    Y = e.transform(Y_Eva)
    Y_encoded = np_utils.to_categorical(Y) # changed code
    
    K = 10 
    kFold = StratifiedKFold(n_splits=K, shuffle=True, random_state=seed) 
    
    accuracy = []
    
    for train_index, test_index in kFold.split(X,Y):
        model = Sequential() 
        model.add(Dense(32, input_dim=5, activation='relu')) 
        model.add(Dense(16, activation='relu')) 
        model.add(Dense(3, activation='softmax'))
        model.compile(loss='categorical_crossentropy', 
                  optimizer='adam', 
                  metrics=['accuracy'])
        model.fit(X[train_index], Y_encoded[train_index], epochs=100, batch_size=2) # changed code
    

    终于,我能够运行代码了。

    【讨论】:

      【解决方案2】:

      TensorFlow 已经在密集层上做了一些文档,如果你不说 input_dim 而是说 input_shape,你可以指定首选的形状。
      https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense

      model = Sequential()
      model.add(Dense(16, input_shape=(5,)))  # Then your data has to be of shape (batch x 5)
      

      当您添加另一个密集层时,您实际上不必提供 input_sahpe

      model.add(Dense(10))
      

      【讨论】:

      • 很抱歉让您感到困惑。我更改了输入数据类型而不是 input_shape 或 input_dim。无论如何它没有工作
      • 但是你输入的 shape 只是 5 还是 [batch_size, 5]?
      • 只需 5。batch size 为 2,epoch 为 100
      • 那你需要把输入的shape改成[batch_size, 5]
      • 我以不同的方式解决了这个问题。无论如何,谢谢!
      猜你喜欢
      • 2020-09-03
      • 2020-06-16
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 2020-09-26
      相关资源
      最近更新 更多