コード例 #1
0
    def calculate_outputs(self, x):
        h = lstm_layer(x, self.history_length, self.lstm_size)
        c = wavenet(x, self.dilations, self.filter_widths, self.skip_channels,
                    self.residual_channels)
        h = tf.concat([h, c, x], axis=2)

        self.h_final = time_distributed_dense_layer(h,
                                                    50,
                                                    activation=tf.nn.relu,
                                                    scope='dense-1')
        y_hat = time_distributed_dense_layer(self.h_final,
                                             1,
                                             activation=tf.nn.sigmoid,
                                             scope='dense-2')
        y_hat = tf.squeeze(y_hat, 2)

        final_temporal_idx = tf.stack([
            tf.range(tf.shape(self.history_length)[0]), self.history_length - 1
        ],
                                      axis=1)
        self.final_states = tf.gather_nd(self.h_final, final_temporal_idx)
        self.final_predictions = tf.gather_nd(y_hat, final_temporal_idx)

        self.prediction_tensors = {
            'user_ids': self.user_id,
            'product_ids': self.product_id,
            'final_states': self.final_states,
            'predictions': self.final_predictions
        }

        return y_hat
コード例 #2
0
    def calculate_outputs(self, x):
        # lstm
        h = lstm_layer(x, self.history_length, self.lstm_size, scope='lstm-1')

        # cnn
        #c = time_distributed_dense_layer(x, self.lstm_size, activation=tf.nn.relu, scope='dense-1')
        #for i in range(6):
        #    c_i = temporal_convolution_layer(
        #        inputs=c,
        #        output_units=self.lstm_size,
        #        convolution_width=2,
        #        activation=tf.nn.relu,
        #        causal=True,
        #        dilation_rate=[2**i],
        #        scope='cnn-exp-{}'.format(i)
        #    )
        #    c += c_i

        #h = tf.concat([h, c, x], axis=2)
        self.h_final = time_distributed_dense_layer(h, 50, activation=tf.nn.relu, scope='dense-2')
        y_hat = time_distributed_dense_layer(self.h_final, 1, activation=tf.nn.sigmoid, scope='dense-3')
        y_hat = tf.squeeze(y_hat, 2)

        final_temporal_idx = tf.stack([tf.range(tf.shape(self.history_length)[0]), self.history_length - 1], axis=1)
        self.final_states = tf.gather_nd(self.h_final, final_temporal_idx)
        self.final_predictions = tf.gather_nd(y_hat, final_temporal_idx)

        self.prediction_tensors = {
            'user_ids': self.user_id,
            'product_ids': self.product_id,
            'final_states': self.final_states,
            'predictions': self.final_predictions
        }

        return y_hat
コード例 #3
0
    def calculate_outputs(self, x):
        h = lstm_layer(x, self.history_length, self.lstm_size, scope='lstm1')
        h = tf.concat([h, x], axis=2)

        self.h_final = time_distributed_dense_layer(h,
                                                    50,
                                                    activation=tf.nn.relu,
                                                    scope='dense1')
        y_hat = tf.squeeze(
            time_distributed_dense_layer(self.h_final,
                                         1,
                                         activation=tf.nn.sigmoid,
                                         scope='dense2'), 2)

        final_temporal_idx = tf.stack([
            tf.range(tf.shape(self.history_length)[0]), self.history_length - 1
        ],
                                      axis=1)
        self.final_states = tf.gather_nd(self.h_final, final_temporal_idx)
        self.final_predictions = tf.gather_nd(y_hat, final_temporal_idx)

        self.prediction_tensors = {
            'user_ids': self.user_id,
            'aisle_ids': self.aisle_id,
            'final_states': self.final_states,
            'predictions': self.final_predictions
        }

        return y_hat
