examples/language_model/end_to_end_memory_networks/model.py
def forward(self, data):
        """
        The shape of data is [batch_size, mem_size], and the content is the id of each word
        """
        q = np.ndarray([self.batch_size, self.edim], dtype=np.float32)
        q.fill(self.init_hid)
        q = paddle.to_tensor(q)

        time = np.ndarray([self.batch_size, self.mem_size], dtype=np.int64)
        for i in range(self.mem_size):
            time[:, i] = i
        time = paddle.to_tensor(time)

        for hop in range(self.nhop):
            A_in_c = self.A(data)  # [batch_size, mem_size, edim]
            A_in_t = self.T_A(time)  # [batch_size, mem_size, edim]
            A_in = paddle.add(A_in_c, A_in_t)  # [batch_size, mem_size, edim]

            q_in = q.reshape([-1, 1, self.edim])  # [batch, 1, edim]
            A_out3d = paddle.matmul(
                q_in, A_in, transpose_y=True)  # [batch, 1, mem_size]
            A_out2d = A_out3d.reshape([-1, self.mem_size])
            p = nn.functional.softmax(A_out2d)  # [batch, mem_size]

            C_in_c = self.C(data)
            C_in_t = self.T_C(time)
            C_in = paddle.add(C_in_c, C_in_t)  # [batch_size, mem_size, edim]

            p_3d = p.reshape([-1, 1, self.mem_size])  # [batch, 1, mem_size]
            C_out3d = paddle.matmul(p_3d, C_in)  # [batch, 1, edim]

            C_out2d = C_out3d.reshape([-1, self.edim])  # [batch, edim]

            # Linear mapping and addition
            q_mapped = self.H(q)
            q_out = paddle.add(C_out2d, q_mapped)

            if self.lindim == self.edim:
                q = q_out
            elif self.lindim == 0:
                q = nn.functional.relu(q_out)
            else:
                F = q_out[:, :self.lindim]
                G = q_out[:, self.lindim:]
                K = nn.functional.relu(G)
                q = paddle.concat([F, K], axis=-1)

        predict = self.W(q)
        return predict

        The shape of data is [batch_size, mem_size], and the content is the id of each word
        
The shape of data is [batch_size, mem_size], and the content is the id of each word
The shape of data is [ batch_size mem_size ] and the content is the id of each word
def forward(self, data):
        q = np.ndarray([self.batch_size, self.edim], dtype=np.float32)
        q.fill(self.init_hid)
        q = paddle.to_tensor(q)
        time = np.ndarray([self.batch_size, self.mem_size], dtype=np.int64)
        for i in range(self.mem_size):
            time[:, i] = i
        time = paddle.to_tensor(time)
        for hop in range(self.nhop):
            A_in_c = self.A(data)  
            A_in_t = self.T_A(time)  
            A_in = paddle.add(A_in_c, A_in_t)  
            q_in = q.reshape([-1, 1, self.edim])  
            A_out3d = paddle.matmul(
                q_in, A_in, transpose_y=True)  
            A_out2d = A_out3d.reshape([-1, self.mem_size])
            p = nn.functional.softmax(A_out2d)  
            C_in_c = self.C(data)
            C_in_t = self.T_C(time)
            C_in = paddle.add(C_in_c, C_in_t)  
            p_3d = p.reshape([-1, 1, self.mem_size])  
            C_out3d = paddle.matmul(p_3d, C_in)  
            C_out2d = C_out3d.reshape([-1, self.edim])  
            q_mapped = self.H(q)
            q_out = paddle.add(C_out2d, q_mapped)
            if self.lindim == self.edim:
                q = q_out
            elif self.lindim == 0:
                q = nn.functional.relu(q_out)
            else:
                F = q_out[:, :self.lindim]
                G = q_out[:, self.lindim:]
                K = nn.functional.relu(G)
                q = paddle.concat([F, K], axis=-1)
        predict = self.W(q)
        return predict
def forward ( self , data ) : q = np . ndarray ( [ self . batch_size , self . edim ] , dtype = np . float32 ) q . fill ( self . init_hid ) q = paddle . to_tensor ( q ) time = np . ndarray ( [ self . batch_size , self . mem_size ] , dtype = np . int64 ) for i in range ( self . mem_size ) : time [ : , i ] = i time = paddle . to_tensor ( time ) for hop in range ( self . nhop ) : A_in_c = self . A ( data ) A_in_t = self . T_A ( time ) A_in = paddle . add ( A_in_c , A_in_t ) q_in = q . reshape ( [ - 1 , 1 , self . edim ] ) A_out3d = paddle . matmul ( q_in , A_in , transpose_y = True ) A_out2d = A_out3d . reshape ( [ - 1 , self . mem_size ] ) p = nn . functional . softmax ( A_out2d ) C_in_c = self . C ( data ) C_in_t = self . T_C ( time ) C_in = paddle . add ( C_in_c , C_in_t ) p_3d = p . reshape ( [ - 1 , 1 , self . mem_size ] ) C_out3d = paddle . matmul ( p_3d , C_in ) C_out2d = C_out3d . reshape ( [ - 1 , self . edim ] ) q_mapped = self . H ( q ) q_out = paddle . add ( C_out2d , q_mapped ) if self . lindim == self . edim : q = q_out elif self . lindim == 0 : q = nn . functional . relu ( q_out ) else : F = q_out [ : , : self . lindim ] G = q_out [ : , self . lindim : ] K = nn . functional . relu ( G ) q = paddle . concat ( [ F , K ] , axis = - 1 ) predict = self . W ( q ) return predict
The shape of data is [batch_size, mem_size], and the content is the id of each word
The shape of data is [ batch_size mem_size ] and the content is the id of each word
"""
        The shape of data is [batch_size, mem_size], and the content is the id of each word
        """ # [batch_size, mem_size, edim] # [batch_size, mem_size, edim] # [batch_size, mem_size, edim] # [batch, 1, edim] # [batch, 1, mem_size] # [batch, mem_size] # [batch_size, mem_size, edim] # [batch, 1, mem_size] # [batch, 1, edim] # [batch, edim] # Linear mapping and addition
self
data