Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keras remove Graph #1

Open
fighting41love opened this issue Feb 18, 2017 · 3 comments
Open

keras remove Graph #1

fighting41love opened this issue Feb 18, 2017 · 3 comments

Comments

@fighting41love
Copy link

fighting41love commented Feb 18, 2017

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!

@ghost
Copy link

ghost commented Sep 24, 2019

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,

@AndreiKrutikov
Copy link

AndreiKrutikov commented Nov 2, 2019

@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

@ghost
Copy link

ghost commented Nov 2, 2019

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants