【发布时间】:2020-07-17 08:02:49
【问题描述】:
我正在构建一个 Bi-LSTM 网络,其中包含一个注意力层。但是添加的层必须是类层的实例会给出错误。
我已经导入的一些库是
from keras.models import Model, Sequential
from keras.layers import LSTM, Activation, Dense, Dropout, Input, Embedding, Bidirectional, Conv1D, Flatten, GlobalMaxPooling1D, SpatialDropout1D
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import backend as K
from tensorflow.keras.layers import *
注意层类是
class attention(Layer):
def __init__(self, return_sequences=True):
self.return_sequences = return_sequences
super(attention,self).__init__()
def build(self, input_shape):
self.W=self.add_weight(name="att_weight", shape=(input_shape[-1],1),
initializer="normal")
self.b=self.add_weight(name="att_bias", shape=(input_shape[1],1),
initializer="zeros")
super(attention,self).build(input_shape)
def call(self, x):
e = K.tanh(K.dot(x,self.W)+self.b)
a = K.softmax(e, axis=1)
output = x*a
if self.return_sequences:
return output
return K.sum(output, axis=1)
模型是这样的
model = Sequential()
model.add(Embedding(max_words, 1152, input_length=max_len, weights=[embeddings]))
model.add(BatchNormalization())
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Bidirectional(LSTM(32, return_sequences=True)))
model.add(attention(return_sequences=True))
model.add(BatchNormalization())
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.summary()
但它给出了一个错误
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-41-ba5b52fe2c87> in <module>()
1 model = Sequential()
----> 2 model.add(Embedding(max_words, 1152, input_length=max_len, weights=[embeddings]))
3 model.add(BatchNormalization())
4 model.add(Activation('tanh'))
5 #model.add(SpatialDropout1D(0.5))
/usr/local/lib/python3.6/dist-packages/keras/engine/sequential.py in add(self, layer)
131 raise TypeError('The added layer must be '
132 'an instance of class Layer. '
--> 133 'Found: ' + str(layer))
134 self.built = False
135 if not self._layers:
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.embeddings.Embedding object at 0x7f0da41aec50>
【问题讨论】:
-
你从哪里导入你的
Layer?好像你在这个库中混淆了tf.keras和keras库。 -
我已经包含了 'from tensorflow.keras.layers import *' 所以这包含了所有层
-
我已经用我的模型中导入的库更新了问题。
-
U 以错误的方式导入库...请在导入时也按照此处的示例:colab.research.google.com/drive/…
-
@MarcoCerliani 我正在使用与您发布的笔记本相同的笔记本,但现在它给出了错误
module 'keras.layers.embeddings' has no attribute 'shape
标签: python-3.x tensorflow keras nlp