codekansas / pydata-carolinas-2016

:bar_chart: My tutorial for PyData Carolinas 2016
30 stars 12 forks source link

AttributeError: 'Embedding' object has no attribute 'W' #2

Open PADMAG6 opened 5 years ago

PADMAG6 commented 5 years ago

from keras.layers import Input, Embedding, merge, Flatten, Reshape, Lambda import keras.backend as K from keras.models import Model

input_sentence = Input(shape=(sentence_maxlen,), dtype='int32') input_embedding = Embedding(n_words, n_embed_dims)(input_sentence) avepool = Lambda(lambda x: K.mean(x, axis=1, keepdims=True), output_shape=lambda x: (x[0], 1)) color_prediction = avepool(Reshape((sentence_maxlen * n_embed_dims,))(input_embedding))

predict_green = Model(input=[input_sentence], output=[color_prediction]) predict_green.compile(optimizer='sgd', loss='binary_crossentropy')

predict_green.fit([sentences_array], [is_green], nb_epoch=5000, verbose=0) embeddings = predict_green.layers[1].W.get_value()

for i in range(n_words): print('{}: {}'.format(idx2word[i], embeddings[i]))

while running this code i am getting the following error

AttributeError Traceback (most recent call last)

in () 47 48 predict_green.fit([sentences_array], [is_green], nb_epoch=5000, verbose=0) ---> 49 embeddings = predict_green.layers[1].W.get_value() 50 51 for i in range(n_words): AttributeError: 'Embedding' object has no attribute 'W' what is this error meaning here? how to overcome this error here?
PADMAG6 commented 5 years ago

here i used the keras :2.2.0 python:3.6 backend theano

PADMAG6 commented 5 years ago

If I am doing ``` embeddings = predict_green.layers[1].get_weights() for i in range(n_words): print('{}: {}'.format(idx2word[i], embeddings[i]))


i am getting  this error IndexError: list index out of range. 
here my range is 8, even idx2word, embeddings length also 8. why i am getting this error? how to slove this error?