我正在使用keras运行注意图层与GRU在python中进行情感分析,但是,python给了我这个错误信息:
TypeError :('不是Keras张量:',Elemwise {add,no_inplace} .0)。
我在网站上搜索但是这个问题仍然无法解决。这是我的代码:
os.environ['KERAS_BACKEND']='theano'
#Attention GRU network
class AttLayer(Layer):
def __init__(self, **kwargs):
self.init = initializers.get('normal')
# self.input_spec = [InputSpec(ndim=3)]
super(AttLayer, self).__init__(**kwargs)
def build(self, input_shape):
assert len(input_shape) == 3
# self.W = self.init((input_shape[-1],1))
self.W = self.init((input_shape[-1],))
# self.input_spec = [InputSpec(shape=input_shape)]
self.trainable_weights = [self.W]
super(AttLayer, self).build(input_shape) # be sure you call this somewhere!
def call(self, x, mask=None):
eij = K.tanh(K.dot(x, self.W))
ai = K.exp(eij)
weights = ai / K.sum(ai, axis=1).dimshuffle(0, 'x')
weighted_input = x * weights.dimshuffle(0, 1, 'x')
return weighted_input.sum(axis=1)
def compute_output_shape(self, input_shape):
return input_shape[0], input_shape[-1]
def get_idx_from_sent(sent, word_idx_map, max_l=1187, filter_h=3):
"""
Transforms sentence into a list of indices. Pad with zeroes.
"""
x = []
pad = filter_h - 1
for i in range(pad):
x.append(0)
words = sent.split()
for word in words:
if word in word_idx_map:
x.append(word_idx_map[word])
while len(x) < max_l + 2 * pad:
x.append(0)
return x
def make_idx_data_cv(revs, word_idx_map, max_l=1187, k=300, filter_h=3):
"""
Transforms sentences into a 2-d matrix.
"""
data = []
for rev in revs:
sent = get_idx_from_sent(rev["text"], word_idx_map, max_l, filter_h)
sent.append(rev["y"])
data.append(sent)
x = np.array(data, dtype="int")[:,:-1]
data_y= np.array(data, dtype="int")[:,-1]
return x, data_y
#load data
x = pk.load(open("mr_allSentiment.p", "rb"))
revs, W, W2, word_idx_map, vocab = x[0], x[1], x[2], x[3], x[4]
X,Y = make_idx_data_cv(revs, word_idx_map, max_l=1187, k=300,filter_h=3)
x_train, x_test, y_train, y_test =train_test_split(X,Y,test_size=0.1)
#keras layers
embedding_layer = Embedding(len(vocab) + 1,
300,
weights=[W],
input_length=1191,
trainable=True)
sequence_input = Input(shape=(1191,), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)
l_gru = Bidirectional(GRU(100, return_sequences=True))(embedded_sequences)
l_att = AttLayer()(l_gru)
preds = Dense(1, activation='sigmoid')(l_att)
model = Model(sequence_input, preds)
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['acc'])
#estimate model
print("model fitting - attention GRU network")
model.summary()
model.fit(x_train, y_train, validation_data=(x_test, y_test),
nb_epoch=10, batch_size=50)
然而,它卡住了,这是错误信息:
Traceback (most recent call last):
File "C:/Users/ruowe/PycharmProjects/resnet/test1.py", line 101, in <module>
nb_epoch=10, batch_size=50)
File "C:\Users\ruowe\Anaconda3\lib\site-packages\keras\engine\training.py", line 1575, in fit
self._make_train_function()
File "C:\Users\ruowe\Anaconda3\lib\site-packages\keras\engine\training.py", line 960, in _make_train_function
loss=self.total_loss)
File "C:\Users\ruowe\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
File "C:\Users\ruowe\Anaconda3\lib\site-packages\keras\optimizers.py", line 427, in get_updates
ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
File "C:\Users\ruowe\Anaconda3\lib\site-packages\keras\optimizers.py", line 427, in <listcomp>
ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
File "C:\Users\ruowe\Anaconda3\lib\site-packages\keras\backend\theano_backend.py", line 275, in int_shape
raise TypeError('Not a Keras tensor:', x)
TypeError: ('Not a Keras tensor:', Elemwise{add,no_inplace}.0)
答案 0 :(得分:0)
有同样的问题,在这里找到答案:
简短回答 - 这是Keras ver的变化。 2
将构建功能更改为:
def build(self, input_shape):
assert len(input_shape)==3
self.W = self.add_weight(name='kernel',
shape=(input_shape[-1],),
initializer='normal',
trainable=True)
super(AttLayer, self).build(input_shape)