コード例 #4
0
ファイル: drqn.py プロジェクト: wweschen/RL-Trader
    def wavenet_logits_target(self):

        x = self.get_inputs(self.opens_, self.highs_, self.lows_, self.closes_,
                            self.volumes_, self.positions_, self.order_prices_,
                            self.current_prices_, self.time_since_,
                            self.todays_)

        inputs, w, b = temporal_convolution_layer(inputs=x,
                                                  output_units=8,
                                                  convolution_width=1,
                                                  scope='target-CNN-1x1')

        self.w_target["wcnn1"] = w
        self.w_target["bcnn1"] = b

        outputs = lstm_layer(inputs,
                             self.lengths,
                             self.lstm_size,
                             scope="series-lstm-target")

        h, w, b = time_distributed_dense_layer(outputs,
                                               128,
                                               scope='target-dense-encode-1',
                                               activation=tf.nn.relu,
                                               reuse=tf.AUTO_REUSE)
        self.w_target["wtf1"] = w
        self.w_target["btf1"] = b

        out, w, b = time_distributed_dense_layer(h,
                                                 32,
                                                 scope='target-dense-encode-2',
                                                 activation=tf.nn.relu,
                                                 reuse=tf.AUTO_REUSE)
        self.w_target["wtf2"] = w
        self.w_target["btf2"] = b

        shape = out.get_shape().as_list()
        out_flat = tf.reshape(out, [tf.shape(out)[0], 1, shape[1] * shape[2]])
        out, state = stateful_lstm(out_flat,
                                   self.num_lstm_layers,
                                   self.lstm_size,
                                   tuple([self.lstm_state_target]),
                                   scope_name="lstm_target")
        self.state_output_target_c = state[0][0]
        self.state_output_target_h = state[0][1]

        shape = out.get_shape().as_list()

        out = tf.reshape(out, [tf.shape(out)[0], shape[2]])

        out, w, b = fully_connected_layer(out,
                                          self.n_actions,
                                          scope_name='target-dense-encode-2',
                                          activation=None)

        self.w_target["wout"] = w
        self.w_target["bout"] = b

        self.q_target_out = out
        self.q_target_action = tf.argmax(self.q_target_out, axis=1)
コード例 #5
0
    def calculate_outputs(self, x):
        h = lstm_layer(x, self.history_length, self.lstm_size, scope='lstm-1')
        h = tf.concat([h, x], axis=2)
        h_final = time_distributed_dense_layer(h, 50, activation=tf.nn.relu, scope='dense-1')

        n_components = 1
        params = time_distributed_dense_layer(h_final, n_components*2, scope='dense-2', activation=None)
        ps, mixing_coefs = tf.split(params, 2, axis=2)

        # this is implemented incorrectly, but it still helped...
        mixing_coefs = tf.nn.softmax(mixing_coefs - tf.reduce_min(mixing_coefs, 2, keep_dims=True))
        ps = tf.nn.sigmoid(ps)

        labels = tf.tile(tf.expand_dims(self.next_is_ordered, 2), (1, 1, n_components))
        losses = tf.reduce_sum(mixing_coefs*log_loss(labels, ps), axis=2)
        sequence_mask = tf.cast(tf.sequence_mask(self.history_length, maxlen=100), tf.float32)
        avg_loss = tf.reduce_sum(losses*sequence_mask) / tf.cast(tf.reduce_sum(self.history_length), tf.float32)

        final_temporal_idx = tf.stack([tf.range(tf.shape(self.history_length)[0]), self.history_length - 1], axis=1)
        self.final_states = tf.gather_nd(h_final, final_temporal_idx)

        self.prediction_tensors = {
            'user_ids': self.user_id,
            'product_ids': self.product_id,
            'final_states': self.final_states
        }

        return avg_loss
