deep learning学习笔记---MemN2N

简介: MemN2N简单介绍

MemN2N

1. Summary

MemN2N is a generalization of RNN
1) The sentence in MemN2N is equivalent to the word in RNN;

687474703a2f2f692e696d6775722e636f6d2f6e7638394a4c632e706e67

2. Kernel Code

  • Build Model
def build_model(self):
    self.build_memory()
    self.W = tf.Variable(tf.random_normal([self.edim, self.nwords], stddev=self.init_std))
    z = tf.matmul(self.hid[-1], self.W)
    self.loss = tf.nn.softmax_cross_entropy_with_logits(logits=z, labels=self.target)
    self.lr = tf.Variable(self.current_lr)
    self.opt = tf.train.GradientDescentOptimizer(self.lr)
    params = [self.A, self.B, self.C, self.T_A, self.T_B, self.W]
    grads_and_vars = self.opt.compute_gradients(self.loss,params)
    clipped_grads_and_vars = [(tf.clip_by_norm(gv[0], self.max_grad_norm), gv[1]) for gv in grads_and_vars]
    inc = self.global_step.assign_add(1)
    with tf.control_dependencies([inc]):
        self.optim = self.opt.apply_gradients(clipped_grads_and_vars)
    tf.global_variables_initializer().run()
    self.saver = tf.train.Saver()
  • Build Memory
def build_memory(self):
    self.global_step = tf.Variable(0, name="global_step")
    self.A = tf.Variable(tf.random_normal([self.nwords, self.edim], stddev=self.init_std))
    self.B = tf.Variable(tf.random_normal([self.nwords, self.edim], stddev=self.init_std))
    self.C = tf.Variable(tf.random_normal([self.edim, self.edim], stddev=self.init_std))
    
    # Temporal Encoding
    self.T_A = tf.Variable(tf.random_normal([self.mem_size, self.edim], stddev=self.init_std))
    self.T_B = tf.Variable(tf.random_normal([self.mem_size, self.edim], stddev=self.init_std))
    
    # m_i = sum A_ij * x_ij + T_A_i
    Ain_c = tf.nn.embedding_lookup(self.A, self.context)
    Ain_t = tf.nn.embedding_lookup(self.T_A, self.time)
    Ain = tf.add(Ain_c, Ain_t)
    
    # c_i = sum B_ij * u + T_B_i
    Bin_c = tf.nn.embedding_lookup(self.B, self.context)
    Bin_t = tf.nn.embedding_lookup(self.T_B, self.time)
    Bin = tf.add(Bin_c, Bin_t)
    
    for h in xrange(self.nhop):
        self.hid3dim = tf.reshape(self.hid[-1], [-1, 1, self.edim])
        Aout = tf.matmul(self.hid3dim, Ain, adjoint_b=True)
        Aout2dim = tf.reshape(Aout, [-1, self.mem_size])
        P = tf.nn.softmax(Aout2dim)
        
        probs3dim = tf.reshape(P, [-1, 1, self.mem_size])
        Bout = tf.matmul(probs3dim, Bin)
        Bout2dim = tf.reshape(Bout, [-1, self.edim])

        Cout = tf.matmul(self.hid[-1], self.C)
        Dout = tf.add(Cout, Bout2dim)

        self.share_list[0].append(Cout)

        if self.lindim == self.edim:
            self.hid.append(Dout)
        elif self.lindim == 0:
            self.hid.append(tf.nn.relu(Dout))
        else:
            F = tf.slice(Dout, [0, 0], [self.batch_size, self.lindim])
            G = tf.slice(Dout, [0, self.lindim], [self.batch_size, self.edim-self.lindim])
            K = tf.nn.relu(G)
            self.hid.append(tf.concat(axis=1, values=[F, K]))

3. Reference
Blog: Memory-network

Github: MemN2N-tensorflow

Paper: End-To-End Memory Networks

相关文章
|
机器学习/深度学习 算法 API
论文笔记之:Deep Reinforcement Learning with Double Q-learning
Deep Reinforcement Learning with Double Q-learning Google DeepMind   Abstract    主流的 Q-learning 算法过高的估计在特定条件下的动作值。
|
5月前
|
机器学习/深度学习 传感器 自然语言处理
Deep Learning
【6月更文挑战第7天】
39 2
|
机器学习/深度学习 算法 决策智能
【5分钟 Paper】Deep Reinforcement Learning with Double Q-learning
【5分钟 Paper】Deep Reinforcement Learning with Double Q-learning
110 0
|
机器学习/深度学习 存储 自然语言处理
Deep learning
三巨头从机器学习谈起,指出传统机器学习的不足,总览深度学习理论、模型,给出了深度学习的发展历史,以及DL中最重要的算法和理论。
147 0
|
决策智能
论文笔记之:Collaborative Deep Reinforcement Learning for Joint Object Search
Collaborative Deep Reinforcement Learning for Joint Object Search   CVPR 2017 Motivation:   传统的 bottom-up object region proposals 的方法,由于提取了较多的 proposal,导致后续计算必须依赖于抢的计算能力,如 GPU 等。
|
机器学习/深度学习 算法 人工智能
|
机器学习/深度学习 自然语言处理 数据挖掘