python类batch_matmul()的实例源码

forward_model.py 文件源码 项目:Buffe 作者: bentzinir 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def decode(self, input):
        # returns a decoder
        hidden = tf.matmul(input, self.weights["decoder1_weights"]) + self.weights["decoder1_biases"]
        hidden_relu = tf.nn.relu(hidden)

        # output is encoding_size x 1 x small_encoding_size
        # multiheaded_hidden = tf.matmul(input, self.weights["multiheaded1_weights"]) + self.weights["multiheaded1_biases"]
        # multiheaded_hidden = tf.reshape(multiheaded_hidden, [-1, self.arch_params['output_dim'], 1, self.arch_params['small_encoding_dim']])
        # multiheaded_hidden = tf.nn.relu(multiheaded_hidden)
        #
        # h = tf.scan(lambda a,x: tf.batch_matmul(x, self.weights["multiheaded2_weights"]), multiheaded_hidden,
        #            initializer=tf.Variable(tf.constant(0.0, shape=[self.arch_params['output_dim'],1,1])))
        # multiheaded_output = h + self.weights["multiheaded2_biases"]
        # output1 = tf.reshape(multiheaded_output, [-1, self.arch_params['output_dim']])

        output1 = tf.matmul(hidden_relu, self.weights["decoder2_weights"]) + self.weights["decoder2_biases"]
        output = output1
        return output
Multi-LSTM-bigram-based.py 文件源码 项目:TensorFlowHub 作者: MJFND 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def lstm_cell1(i, o, state):
    """Create a LSTM cell. See e.g.: http://arxiv.org/pdf/1402.1128v1.pdf
    Note that in this formulation, we omit the various connections between the
    previous state and the gates."""    
    m_input2 = tf.pack([i for _ in range(m_rows)])
    m_saved_output2 = tf.pack([o for _ in range(m_rows)])

   # m_input2 = tf.nn.dropout(m_input2, keep_prob)
    m_all = tf.batch_matmul(m_input2, m_input_w2) + tf.batch_matmul(m_saved_output2, m_middle_w2) + m_biases
    m_all = tf.unpack(m_all)

    input_gate = tf.sigmoid(m_all[m_input_index])
    forget_gate = tf.sigmoid(m_all[m_forget_index])
    update = m_all[m_update_index]
    state = forget_gate * state + input_gate * tf.tanh(update)
    output_gate = tf.sigmoid(m_all[m_output_index])

    return output_gate * tf.tanh(state), state

  # Input data.
memory.py 文件源码 项目:Neural-Turing-Machine 作者: camigord 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def get_content_adressing(self, memory_matrix, keys, strengths):
        """
        retrives a content-based addressing weighting given the keys

        Parameters:
        ----------
        memory_matrix: Tensor (batch_size, memory_locations, word_size)
            the memory matrix to lookup in
        keys: Tensor (batch_size, word_size, number_of_keys)
            the keys to query the memory with
        strengths: Tensor (batch_size, number_of_keys)
            the list of strengths for each lookup key

        Returns: Tensor (batch_size, memory_locations, number_of_keys)
            The list of lookup weightings for each provided key
        """

        normalized_memory = tf.nn.l2_normalize(memory_matrix, 2)
        normalized_keys = tf.nn.l2_normalize(keys, 1)

        similiarity = tf.batch_matmul(normalized_memory, normalized_keys)
        strengths = tf.expand_dims(strengths, 1)

        return tf.nn.softmax(similiarity * strengths, 1)
memory.py 文件源码 项目:Neural-Turing-Machine 作者: camigord 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def update_read_vectors(self, memory_matrix, read_weightings):
        """
        reads, updates, and returns the read vectors of the recently updated memory

        Parameters:
        ----------
        memory_matrix: Tensor (batch_size, memory_locations, word_size)
            the recently updated memory matrix
        read_weightings: Tensor (batch_size, memory_locations, read_heads)
            the amount of info to read from each memory location by each read head

        Returns: Tensor (batch_size, word_size, read_heads)
        """

        updated_read_vectors = tf.batch_matmul(memory_matrix, read_weightings, adj_x=True)

        return updated_read_vectors
networks.py 文件源码 项目:comprehend 作者: Fenugreek 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def energy(self, v, h):
        """Energy of system given visible unit and hidden values."""

        vbias_term = tf.matmul(v, tf.expand_dims(self.params['bvis'], 1))
        hidden_term = tf.matmul(h, tf.expand_dims(self.params['bhid'], 1))
        Wx = tf.matmul(v, self.params['W'])
        hWx = tf.batch_matmul(tf.expand_dims(Wx, 1), tf.expand_dims(h, 2))

        return -tf.squeeze(hidden_term) - tf.squeeze(vbias_term) - tf.squeeze(hWx)