コード例 #6
0
    def calculate_outputs(self, x):
        h = lstm_layer(self.x,
                       self.history_length,
                       self.lstm_size,
                       scope='lstm1')

        self.h_final = time_distributed_dense_layer(h,
                                                    50,
                                                    activation=tf.nn.relu,
                                                    scope='dense0')

        n_components = 3
        params = time_distributed_dense_layer(self.h_final,
                                              n_components * 3,
                                              scope='dense1')
        means, variances, mixing_coefs = tf.split(params, 3, axis=2)

        mixing_coefs = tf.nn.softmax(
            mixing_coefs - tf.reduce_min(mixing_coefs, 2, keep_dims=True))
        variances = tf.exp(variances) + 1e-5

        labels = tf.cast(
            tf.tile(tf.expand_dims(self.next_reorder_size, 2),
                    (1, 1, n_components)), tf.float32)
        n_likelihoods = 1.0 / (tf.sqrt(2 * np.pi * variances)) * tf.exp(
            -tf.square(labels - means) / (2 * variances))
        log_likelihood = -tf.log(
            tf.reduce_sum(mixing_coefs * n_likelihoods, axis=2) + 1e-10)

        self.means = means
        self.variances = variances
        self.mixing_coefs = mixing_coefs
        self.nll = log_likelihood

        samples = tf.cast(tf.reshape(tf.range(25), (1, 1, 1, 25)), tf.float32)

        means = tf.tile(tf.expand_dims(means, 3), (1, 1, 1, 25))
        variances = tf.tile(tf.expand_dims(variances, 3), (1, 1, 1, 25))
        mixing_coefs = tf.tile(tf.expand_dims(mixing_coefs, 3), (1, 1, 1, 25))
        sample_n_likelihoods = 1.0 / (tf.sqrt(2 * np.pi * variances)) * tf.exp(
            -tf.square(samples - means) / (2 * variances))

        self.sample_log_likelihoods = tf.reduce_sum(mixing_coefs *
                                                    sample_n_likelihoods,
                                                    axis=2)
        final_temporal_idx = tf.stack([
            tf.range(tf.shape(self.history_length)[0]), self.history_length - 1
        ],
                                      axis=1)
        self.final_states = tf.gather_nd(self.h_final, final_temporal_idx)

        self.prediction_tensors = {
            'user_ids': self.user_id,
            'final_states': self.final_states,
            'predictions': self.sample_log_likelihoods
        }

        return self.nll
コード例 #7
0
    def calculate_outputs(self, x):
        h = lstm_layer(x, self.history_length, self.lstm_size, scope='lstm-1')
        h_final = time_distributed_dense_layer(h,
                                               50,
                                               activation=tf.nn.relu,
                                               scope='dense-1')

        n_components = 3
        params = time_distributed_dense_layer(h_final,
                                              n_components * 3,
                                              scope='dense-2')
        means, variances, mixing_coefs = tf.split(params, 3, axis=2)

        mixing_coefs = tf.nn.softmax(
            mixing_coefs - tf.reduce_min(mixing_coefs, 2, keep_dims=True))
        variances = tf.exp(variances) + 1e-5

        labels = tf.cast(
            tf.tile(tf.expand_dims(self.next_reorder_size, 2),
                    (1, 1, n_components)), tf.float32)
        n_likelihoods = 1.0 / (tf.sqrt(2 * np.pi * variances)) * tf.exp(
            -tf.square(labels - means) / (2 * variances))
        nlls = -tf.log(
            tf.reduce_sum(mixing_coefs * n_likelihoods, axis=2) + 1e-10)

        sequence_mask = tf.cast(
            tf.sequence_mask(self.history_length, maxlen=100), tf.float32)
        nll = tf.reduce_sum(nlls * sequence_mask) / tf.cast(
            tf.reduce_sum(self.history_length), tf.float32)

        # evaluate likelihood at a sample of discrete points
        samples = tf.cast(tf.reshape(tf.range(25), (1, 1, 1, 25)), tf.float32)
        means = tf.tile(tf.expand_dims(means, 3), (1, 1, 1, 25))
        variances = tf.tile(tf.expand_dims(variances, 3), (1, 1, 1, 25))
        mixing_coefs = tf.tile(tf.expand_dims(mixing_coefs, 3), (1, 1, 1, 25))
        n_sample_likelihoods = 1.0 / (tf.sqrt(2 * np.pi * variances)) * tf.exp(
            -tf.square(samples - means) / (2 * variances))
        sample_nlls = -tf.log(
            tf.reduce_sum(mixing_coefs * n_sample_likelihoods, axis=2) + 1e-10)

        final_temporal_idx = tf.stack([
            tf.range(tf.shape(self.history_length)[0]), self.history_length - 1
        ],
                                      axis=1)
        final_states = tf.gather_nd(h_final, final_temporal_idx)
        final_sample_nlls = tf.gather_nd(sample_nlls, final_temporal_idx)
        self.final_states = tf.concat([final_states, final_sample_nlls],
                                      axis=1)

        self.prediction_tensors = {
            'user_ids': self.user_id,
            'final_states': self.final_states
        }

        return nll
