簡體   English   中英

如何在Sequential keras模型中定義inpute形狀

[英]How to define inpute shapes in Sequential keras model

請幫助在keras模型中定義適當的Dense輸入形狀。 也許我必須首先重塑我的數據。 我的數據集尺寸如下所示:

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

我想像那樣定義Sequential keras模型

``

    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))

``我得到一個錯誤:

``

  ValueError: Error when checking target: expected dense_21 to have shape (512,) but got array with shape (1,)                    

``

修改

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

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

輸出形狀的大小為1,與y_train.shape [1]相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM