Demo entry 6662788



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

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

        inputs = Input(shape=(data.input_len, data.input_dim))
        ground_truth = Input(shape=(data.output_len, data.output_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')(ground_truth)
        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))(ground_truth) \
                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, ground_truth], model_outputs)
        return self.model

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).