2017-11-29 13 views
0

私はケラスを使用してpythonで感情分析を行うためにGRUで注意層を実行しますが、pythonは私にこのエラーメッセージを表示します:TypeError:( 'Not Kerasテンソル:'、Elemwise {add、no_inplace} .0)

TypeError: ('Not a Keras tensor:', 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

音: 'MS = [K.zeros(K.int_shape(P)、DTYPE = K.dtype(P)) for p in params] '、' p'はkerasテンソルでなければなりませんが、そうではありません。 ---しかし、この行はあなたの質問に表示されていません –

答えて

0

は同じ問題を持っていた、ここで答えを見つけた:

https://www.cpume.com/question/fsnnffzn-why-the-keras-code-get-error-messages-when-changing-from-keras-1-2-2-to-keras-2.html

短い回答 - これはKeras版の変更です。 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) 
+0

ありがとうございました!できます!このトラブルは学期のほぼ半分を混乱させ、私はこの方法をほとんど断念しました.... – Vera

関連する問題