簡體   English   中英

model.RNN 的摘要錯誤。 此 model 尚未構建

[英]model.summary error for RNN. This model has not yet been built

我正在使用以下代碼:

model = Sequential()
model.add(Conv2D(filters=96, kernel_size=(11,11), strides=(11, 3),input_shape=(input_length,input_features,1), activation='relu'))
model.add(Dense(5, activation="softmax"))
model.compile(loss='categorical_crossentropy', metrics=["accuracy"], optimizer=optimizer(lr))
model.summary()

del model
model = Sequential()

model.add(Bidirectional(LSTM(128, return_sequences=True, input_shape=(input_length,input_features))))
model.add(Dense(5, activation="softmax"))
model.compile(loss='categorical_crossentropy', metrics=["accuracy"], optimizer=optimizer(lr))
model.summary()
exit()

並獲得以下 output:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 55, 55, 96)        11712     
_________________________________________________________________
dense (Dense)                (None, 55, 55, 5)         485       
=================================================================
Total params: 12,197
Trainable params: 12,197
Non-trainable params: 0
_________________________________________________________________
Traceback (most recent call last):
  File "Convert2hdf5.py", line 28, in <module>
    model.summary()
  File "/home/erez/projects/Journal/VE/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 2376, in summary
    raise ValueError('This model has not yet been built. '
ValueError: This model has not yet been built. Build the model first by calling `build()` or calling `fit()` with some data, or specify an `input_shape` argument in the first layer(s) for automatic build.

為什么Conv2D層使用input_shape指定輸入就足夠了,而對於 LSTM 還不夠? 我在這里做錯了嗎?

你已經用一個Bidirectional層包裹了你的LSTM層。 因此,您應該將input_shape參數傳遞給Bidirectional而不是LSTM層。 根據以下內容進行更改:

model.add(Bidirectional(LSTM(128, return_sequences=True), input_shape=(nput_length, input_features)))

model 然后編譯沒有問題。

暫無
暫無

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

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