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

Using return_sequences in recurrentshop #97

Open
ghost opened this issue May 2, 2018 · 0 comments
Open

Using return_sequences in recurrentshop #97

ghost opened this issue May 2, 2018 · 0 comments

Comments

@ghost
Copy link

ghost commented May 2, 2018

I'm trying to write a custom recurrent layer using recurrentshop. The input is a sequence of length "timesteps" and the output should be the sequence of all timesteps outputs, hence I'm using return_sequences=True.

The recurrent model has two inputs, one of which is recurrent from the last time step. I'm using this layer similar to this reccurentshop example, but I keep getting errors saying "You must feed a value for placeholder tensor 'input_2' with dtype float". What am I doing wrong? The full code follows:


import keras.backend as K
from recurrentshop import RecurrentModel
import numpy as np
from keras.models import Model
from keras.layers import Dense, Reshape, Conv1D, Input, Lambda, concatenate
from keras.optimizers import Adam

# parameters:
timesteps = 35
output_dim = 315
input_dim = 10
batch_size = 100

# recurrent layer definition:
def myRNN(input_dim,output_dim):
    inp = Input((input_dim,))
    h_tm1 = Input((output_dim,))
    modified_h = Lambda(lambda x: x * K.sum(K.square(inp)))(h_tm1)
    modified_inp = Dense(output_dim, use_bias=False, activation='tanh')(inp)
    modified_inp = Reshape((output_dim,1))(modified_inp)
    modified_inp = Conv1D(128, 7, padding='same', activation='tanh', use_bias=False)(modified_inp)
    modified_inp = Lambda(lambda x: K.sum(x, axis=-1))(modified_inp)
    hid = concatenate([modified_h, modified_inp], axis=-1)
    h_t = Dense(output_dim, use_bias=False, activation='tanh')(hid)  
    return RecurrentModel(input=inp, output=h_t, initial_states=h_tm1, final_states=h_t,
                          return_sequences=True, state_initializer=['zeros'])

# building the model:
inp = Input((timesteps, input_dim))
temp = myRNN(input_dim,output_dim)(inp)
out = Reshape((timesteps*output_dim,1))(temp)
model = Model(inputs=inp, outputs=out)
model.compile(loss='mse', optimizer='adam')

# testing the model:
inp = np.random.rand(batch_size ,timesteps ,input_dim)
prediction = model.predict(inp)

Thank you very much!

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

0 participants