Demo entry 6662773

FFFF

   

Submitted by anonymous on Nov 27, 2017 at 15:51
Language: Python. Code size: 1.4 kB.

class KolyanForcing():

    def __init__(self, c, data):
        self.c, self.data = c, data

    def build_train_model(self):
        c, data = self.c, self.data
        latent_dim = c['model.units'][0]

        inputs = Input(shape=(data.input_len, data.input_dim))
        lstm = LSTM(latent_dim, return_sequences=True, return_state=True)
        dense = Dense(data.output_dim)

        all_outputs, states = [], None
        true = np.arange(0, data.output_len, c['model.teacher.true_step'])  # each true_step sample is TRUE
        lstm_input = Lambda(lambda x: x[:, 0:1, :], name='input_start')(inputs)
        print 'Start input shape', lstm_input._keras_shape

        for i in xrange(data.output_len):
            outputs, state_h, state_c = lstm(lstm_input, initial_state=states)  # run the decoder on one timestep
            outputs = dense(outputs)
            all_outputs.append(outputs)  # store the current prediction

            # reinject outputs and states as inputs for the next loop iteration
            lstm_input = Lambda(lambda x: x[:, i:i+1, :], name='input_true_'+str(i))(inputs) \
                if i in true else outputs
            states = [state_h, state_c]

        model_outputs = Lambda(lambda x: K.concatenate(x, axis=1))(all_outputs)
        print 'Output shape', model_outputs._keras_shape
        self.model = Model(inputs, model_outputs)
        return self.model

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).