Esempio n. 1
0
    def step(self, cell_previous, hid_previous, train):

        ingate = T.dot(hid_previous, self.lstmpar.W_hid_to_ingate) + K.reshape(self.lstmpar.b_ingate,[1,self.num_lstm])
        forgetgate = T.dot(hid_previous, self.lstmpar.W_hid_to_forgetgate) + K.reshape(self.lstmpar.b_forgetgate,[1,self.num_lstm])
        cell_input = T.dot(hid_previous, self.lstmpar.W_hid_to_cell) + K.reshape(self.lstmpar.b_cell,[1,self.num_lstm])
        outgate = T.dot(hid_previous, self.lstmpar.W_hid_to_outgate) + K.reshape(self.lstmpar.b_outgate,[1,self.num_lstm])

        # Compute peephole connections
        ingate += cell_previous * K.reshape(self.lstmpar.W_cell_to_ingate,[1,self.num_lstm])
        forgetgate += cell_previous * K.reshape(self.lstmpar.W_cell_to_forgetgate,[1,self.num_lstm])

        # Apply nonlinearities
        ingate = K.sigmoid(ingate)
        forgetgate = K.sigmoid(forgetgate)
        cell_input = K.tanh(cell_input)

        # Compute new cell value
        cell = forgetgate * cell_previous + ingate * cell_input
        cell_bn = self.bn.set_output(cell,train=train)

        outgate += cell_bn * K.reshape(self.lstmpar.W_cell_to_outgate,[1,self.num_lstm])
        outgate = K.sigmoid(outgate)

        # Compute new hidden unit activation
        if self.use_th:
            hid = outgate * K.tanh(cell_bn)
        else:
            hid = outgate * cell_bn
        return [cell_bn, hid]
Esempio n. 2
0
    def step(self, input_n, cell_previous, hid_previous, train):
        input_to_in = T.dot(input_n, self.lstmpar.W_in_to_ingate) + K.reshape(
            self.lstmpar.b_ingate, [1, self.num_lstm])
        input_to_forget = T.dot(
            input_n, self.lstmpar.W_in_to_forgetgate) + K.reshape(
                self.lstmpar.b_forgetgate, [1, self.num_lstm])
        input_to_cell = T.dot(input_n, self.lstmpar.W_in_to_cell) + K.reshape(
            self.lstmpar.b_cell, [1, self.num_lstm])
        input_to_out = T.dot(input_n,
                             self.lstmpar.W_in_to_outgate) + K.reshape(
                                 self.lstmpar.b_outgate, [1, self.num_lstm])

        ingate = input_to_in + T.dot(hid_previous,
                                     self.lstmpar.W_hid_to_ingate)
        forgetgate = input_to_forget + T.dot(hid_previous,
                                             self.lstmpar.W_hid_to_forgetgate)
        cell_input = input_to_cell + T.dot(hid_previous,
                                           self.lstmpar.W_hid_to_cell)
        outgate = input_to_out + T.dot(hid_previous,
                                       self.lstmpar.W_hid_to_outgate)

        # Compute peephole connections
        ingate += cell_previous * K.reshape(self.lstmpar.W_cell_to_ingate,
                                            [1, self.num_lstm])
        forgetgate += cell_previous * K.reshape(
            self.lstmpar.W_cell_to_forgetgate, [1, self.num_lstm])

        # Apply nonlinearities
        ingate = K.sigmoid(ingate)
        forgetgate = K.sigmoid(forgetgate)
        cell_input = K.tanh(cell_input)

        # Compute new cell value
        cell = forgetgate * cell_previous + ingate * cell_input
        cell_bn = self.bn.set_output(cell, train=train)

        outgate += cell_bn * K.reshape(self.lstmpar.W_cell_to_outgate,
                                       [1, self.num_lstm])
        outgate = K.sigmoid(outgate)

        # Compute new hidden unit activation
        hid = outgate * cell_bn
        return [cell_bn, hid]
Esempio n. 3
0
    def CompileAndUpdate(self, Params):

        weight = self.options['weight']

        fea = T.tensor4(name='input_features', dtype=theano.config.floatX)
        att = T.tensor4(name='input_att', dtype='float32')

        LSTM = lstm_simple(fea, att, self.options, Params)
        LSTMproj = LSTM.set_output()
        LSTMC = ComputeCode(fea, self.options, Params, LSTMproj)
        frame, featurepart = LSTMC.set_output()

        self.params = LSTM.get_Params()

        steps = self.options['steps']
        # for i in range(self.options['batch_size']):
        AA = K.sigmoid(frame)

        Code = AA * featurepart

        #Code = Code / T.sqrt(T.sum(T.sqr(featurepart)))

        # 	for par_name, par_value in Params:
        #     	Params[par_name] += self.options['l2_decay'] * K.sum(K.mean(K.square(par_value.get_value()), axis=0))
        #     return Params
        # Params = Regularize(Params)

        # opt = optimizer.Adam(self.params, lr=self.options['lrate'])
        # updates = opt.get_updates(self.params, loss)

        # train_graph = theano.function([fea, att, pos_fea, pos_att, neg_fea, neg_att], loss, on_unused_input='warn', allow_input_downcast=True)
        # self.test_graph = theano.function([fea, att, pos_fea, pos_att, neg_fea, neg_att], loss, on_unused_input='warn')
        my_H_last = Code
        encoder = theano.function([fea, att],
                                  my_H_last,
                                  on_unused_input='ignore',
                                  allow_input_downcast=True)

        return fea, att, Code, encoder
