2017-11-29 5 views
0

Ich benutze Keras, um Aufmerksamkeitsschicht mit GRU zu tun, Sentiment Analyse in Python zu tun, jedoch gibt Python mir diese Fehlermeldung:TypeError: ('Nicht ein Keras Tensor:', Elemwise {add, no_inplace} .0)

TypeError: ('Not a Keras tensor:', Elemwise{add,no_inplace}.0).

Ich suchte auf der Website, aber dieses Problem kann immer noch nicht gelöst werden. Hier ist mein Code:

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) 

Allerdings wird es stecken und hier wird die Fehlermeldung:

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

klingt wie die Fehler in dieser Zeile ist: 'ms = [K.zeros (K.int_shape (p), dtype = K.dtype (p)) für p in params] ', wobei' p' ein Keras-Tensor sein sollte, aber nicht. --- Aber diese Zeile ist nicht in Ihrer Frage –

Antwort

0

hatte das gleiche Problem, fand die Antwort hier:

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

Kurze Antwort - das ist eine Veränderung in Keras ver. 2

Ändern der Erstellungsfunktion:

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

Vielen Dank! Es klappt! Dieses Problem verwirrte mich fast die Hälfte des Semesters und ich gab diese Methode fast auf .... – Vera

Verwandte Themen