这是我的模型代码:
encoder = Embedding(input_dim=dataset.shape[0],output_dim=300, mask_zero=True, input_length=12,embeddings_initializer='uniform')encoder = LSTM(epochs, input_shape=(train_X.shape[1], train_X.shape[2]), return_sequences=True, unroll=True)encoder_last = encoder[:,-1,:]
我遇到了以下错误:
---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-88-3967dfedaa44> in <module> 1 encoder = Embedding(input_dim=dataset.shape[0],output_dim=300, mask_zero=True, input_length=12,embeddings_initializer='uniform') 2 encoder = LSTM(epochs, input_shape=(train_X.shape[1], train_X.shape[2]), return_sequences=True, unroll=True)----> 3 encoder_last = encoder[:,-1,:]TypeError: 'LSTM' object is not subscriptable
我该如何修复这个问题?
回答:
我想你是要将LSTM
层应用于Embedding
层的输出,然后取LSTM的最后一个输出。因此,首先你需要调用(即应用)你定义的层到某些张量(即某个层的输出)上,像这样:
inp = Input(shape=...)encoder = Embedding(...)(inp) # 在输入上调用嵌入层encoder = LSTM(...)(encoder) # 在嵌入层的输出上调用LSTM层
这样,层之间就连接起来了。然后你需要使用Lambda
层来切片LSTM层的输出:
encoder_last = Lambda(lambda x: x[:,-1,:])(encoder)