tsne_op.py 文件源码 项目:deligan 作者: val-iisc 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def read_attn(x,x_hat,h_dec_prev):
    Fx,Fy,gamma=attn_window("read",h_dec_prev,read_n)
    def filter_img(img,Fx,Fy,gamma,N):
        Fxt=tf.transpose(Fx,perm=[0,2,1])
        img=tf.reshape(img,[-1,B,A])
        glimpse=tf.batch_matmul(Fy,tf.batch_matmul(img,Fxt))
        glimpse=tf.reshape(glimpse,[-1,N*N])
        return glimpse*tf.reshape(gamma,[-1,1])
    x=filter_img(x,Fx,Fy,gamma,read_n) # batch x (read_n*read_n)
    x_hat=filter_img(x_hat,Fx,Fy,gamma,read_n)
    return tf.concat(1,[x,x_hat]) # concat along feature axis
tsne_op.py 文件源码 项目:deligan 作者: val-iisc 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def write_attn(h_dec):
    with tf.variable_scope("writeW",reuse=DO_SHARE):
        w=linear(h_dec,write_size) # batch x (write_n*write_n)
    N=write_n
    w=tf.reshape(w,[batch_size,N,N])
    Fx,Fy,gamma=attn_window("write",h_dec,write_n)
    Fyt=tf.transpose(Fy,perm=[0,2,1])
    wr=tf.batch_matmul(Fyt,tf.batch_matmul(w,Fx))
    wr=tf.reshape(wr,[batch_size,B*A])
    #gamma=tf.tile(gamma,[1,B*A])
    return wr*tf.reshape(1.0/gamma,[-1,1])
pose_model.py 文件源码 项目:Face-Pose-Net 作者: fengju514 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def _transform(self, theta, input_dim, out_size, channel_input):
    with tf.variable_scope('_transform'):
      print input_dim.get_shape(), theta.get_shape(), out_size[0], out_size[1]
      num_batch = self.hps.batch_size #tf.shape(input_dim)[0]
      height = tf.shape(input_dim)[1]
      width = tf.shape(input_dim)[2]
      num_channels = tf.shape(input_dim)[3]
      theta = tf.reshape(theta, (-1, 2, 3))
      theta = tf.cast(theta, 'float32')

      # grid of (x_t, y_t, 1), eq (1) in ref [1]
      height_f = tf.cast(height, 'float32')
      width_f = tf.cast(width, 'float32')
      out_height = out_size[0]
      out_width = out_size[1]
      grid = self._meshgrid(out_height, out_width)
      #print grid, grid.get_shape()
      grid = tf.expand_dims(grid, 0)
      grid = tf.reshape(grid, [-1])
      grid = tf.tile(grid, tf.pack([num_batch]))
      grid = tf.reshape(grid, tf.pack([num_batch, 3, -1]))
      #print grid, grid.get_shape()

      # Transform A x (x_t, y_t, 1)^T -> (x_s, y_s)
      T_g = tf.batch_matmul(theta, grid)
      x_s = tf.slice(T_g, [0, 0, 0], [-1, 1, -1])
      y_s = tf.slice(T_g, [0, 1, 0], [-1, 1, -1])
      x_s_flat = tf.reshape(x_s, [-1])
      y_s_flat = tf.reshape(y_s, [-1])
      #print x_s_flat.get_shape(), y_s_flat.get_shape()
      input_transformed = self._interpolate(input_dim, x_s_flat, y_s_flat, out_size, channel_input)
      #print input_transformed.get_shape()

      output = tf.reshape(input_transformed, tf.pack([num_batch, out_height, out_width, channel_input]))
      return output
      #return input_dim
similarities.py 文件源码 项目:How-to-Learn-from-Little-Data 作者: llSourcell 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def cosine_similarity(x, y, eps=1e-6):
    z = tf.batch_matmul(x, tf.transpose(y, perm=[0,2,1]))
    z /= tf.sqrt(tf.multiply(tf.expand_dims(tf.reduce_sum(tf.multiply(x,x), 2), 2),tf.expand_dims(tf.reduce_sum(tf.multiply(y,y), 2), 1)) + eps)

    return z
multiclass_svm.py 文件源码 项目:TensorFlow-Machine-Learning-Cookbook 作者: PacktPublishing 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def reshape_matmul(mat):
    v1 = tf.expand_dims(mat, 1)
    v2 = tf.reshape(v1, [3, batch_size, 1])
    return(tf.batch_matmul(v2, v1))

# Compute SVM Model
modellib.py 文件源码 项目:rec-attend-public 作者: renmengye 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def extract_patch(x, f_y, f_x, nchannels, normalize=False):
  """
  Args:
      x: [B, H, W, D]
      f_y: [B, H, FH]
      f_x: [B, W, FH]
      nchannels: D
  Returns:
      patch: [B, FH, FW]
  """
  patch = [None] * nchannels
  fsize_h = tf.shape(f_y)[2]
  fsize_w = tf.shape(f_x)[2]
  hh = tf.shape(x)[1]
  ww = tf.shape(x)[2]

  for dd in range(nchannels):
    # [B, H, W]
    x_ch = tf.reshape(
        tf.slice(x, [0, 0, 0, dd], [-1, -1, -1, 1]), tf.pack([-1, hh, ww]))
    patch[dd] = tf.reshape(
        tf.batch_matmul(
            tf.batch_matmul(
                f_y, x_ch, adj_x=True), f_x),
        tf.pack([-1, fsize_h, fsize_w, 1]))

  return tf.concat(3, patch)
