如何在顺序keras模型中定义输入形状

时间:2019-04-03 10:40:07

标签: python keras-layer

请帮助您在keras模型中定义适当的if (confirm('are you really want to do what you are going to do?')) { console.log('ok') } else { console.log('no') }输入形状。也许我必须先重塑数据。我的数据集的尺寸如下所示:

Dense

我想这样定义顺序keras模型

``


    Data shapes are X_train: (2858, 2037) y_train: (2858, 1) X_test: (715, 2037) y_test: (715, 1)
Number of features (input shape) is 2037

``

模型摘要:

``

    batch_size = 128
    num_classes = 2
    epochs = 20

    model = Sequential()
    model.add(Dense(512, activation='relu', input_shape=(X_input_shape,)))
    model.add(Dropout(0.2))
    model.add(Dense(512, activation='relu'))

    model.summary()

    model.compile(loss='binary_crossentropy',
                optimizer=RMSprop(),
                from_logits=True,
                metrics=['accuracy'])

``

当我尝试适应它时...

``

    Layer (type)                 Output Shape              Param #   
    =================================================================
    dense_20 (Dense)             (None, 512)               1043456   
    _________________________________________________________________
    dropout_12 (Dropout)         (None, 512)               0         
    _________________________________________________________________
    dense_21 (Dense)             (None, 512)               262656    
    =================================================================
    Total params: 1,306,112
    Trainable params: 1,306,112
    Non-trainable params: 0

`` 我遇到错误:

``

    history = model.fit(X_train, y_train,
                        batch_size=batch_size,
                        epochs=epochs,
                        verbose=1,
                        validation_data=(X_test, y_test))

``

1 个答案:

答案 0 :(得分:0)

修改

model.add(Dense(512, activation='relu'))

model.add(Dense(1, activation='relu'))

输出形状的大小为1,与y_train.shape [1]相同。