Двунаправленная модель LSTM еще не была построена ошибка - PullRequest
0 голосов
/ 27 апреля 2020

Я сейчас кодирую модель двунаправленного LSTM. Однако в процессе построения модели произошла ошибка. Как мне это решить? Ниже код моей модели.

def lstm_model():
            model = Sequential()
            model.add(Bidirectional(LSTM(lstm_sell, return_sequences=True,
                           input_shape=(time_steps, n_features), dropout=0.5, recurrent_dropout=0.5)))  # return_sequences=True , stateful=True
            #model.add(Dropout(0.5))
            model.add(Bidirectional(LSTM(lstm_sell, return_sequences=True, dropout=0.5, recurrent_dropout=0.3)))  # return_sequences=True , stateful=True
            #model.add(Dropout(0.3))
            model.add(Bidirectional(LSTM(lstm_sell, return_sequences=True)))  # 80

            model.add(Flatten())
            model.add(Dense(8))
            model.add(Dense(1, activation='sigmoid'))
           # model.add(Reshape((time_steps,)))

            #opt = RMSprop(lr=0.0001)#, decay=1e-6)
            model.compile(loss='mse',
                          optimizer='rmsprop',
                          metrics=['mse'])


            model.summary()

            return model

А затем содержание ошибки.

Traceback (most recent call last):
  File "C:/Users/USER/PycharmProjects/untitled/GA-LSTM.py", line 504, in <module>
    model = lstm_model()
  File "C:/Users/USER/PycharmProjects/untitled/GA-LSTM.py", line 498, in lstm_model
    model.summary()
  File "C:\Users\USER\Anaconda3\lib\site-packages\keras\engine\network.py", line 1252, in summary
    '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 input_shape or batch_input_shape in the first layer for automatic build. 

Process finished with exit code 1

1 Ответ

0 голосов
/ 27 апреля 2020

Модель должна знать, какую форму ввода она должна ожидать.

установить форму ввода для двунаправленного слоя

def lstm_model():
      model = Sequential()
      model.add(Bidirectional(LSTM(lstm_sell, return_sequences=True, dropout=0.5, recurrent_dropout=0.5),
                      input_shape=(time_steps, n_features)))  # return_sequences=True , stateful=True
      #model.add(Dropout(0.5))
      model.add(Bidirectional(LSTM(lstm_sell, return_sequences=True, dropout=0.5, recurrent_dropout=0.3)))  # return_sequences=True , stateful=True
      #model.add(Dropout(0.3))
      model.add(Bidirectional(LSTM(lstm_sell, return_sequences=True)))  # 80

      model.add(Flatten())
      model.add(Dense(8))
      model.add(Dense(1, activation='sigmoid'))
      # model.add(Reshape((time_steps,)))

      #opt = RMSprop(lr=0.0001)#, decay=1e-6)
      model.compile(loss='mse',
                    optimizer='rmsprop',
                    metrics=['mse'])


      model.summary()

      return model

первый слой - двунаправленный слой

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
bidirectional_1 (Bidirection (None, 1, 20)             1680      
_________________________________________________________________
bidirectional_2 (Bidirection (None, 1, 20)             2480      
_________________________________________________________________
bidirectional_3 (Bidirection (None, 1, 20)             2480      
_________________________________________________________________
flatten_1 (Flatten)          (None, 20)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 8)                 168       
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 9         
=================================================================
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...