【问题标题】:Can't load LSTM encoder decoder model Keras IndexError: list assignment index out of rangeCan\'t load LSTM encoder decoder model Keras IndexError: list assignment index out of range
【发布时间】:2022-08-10 00:35:42
【问题描述】:

我正在使用 keras 和功能 API 创建一个编码器解码器模型,该模型包含 2 个 LSTM 层,每个层用于二进制分类。 编码器 x 的输入形状为 (samples, time step, in_features) = (126144, 1, 113) 标签 y 的形状为 (samples, time steps, out_features) =(126144, 1, 2) x 和 y 都是 numpy 数组。

import tensorflow as tf

import tensorflow.keras as keras
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dense
from numpy import array
from numpy import array_equal
from tensorflow.keras.layers import Lambda
from tensorflow.keras import backend as K
n_timesteps_in = 1
n_features = 113
out_features = 2
numberOfLSTMunits = 256
def create_hard_coded_decoder_input_model(batch_size):
# The first part is encoder
encoder_inputs = Input(shape=(n_timesteps_in, n_features), name=\'encoder_inputs\')
encoder_lstm = LSTM(numberOfLSTMunits, return_state=True,return_sequences=True,  
name=\'encoder_lstm\')
encoder_outputs, state_h1, state_c1 = encoder_lstm(encoder_inputs)
# Second LSTM Added
encoder_lstm2 = LSTM(numberOfLSTMunits, return_state=True,  name=\'encoder_lstm2\')
_, state_h2, state_c2 = encoder_lstm2(encoder_outputs) 

states = [state_h1, state_c1, state_h2, state_c2]
decoder_inputs = Input(shape=(1, out_features),  name=\'decoder_inputs\')
decoder_lstm = LSTM(numberOfLSTMunits, return_sequences=True, return_state=True, 
name=\'decoder_lstm\')

# Second LSTM
decoder_lstm2 = LSTM(numberOfLSTMunits, return_sequences=True, return_state=True, 
name=\'decoder_lstm2\')

decoder_dense = Dense(out_features, activation=\'softmax\',  name=\'decoder_dense\')
# New input decoder
all_outputs = []
decoder_input_data = np.zeros((batch_size, 1, out_features))
decoder_input_data[:, 0, 0] = -1 
inputs = decoder_input_data
states1 = [state_h1, state_c1]
states2 = [state_h2, state_c2]
for _ in range(n_timesteps_in):
  # Run the decoder on one time step
  outputs, dh1, dc1 = decoder_lstm(inputs,initial_state= states1)
  final, dh2, dc2 = decoder_lstm2(outputs, initial_state=states2)
  outputs = decoder_dense(final)
  # Store the current prediction (we will concatenate all predictions later)
  all_outputs.append(outputs)
  # Reinject the outputs as inputs for the next loop iteration
  # as well as update the states
  inputs = outputs
  states1 = [state_h1, state_c1]
  states2 = [state_h2, state_c2]
decoder_outputs = Lambda(lambda x: K.concatenate(x, axis=1))(all_outputs)
model = Model(encoder_inputs, decoder_outputs, name=\'model_encoder_decoder\')
model.compile(optimizer=\'rmsprop\', loss=\'categorical_crossentropy\', metrics=[\'accuracy\'])
return model

我使用 192 作为批量大小。 训练后,我使用以下代码保存模型:

model.save(\'lstm.h5\')

当我加载模型时:

savedModel=load_model(\'lstm.h5\')

我收到此错误:

  /usr/local/lib/python3.7/dist-packages/keras/layers/recurrent.py in get_input_spec(shape)
  547       batch_index, time_step_index = (1, 0) if self.time_major else (0, 1)
  548       if not self.stateful:
  --> 549         input_spec_shape[batch_index] = None
  550       input_spec_shape[time_step_index] = None
  551       return InputSpec(shape=tuple(input_spec_shape))

  IndexError: list assignment index out of range

我试图解决这个问题好几天了,但似乎没有任何效果。我真的很感激任何帮助。

    标签: python tensorflow keras lstm encoder-decoder


    【解决方案1】:

    你解决了这个问题吗? 我面临着完全相同的问题。任何帮助,将不胜感激!

    【讨论】:

    猜你喜欢
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 2018-02-08
    • 2014-10-20
    • 1970-01-01
    相关资源
    最近更新 更多