ml_code_completion icon indicating copy to clipboard operation
ml_code_completion copied to clipboard

keras remove Graph

Open fighting41love opened this issue 8 years ago • 3 comments

Keras removes Graph in the new version. For new keras, they use the functional API instead.

Hence, the code has an error:
from keras.models import Sequential, Graph ImportError: cannot import name 'Graph'

Anyone knows how to modify the codes in 'rnnLSTM' ? from keras.models import Model ? and How to modify the codes from line 268 - line 303?

Thanks!

fighting41love avatar Feb 18 '17 21:02 fighting41love

I think late but this is my code:

    inputs = Input(shape=(winSize,))
    wvec = Embedding(vocab_size, wdim, input_length=winSize)(inputs)
    wvecf = Flatten()(wvec)
    attn = Dense(winSize, activation='sigmoid', W_regularizer=l2(reg), name="attn")(wvecf)
    attnr = RepeatVector(wdim)(attn)
    attnp = Permute(dims=(2,1))(attnr)
    # multiply word vector by attention and flatten output
    awvecf = Flatten()(Multiply()([wvec, attnp]))  
    # fully connected layers
    d1 = Dense(zdim, activation='relu', W_regularizer=l2(reg))(awvecf)
    d2 = Dense(zdim2, activation='relu', W_regularizer=l2(reg))(d1)
    # final layer
    d3 = Dense(vocab_size, activation=output_activation, W_regularizer=l2(reg))(d2)
    self.model = Model(inputs=[inputs], outputs=d3)
    self.model.compile(optimizer=loss_optimizer, loss='categorical_crossentropy')

    # also compile a function for getting the attention vector
    self.get_attn = Model(inputs=[inputs], outputs=attn)

Note: When you call attn attn = self.get_attn.predict(Xs.astype(np.int32))

Hope will help you,

ghost avatar Sep 24 '19 08:09 ghost

@truonghoang It didn't help me. Now it saying probs = self.model.predict({'word': Xs})['probs'] IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

AndreiKrutikov avatar Nov 02 '19 10:11 AndreiKrutikov

@truonghoang It didn't help me. Now it saying probs = self.model.predict({'word': Xs})['probs'] IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

I think we change this function line 442 file rnnLSTM.py:

def score(self, Xs):
        if isinstance(self.model, Graph):
            return self.model.predict({'word': Xs})['probs']
        else:
            return self.model.predict(Xs)

to :

def score(self, Xs):
        return self.model.predict(Xs)

and this function line 346:

def score(self, Xs):
        return self.model.predict({'word': Xs})['probs']

to:

def score(self, Xs):
        return self.model.predict(Xs)

because now we don't depend on the Graph.

sorry I missed those.

ghost avatar Nov 02 '19 11:11 ghost