【问题标题】:value error : Error when checking target: expected dense_1 to have shape(None,1) but got array with shape (6000,3)值错误:检查目标时出错:预期dense_1具有形状(无,1)但得到的数组具有形状(6000,3)
【发布时间】:2018-09-15 23:49:59
【问题描述】:

我面临分类分类器模型输入形状的问题

   x         y
 [1,2,3]    [0]
 [2,3,5]    [1]
 [2,1,6]    [2]
 [1,2,3]    [0]
 [2,3,5]    [0]
 [2,1,6]    [2]

然后我将 y 标签更改为 categorical as

   y
  [1,0,0]
  [0,1,0]
  [0,0,1]
  [1,0,0]
  [1,0,0]
  [0,0,1]

我的 x_train 形状是 (6000,3) y_train 形状为 (6000,3) x_test 形状为 (2000,3) y_test 形状为 (2000,3)

我试过这个模型并得到值错误

model=sequential()
model.add(Dense(1, input_shape(3,), activation="softmax"))
model.compile(Adam(lr=0.5), 'categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train,y_train,epochs=50, verbose=1)

Value error: Error when checking target: expected dense_1 to have shape(None,1) but got array with shape (6000,3)

我不明白这个错误。帮我解决这个问题

【问题讨论】:

    标签: python keras


    【解决方案1】:

    您的网络需要一个与输出类数量相匹配的输出层。你可以这样做

    X_train = np.zeros((10,3))
    y_train = np.zeros((10,))
    
    X_test = np.zeros((10,3))
    y_test = np.zeros((10,))
    
    num_classes = 3
    y_train_binary = keras.utils.to_categorical(y_train, num_classes)
    y_test_binary = keras.utils.to_categorical(y_test, num_classes)
    
    input_shape = (3,)
    
    model = Sequential()                 
    model.add(Dense(16, activation='relu',input_shape=input_shape))         
    model.add(Dense(num_classes, activation='softmax'))
    
    model.compile(loss='categorical_crossentropy',
                           optimizer='rmsprop',
                           metrics=['mae'])
    
    model.summary()
    
    history=model.fit(X_train,
                      y_train_binary,
                      epochs=5,
                      batch_size=8,
                      validation_data=(X_test, y_test_binary))
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    相关资源
    最近更新 更多