utils.py 文件源码 项目:tensorstyle 作者: tonypeng 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def tf_batch_gram_matrix(batch):
    _, height, width, channels = tensor_shape(batch)
    batch = tf.reshape(batch, (-1, height * width, channels))
    batch_T = tf.batch_matrix_transpose(batch)
    return tf.batch_matmul(batch_T, batch) / (height * width * channels)
e2c_plane.py 文件源码 项目:e2c-pytorch 作者: ethanluoyc 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def sampleQ_psi(z, u, Q_phi):
    A, B, o, v, r = transition(z)
    with tf.variable_scope("sampleQ_psi"):
        mu_t = tf.expand_dims(Q_phi.mu, -1)  # batch,z_dim,1
        Amu = tf.squeeze(tf.batch_matmul(A, mu_t), [-1])
        u = tf.expand_dims(u, -1)  # batch,u_dim,1
        Bu = tf.squeeze(tf.batch_matmul(B, u), [-1])
        Q_psi = NormalDistribution(Amu + Bu + o, Q_phi.sigma, Q_phi.logsigma,
                                   v, r)
        # the actual z_next sample is generated by deterministically transforming z_t
        z = tf.expand_dims(z, -1)
        Az = tf.squeeze(tf.batch_matmul(A, z), [-1])
        z_next = Az + Bu + o
        return z_next, Q_psi  #,(A,B,o,v,r) # debugging
e2c_seq.py 文件源码 项目:e2c-pytorch 作者: ethanluoyc 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def sampleQ_psi(z,u,Q_phi,share=None):
  A,B,o,v,r=transition(z,share)
  with tf.variable_scope("sampleQ_psi"):
    mu_t=tf.expand_dims(Q_phi.mu,-1) # batch,z_dim,1
    Amu=tf.squeeze(tf.batch_matmul(A,mu_t), [-1])
    u=tf.expand_dims(u,-1) # batch,u_dim,1
    Bu=tf.squeeze(tf.batch_matmul(B,u),[-1])
    Q_psi=NormalDistribution(Amu+Bu+o,Q_phi.sigma,Q_phi.logsigma, v, r)
    # the actual z_next sample is generated by deterministically transforming z_t
    z=tf.expand_dims(z,-1)
    Az=tf.squeeze(tf.batch_matmul(A,z),[-1])
    z_next=Az+Bu+o
    return z_next,Q_psi#,(A,B,o,v,r) # debugging
attention_.py 文件源码 项目:tf_practice 作者: juho-lee 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def _read(self, x, F_xt, F_y, gamma):
        return tf.reshape(tf.batch_matmul(F_y, tf.batch_matmul(
            tf.reshape(x, [-1,self.height,self.width]), F_xt)),
            [-1,self.read_dim])*tf.reshape(gamma, [-1,1])
attention_.py 文件源码 项目:tf_practice 作者: juho-lee 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def _write(self, w, F_x, F_y, gamma):
        return tf.reshape(tf.batch_matmul(tf.transpose(F_y, [0,2,1]),
            tf.batch_matmul(tf.reshape(w, [-1,self.N,self.N]), F_x)),
            [-1,self.write_dim])*tf.reshape(1./gamma, [-1,1])
attention.py 文件源码 项目:tf_practice 作者: juho-lee 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def _read(self, x, F_xt, F_y, gamma):
        return tf.reshape(tf.batch_matmul(F_y, tf.batch_matmul(
            tf.reshape(x, [-1,self.height,self.width]), F_xt)),
            [-1,self.read_dim])*tf.reshape(gamma, [-1,1])
attention.py 文件源码 项目:tf_practice 作者: juho-lee 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def _write(self, w, F_x, F_y, gamma):
        return tf.reshape(tf.batch_matmul(tf.transpose(F_y, [0,2,1]),
            tf.batch_matmul(tf.reshape(w, [-1,self.N,self.N]), F_x)),
            [-1,self.write_dim])*tf.reshape(1./gamma, [-1,1])
attention_.py 文件源码 项目:tf_practice 作者: juho-lee 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def _read(self, x, F_xt, F_y, gamma):
        return tf.reshape(tf.batch_matmul(F_y, tf.batch_matmul(
            tf.reshape(x, [-1,self.height,self.width]), F_xt)),
            [-1,self.read_dim])*tf.reshape(gamma, [-1,1])
attention.py 文件源码 项目:tf_practice 作者: juho-lee 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def _read(self, x, F_xt, F_y, gamma):
        return tf.reshape(tf.batch_matmul(F_y, tf.batch_matmul(
            tf.reshape(x, [-1,self.height,self.width]), F_xt)),
            [-1,self.read_dim])*tf.reshape(gamma, [-1,1])


问题


面经


文章

微信
公众号

扫码关注公众号