Recursive AutoEncoder

2019-03-11  本文已影响0人  _Megamind_
AutoEncoder

Input : 一个可变长的序列(长度至少为2)
Output :
1) 在第一个特征向量的位置, Output等同于Input的两个序列点(x_3 & x_4
2) 其他位置,对应每个位置上Input的一个序列点(x_2等)以及前一个位置上的特征向量(y_1等)

比较每个位置上Input和Output的交叉熵

def build_model(self, options):

        input = tf.placeholder(tf.float32, [None, options["max_seq_length"], options["inputSize"]], name="input")
        inputLayer = tf.einsum("jkl,lm->jkm", input, self.L, name="inputLayer")
        mask = tf.placeholder(tf.float32, [None, options["max_seq_length"] - 1], name="mask")

        p = tf.nn.relu(
            tf.nn.bias_add(tf.einsum("jl,lm->jm", tf.concat([inputLayer[:, 0], inputLayer[:, 1]], 1), self.W_1),
                           self.b_1), name="p")
        p = tf.div(p, tf.reshape(tf.norm(p, axis=1), (-1, 1)))

        self.p = [p]

        c_ = tf.nn.relu(tf.nn.bias_add(tf.einsum("jl,lm->jm", p, self.W_2), self.b_2), name="c_")
        c1 = c_[:, :options["embSize"]]
        c2 = c_[:, options["embSize"]:]

        cost = [tf.add(tf.reduce_mean(tf.square(tf.subtract(c1, inputLayer[:, 0])) / 2, axis=1),
                       tf.reduce_mean(tf.square(tf.subtract(c2, inputLayer[:, 1])) / 2, axis=1))]

        for _ in range(2, options["max_seq_length"]):
            c = tf.concat([inputLayer[:, _], p], 1)
            p_ = tf.nn.relu(tf.nn.bias_add(tf.einsum("jk,kl->jl", c, self.W_1), self.b_1), name="p_")
            c_ = tf.nn.relu(tf.nn.bias_add(tf.einsum("jk,kl->jl", p_, self.W_2), self.b_2), name="c_")
            c1 = c_[:, :options["embSize"]]
            c2 = c_[:, options["embSize"]:]
            cost_ = tf.add((1 / (1 + _) * tf.reduce_mean(tf.square(tf.subtract(c1, inputLayer[:, _])), axis=1)),
                           (_ / (1 + _) * tf.reduce_mean(tf.square(tf.subtract(c2, p)), axis=1)), name="cost_")
            p = p_
            cost = tf.concat([cost, [cost_]], 0)
            self.p = tf.concat([self.p, [p]], 0)

        cost = tf.transpose(cost, perm=[1, 0], name="cost")
        all_cost = tf.reduce_sum(cost * mask, axis=1) / tf.reduce_sum(mask, axis=1, name="all_cost")
        optimizer = tf.train.AdadeltaOptimizer().minimize(all_cost)

        return input, mask, all_cost, optimizer
上一篇 下一篇

猜你喜欢

热点阅读