コード例 #8
0
    def calculate_outputs(self, x):
        h = lstm_layer(x, self.history_length, self.lstm_size, scope='lstm-1')
        h_final = time_distributed_dense_layer(h, 50, activation=tf.nn.relu, scope='dense-1')
        y_hat = tf.squeeze(time_distributed_dense_layer(h_final, 1, scope='dense2'), 2)

        final_temporal_idx = tf.stack([tf.range(tf.shape(self.history_length)[0]), self.history_length - 1], axis=1)
        self.final_states = tf.gather_nd(h_final, final_temporal_idx)
        self.final_predictions = tf.gather_nd(y_hat, final_temporal_idx)

        self.prediction_tensors = {
            'user_ids': self.user_id,
            'final_states': self.final_states,
            'predictions': self.final_predictions
        }

        return y_hat
コード例 #9
0
    def calculate_outputs(self, x):
        h = lstm_layer(x, self.history_length, self.lstm_size, scope='lstm-1')
        h = tf.concat([h, x], axis=2)
        h_final = time_distributed_dense_layer(h,
                                               50,
                                               activation=tf.nn.relu,
                                               scope='dense-1')

        n_components = 1
        params = time_distributed_dense_layer(h_final,
                                              n_components * 2,
                                              scope='dense-2',
                                              activation=None)
        ps, mixing_coefs = tf.split(params, 2, axis=2)

        # this is implemented incorrectly, but it still helped...
        mixing_coefs = tf.nn.softmax(
            mixing_coefs - tf.reduce_min(mixing_coefs, 2, keep_dims=True))
        ps = tf.nn.sigmoid(ps)

        labels = tf.tile(tf.expand_dims(self.next_is_ordered, 2),
                         (1, 1, n_components))
        losses = tf.reduce_sum(mixing_coefs * log_loss(labels, ps), axis=2)
        sequence_mask = tf.cast(
            tf.sequence_mask(self.history_length, maxlen=100), tf.float32)
        avg_loss = tf.reduce_sum(losses * sequence_mask) / tf.cast(
            tf.reduce_sum(self.history_length), tf.float32)

        final_temporal_idx = tf.stack([
            tf.range(tf.shape(self.history_length)[0]), self.history_length - 1
        ],
                                      axis=1)
        self.final_states = tf.gather_nd(h_final, final_temporal_idx)

        self.prediction_tensors = {
            'user_ids': self.user_id,
            'product_ids': self.product_id,
            'final_states': self.final_states
        }

        return avg_loss
コード例 #10
0
    def calculate_outputs(self, x):
        h = lstm_layer(x, self.history_length, self.lstm_size)
        c = wavenet(x, self.dilations, self.filter_widths, self.skip_channels, self.residual_channels)
        h = tf.concat([h, c, x], axis=2)

        self.h_final = time_distributed_dense_layer(h, 50, activation=tf.nn.relu, scope='dense-1')
        y_hat = time_distributed_dense_layer(self.h_final, 1, activation=tf.nn.sigmoid, scope='dense-2')
        y_hat = tf.squeeze(y_hat, 2)

        final_temporal_idx = tf.stack([tf.range(tf.shape(self.history_length)[0]), self.history_length - 1], axis=1)
        self.final_states = tf.gather_nd(self.h_final, final_temporal_idx)
        self.final_predictions = tf.gather_nd(y_hat, final_temporal_idx)

        self.prediction_tensors = {
            'user_ids': self.user_id,
            'product_ids': self.product_id,
            'final_states': self.final_states,
            'predictions': self.final_predictions
        }

        return y_hat