Esempio n. 4
0
def sigmoid(x):
    """x can be tensor
    """
    return K.sigmoid(x)
Esempio n. 5
0
def sigmoid(x):
    return K.sigmoid(x)
Esempio n. 6
0
def sigmoid( x ):
    """x can be tensor
    """
    return K.sigmoid( x )
Esempio n. 7
0
    def CompileAndUpdate(self, Params):
        self.regularizerS = []
        # for par_name, par_value in Params:
        #     regularizer = regularizers.WeightRegularizer(l1=0., l2=self.options['l2_decay'])
        #     regularizer.set_param(par_value.get_value())
        #     self.regularizerS.append(regularizer)

        weight = self.options['weight']

        fea = T.tensor4(name='input_features', dtype=theano.config.floatX)
        att = T.tensor4(name='input_att', dtype='float32')
        pos_fea = T.tensor4(name='pos_fea', dtype='float32')
        pos_att = T.tensor4(name='pos_att', dtype='float32')
        neg_fea = T.tensor4(name='pos_fea', dtype='float32')
        neg_att = T.tensor4(name='neg_att', dtype='float32')
        TT = [fea, att, pos_fea, pos_att, neg_fea, neg_att]

        LSTM = lstm_simple(fea, att, self.options, Params)
        LSTMproj = LSTM.set_output()
        LSTMC = ComputeCode(fea, self.options, Params, LSTMproj)
        frame, featurepart = LSTMC.set_output()

        LSTM_pos = lstm_simple(pos_fea, pos_att, self.options, Params)
        LSTMproj_pos = LSTM_pos.set_output()
        LSTMC_pos = ComputeCode(pos_fea, self.options, Params, LSTMproj_pos)
        frame_pos, featurepart_pos = LSTMC_pos.set_output()

        LSTM_neg = lstm_simple(neg_fea, neg_att, self.options, Params)
        LSTMproj_neg = LSTM_neg.set_output()
        LSTMC_neg = ComputeCode(neg_fea, self.options, Params, LSTMproj_neg)
        frame_neg, featurepart_neg = LSTMC_neg.set_output()

        self.params = LSTM.get_Params()

        steps = self.options['steps']
        self.loss_1 = self.loss2 = self.loss_3 = 0.
        loss = 0.
        # for i in range(self.options['batch_size']):
        AA = K.sigmoid(frame)
        BB = K.sigmoid(frame_pos)
        CC = K.sigmoid(frame_neg)

        Code = AA * featurepart
        Code_pos = BB * featurepart_pos
        Code_neg = CC * featurepart_neg

        Code_ = (Code >= 0).astype('float32')
        Code_pos_ = (Code_pos >= 0).astype('float32')
        Code_neg_ = (Code_neg >= 0).astype('float32')
        # Code = Code / T.sqrt(T.sum(T.sqr(featurepart)))
        # Code_pos = Code_pos / T.sqrt(T.sum(T.sqr(Code_pos)))
        # Code_neg = Code_neg / T.sqrt(T.sum(T.sqr(Code_neg)))

        self.loss2 = T.max(
            (0, 2. - T.sqrt(T.sum(T.sqr(Code - Code_neg))) / 32. +
             T.sqrt(T.sum(T.sqr(Code - Code_pos))) / 32.))
        for i in range(32):
            self.loss_3 += T.max(
                (0, 2. - T.sqrt(T.sum(T.sqr(Code_[0][i] - Code_neg_[0][i]))) +
                 T.sqrt(T.sum(T.sqr(Code_[0][i] - Code_pos_[0][i])))))

        loss = self.loss2 + 0.1 * self.loss_3

        for par in Params.values():
            loss += K.sum(K.square(par)) * self.options['l2_decay'] / 2.
        # def Regularize(Params):
        #   for par_name, par_value in Params:
        #       Params[par_name] += self.options['l2_decay'] * K.sum(K.mean(K.square(par_value.get_value()), axis=0))
        #     return Params
        # Params = Regularize(Params)

        # opt = optimizer.Adam(self.params, lr=self.options['lrate'])
        # updates = opt.get_updates(self.params, loss)

        # train_graph = theano.function([fea, att, pos_fea, pos_att, neg_fea, neg_att], loss, on_unused_input='warn', allow_input_downcast=True)
        # self.test_graph = theano.function([fea, att, pos_fea, pos_att, neg_fea, neg_att], loss, on_unused_input='warn')
        my_H_last = Code
        encoder = theano.function(
            [fea, att, pos_fea, pos_att, neg_fea, neg_att],
            my_H_last,
            on_unused_input='ignore',
            allow_input_downcast=True)

        return loss, fea, att, pos_fea, pos_att, neg_fea, neg_att, Code, encoder