我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.batch_matmul()。
def channel_wise_fc_layer(self, input, name): # bottom: (7x7x512) _, width, height, n_feat_map = input.get_shape().as_list() input_reshape = tf.reshape( input, [-1, width*height, n_feat_map] ) input_transpose = tf.transpose( input_reshape, [2,0,1] ) with tf.variable_scope(name): W = tf.get_variable( "W", shape=[n_feat_map,width*height, width*height], # (512,49,49) initializer=tf.random_normal_initializer(0., 0.005)) output = tf.batch_matmul(input_transpose, W) output_transpose = tf.transpose(output, [1,2,0]) output_reshape = tf.reshape( output_transpose, [-1, height, width, n_feat_map] ) return output_reshape
def __call__(self, u_t, a, b, scope=None): """ :param u_t: [N, M, d] :param a: [N, M. 1] :param b: [N, M. 1] :param mask: [N, M] :return: """ N, M, d = self.batch_size, self.mem_size, self.hidden_size L, sL = self.L, self.sL with tf.name_scope(scope or self.__class__.__name__): L = tf.tile(tf.expand_dims(L, 0), [N, 1, 1]) sL = tf.tile(tf.expand_dims(sL, 0), [N, 1, 1]) logb = tf.log(b + 1e-9) logb = tf.concat(1, [tf.zeros([N, 1, 1]), tf.slice(logb, [0, 1, 0], [-1, -1, -1])]) left = L * tf.exp(tf.batch_matmul(L, logb * sL)) # [N, M, M] right = a * u_t # [N, M, d] u = tf.batch_matmul(left, right) # [N, M, d] return u
def __call__(self, u_t, a, b, scope=None): """ :param u_t: [N, M, d] :param a: [N, M. d] :param b: [N, M. d] :param mask: [N, M] :return: """ N, M, d = self.batch_size, self.mem_size, self.hidden_size L, sL = self.L, self.sL with tf.name_scope(scope or self.__class__.__name__): L = tf.tile(tf.expand_dims(tf.expand_dims(L, 0), 0), [N, d, 1, 1]) sL = tf.tile(tf.expand_dims(tf.expand_dims(sL, 0), 0), [N, d, 1, 1]) logb = tf.log(b + 1e-9) # [N, M, d] logb = tf.concat(1, [tf.zeros([N, 1, d]), tf.slice(logb, [0, 1, 0], [-1, -1, -1])]) # [N, M, d] logb = tf.expand_dims(tf.transpose(logb, [0, 2, 1]), -1) # [N, d, M, 1] left = L * tf.exp(tf.batch_matmul(L, logb * sL)) # [N, d, M, M] right = a * u_t # [N, M, d] right = tf.expand_dims(tf.transpose(right, [0, 2, 1]), -1) # [N, d, M, 1] u = tf.batch_matmul(left, right) # [N, d, M, 1] u = tf.transpose(tf.squeeze(u, [3]), [0, 2, 1]) # [N, M, d] return u
def channel_wise_fc_layer(bottom, name, bias=True): """ channel-wise fully connected layer """ _, width, height, n_feat_map = bottom.get_shape().as_list() input_reshape = tf.reshape( bottom, [-1, width*height, n_feat_map] ) # order='C' input_transpose = tf.transpose( input_reshape, [2,0,1] ) # n_feat_map * batch * d with tf.variable_scope(name): W = tf.get_variable( "W", shape=[n_feat_map,width*height, width*height], # n_feat_map * d * d_filter initializer=tf.truncated_normal_initializer(0., 0.005)) output = tf.batch_matmul(input_transpose, W) # n_feat_map * batch * d_filter if bias == True: b = tf.get_variable( "b", shape=width*height, initializer=tf.constant_initializer(0.)) output = tf.nn.bias_add(output, b) output_transpose = tf.transpose(output, [1,2,0]) # batch * d_filter * n_feat_map output_reshape = tf.reshape( output_transpose, [-1, width, height, n_feat_map] ) return output_reshape
def transition(h): # compute A,B,o linearization matrices with tf.variable_scope("trans"): for l in range(2): h = ReLU(h, 100, "aggregate_loss" + str(l)) with tf.variable_scope("A"): v, r = tf.split(1, 2, linear(h, z_dim * 2)) v1 = tf.expand_dims(v, -1) # (batch, z_dim, 1) rT = tf.expand_dims(r, 1) # batch, 1, z_dim I = tf.diag([1.] * z_dim) A = ( I + tf.batch_matmul(v1, rT) ) # (z_dim, z_dim) + (batch, z_dim, 1)*(batch, 1, z_dim) (I is broadcasted) with tf.variable_scope("B"): B = linear(h, z_dim * u_dim) B = tf.reshape(B, [-1, z_dim, u_dim]) with tf.variable_scope("o"): o = linear(h, z_dim) return A, B, o, v, r
def transition(h,share=None): # compute A,B,o linearization matrices with tf.variable_scope("trans",reuse=share): for l in range(2): h=ReLU(h,100,"aggregate_loss"+str(l)) with tf.variable_scope("A"): v,r=tf.split(1,2,linear(h,z_dim*2)) v1=tf.expand_dims(v,-1) # (batch, z_dim, 1) rT=tf.expand_dims(r,1) # batch, 1, z_dim I=tf.diag([1.]*z_dim) A=(I+tf.batch_matmul(v1,rT)) # (z_dim, z_dim) + (batch, z_dim, 1)*(batch, 1, z_dim) (I is broadcasted) with tf.variable_scope("B"): B=linear(h,z_dim*u_dim) B=tf.reshape(B,[-1,z_dim,u_dim]) with tf.variable_scope("o"): o=linear(h,z_dim) return A,B,o,v,r
def spatial_transformer(U, theta, out_height, out_width): num_batch = tf.shape(U)[0] height, width, num_channels = U.get_shape()[1:] x_t, y_t = meshgrid(out_height, out_width) x_t = tf.expand_dims(x_t, 0) y_t = tf.expand_dims(y_t, 0) if theta.get_shape()[1] == 3: s, t_x, t_y = tf.split(1, 3, theta) x_s = tf.reshape(s*tf.tile(x_t, [num_batch,1]) + t_x, [-1]) y_s = tf.reshape(s*tf.tile(y_t, [num_batch,1]) + t_y, [-1]) else: grid = tf.expand_dims(tf.concat(0, [x_t, y_t, tf.ones_like(x_t)]), 0) grid = tf.tile(grid, [num_batch,1,1]) grid_t = tf.batch_matmul(tf.reshape(theta, [-1,2,3]), grid) x_s = tf.reshape(tf.slice(grid_t, [0,0,0], [-1,1,-1]), [-1]) y_s = tf.reshape(tf.slice(grid_t, [0,1,0], [-1,1,-1]), [-1]) return transform(U, x_s, y_s, num_batch, out_height, out_width, num_channels) # last layer of localization net
def _define_diag_covariance_probs(self, shard_id, shard): """Defines the diagonal covariance probabilities per example in a class. Args: shard_id: id of the current shard. shard: current data shard, 1 X num_examples X dimensions. Returns a matrix num_examples * num_classes. """ # num_classes X 1 # TODO(xavigonzalvo): look into alternatives to log for # reparametrization of variance parameters. det_expanded = tf.reduce_sum(tf.log(self._covs + 1e-3), 1, keep_dims=True) diff = shard - self._means x2 = tf.square(diff) cov_expanded = tf.expand_dims(1.0 / (self._covs + 1e-3), 2) # num_classes X num_examples x2_cov = tf.batch_matmul(x2, cov_expanded) x2_cov = tf.transpose(tf.squeeze(x2_cov, [2])) self._probs[shard_id] = -0.5 * ( tf.to_float(self._dimensions) * tf.log(2.0 * np.pi) + tf.transpose(det_expanded) + x2_cov)
def _define_partial_maximization_operation(self, shard_id, shard): """Computes the partial statistics of the means and covariances. Args: shard_id: current shard id. shard: current data shard, 1 X num_examples X dimensions. """ # Soft assignment of each data point to each of the two clusters. self._points_in_k[shard_id] = tf.reduce_sum(self._w[shard_id], 0, keep_dims=True) # Partial means. w_mul_x = tf.expand_dims( tf.matmul(self._w[shard_id], tf.squeeze(shard, [0]), transpose_a=True), 1) self._w_mul_x.append(w_mul_x) # Partial covariances. x = tf.concat(0, [shard for _ in range(self._num_classes)]) x_trans = tf.transpose(x, perm=[0, 2, 1]) x_mul_w = tf.concat(0, [ tf.expand_dims(x_trans[k, :, :] * self._w[shard_id][:, k], 0) for k in range(self._num_classes)]) self._w_mul_x2.append(tf.batch_matmul(x_mul_w, x))
def buildAttention(self): q_relu = self.tensors['q_relu'] a_relu = self.tensors['a_relu'] with tf.name_scope("attention"): W = identity([self.params['nb_filter'], self.params['nb_filter']], name='W') batch = tf.shape(q_relu)[0] q_matmul = tf.batch_matmul(q_relu, tf.tile(tf.expand_dims(W,[0]), tf.pack([batch, tf.constant(1), tf.constant(1)]))) qa_attention = tf.batch_matmul(q_matmul, a_relu, adj_x=False, adj_y=True, name="attention") # shape = (batch, q_length, 1) qa_attention = tf.tanh(qa_attention) q_max = tf.reduce_max(qa_attention, reduction_indices=[2], keep_dims=True, name='q_max') # shape = (batch, 1, a_length) a_max = tf.reduce_max(qa_attention, reduction_indices=[1], keep_dims=True, name='a_max') # shape = (batch, q_length, 1) q_softmax = tf.expand_dims(tf.nn.softmax(tf.squeeze(q_max, [2])), -1) # shape = (batch, a_length, 1) a_softmax = tf.expand_dims(tf.nn.softmax(tf.squeeze(a_max, [1])), -1) # https://www.tensorflow.org/versions/r0.9/api_docs/python/math_ops.html#batch_matmul # shape = (batch, NUM_FILTERS, 1) q_feature = tf.batch_matmul(q_relu, q_softmax, adj_x=True, adj_y=False) a_feature = tf.batch_matmul(a_relu, a_softmax, adj_x=True, adj_y=False) self.tensors['q_feature'] = q_feature self.tensors['a_feature'] = a_feature self.tensors.setdefault('weights', []).append(W)
def soft_attn(self, top_recur): """""" reuse = (self.moving_params is not None) or None input_size = top_recur.get_shape().as_list()[-1] with tf.variable_scope('MLP', reuse=reuse): head_mlp, dep_mlp = self.MLP(top_recur, self.info_mlp_size, func=self.info_func, keep_prob=self.info_keep_prob, n_splits=2) with tf.variable_scope('Arcs', reuse=reuse): arc_logits = self.bilinear_classifier(dep_mlp, head_mlp, keep_prob=self.info_keep_prob) arc_prob = self.softmax(arc_logits) head_lin = tf.batch_matmul(arc_prob, top_recur) top_recur = tf.concat(2, [top_recur, head_lin]) top_recur.set_shape([tf.Dimension(None), tf.Dimension(None), tf.Dimension(4*self.recur_size)]) return top_recur #=============================================================
def read(x,x_hat,h_dec_prev): """Function to implement eq 27""" Fx,Fy,gamma=attn_window("read",h_dec_prev,patch_read) # gamma in [batch_size,1] # Fx in [batch_size, patch_read, 28] def filter_img(img,Fx,Fy,gamma,N): Fxt=tf.transpose(Fx,perm=[0,2,1]) img=tf.reshape(img,[-1,B,A]) # in [batch_size, 28,28] glimpse=tf.batch_matmul(Fy,tf.batch_matmul(img,Fxt)) #in [batch_size, patch_read, patch_read] glimpse=tf.reshape(glimpse,[-1,N*N]) # in batch_size, patch_read*patch_read return glimpse*tf.reshape(gamma,[-1,1]) x=filter_img(x,Fx,Fy,gamma,patch_read) # batch x (patch_read*patch_read) x_hat=filter_img(x_hat,Fx,Fy,gamma,patch_read) # x in [batch_size, patch_read^2] # x_hat in [batch_size, patch_read^2] return tf.concat(1,[x,x_hat]) # concat along feature axis
def transition(h): # compute A,B,o linearization matrices with tf.variable_scope("trans"): for l in range(2): h=ReLU(h,100,"l"+str(l)) with tf.variable_scope("A"): v,r=tf.split(1,2,linear(h,z_dim*2)) v1=tf.expand_dims(v,-1) # (batch, z_dim, 1) rT=tf.expand_dims(r,1) # batch, 1, z_dim I=tf.diag([1.]*z_dim) A=(I+tf.batch_matmul(v1,rT)) # (z_dim, z_dim) + (batch, z_dim, 1)*(batch, 1, z_dim) (I is broadcasted) with tf.variable_scope("B"): B=linear(h,z_dim*u_dim) B=tf.reshape(B,[-1,z_dim,u_dim]) with tf.variable_scope("o"): o=linear(h,z_dim) return A,B,o,v,r
def transition(h,share=None): # compute A,B,o linearization matrices with tf.variable_scope("trans",reuse=share): for l in range(2): h=ReLU(h,100,"l"+str(l)) with tf.variable_scope("A"): v,r=tf.split(1,2,linear(h,z_dim*2)) v1=tf.expand_dims(v,-1) # (batch, z_dim, 1) rT=tf.expand_dims(r,1) # batch, 1, z_dim I=tf.diag([1.]*z_dim) A=(I+tf.batch_matmul(v1,rT)) # (z_dim, z_dim) + (batch, z_dim, 1)*(batch, 1, z_dim) (I is broadcasted) with tf.variable_scope("B"): B=linear(h,z_dim*u_dim) B=tf.reshape(B,[-1,z_dim,u_dim]) with tf.variable_scope("o"): o=linear(h,z_dim) return A,B,o,v,r
def address(M0, w0, head): # Content focusing # Compute cosine similarity key = tf.expand_dims(head["key"], 1) key_matches = tf.batch_matmul(key, tf.transpose(M0, [0, 2, 1])) key_matches = tf.squeeze(key_matches) key_mag = tf.expand_dims(NTMCell.magnitude(head["key"], 1), 1) M_col_mag = NTMCell.magnitude(M0, 2) cosine_sim = key_matches / (key_mag * M_col_mag) # Compute content weights wc = tf.nn.softmax(head["key_str"] * cosine_sim) # Location focusing wg = head["interp"] * wc + (1 - head["interp"]) * w0 ws = rotate.ntm_rotate(wg, head["shift"]) ws_pow = tf.pow(ws, head["sharp"]) w1 = ws_pow / tf.reduce_sum(ws_pow, 1, keep_dims=True) return w1
def grams(X): dim_ordering = K.image_dim_ordering() if dim_ordering == 'tf': X = K.permute_dimensions(X, (0, 3, 1, 2)) (samples, c, h, w) = get_shape(X) X_reshaped = K.reshape(X, (-1, c, h * w)) X_T = K.permute_dimensions(X_reshaped, (0, 2, 1)) if K._BACKEND == 'theano': X_gram = T.batched_dot(X_reshaped, X_T) else: X_gram = tf.batch_matmul(X_reshaped, X_T) X_gram /= c * h * w return X_gram
def read_attention(self, x, x_hat, h_dec_prev): Fx, Fy, gamma = self.attn_window("read", h_dec_prev) # we have the parameters for a patch of gaussian filters. apply them. def filter_img(img, Fx, Fy, gamma): Fxt = tf.transpose(Fx, perm=[0,2,1]) img = tf.reshape(img, [-1, self.img_size, self.img_size]) # Apply the gaussian patches: # keep in mind: horiz = imgsize = verts (they are all the image size) # keep in mind: attn = height/length of attention patches # allfilters = [attn, vert] * [imgsize,imgsize] * [horiz, attn] # we have batches, so the full batch_matmul equation looks like: # [1, 1, vert] * [batchsize,imgsize,imgsize] * [1, horiz, 1] glimpse = tf.batch_matmul(Fy, tf.batch_matmul(img, Fxt)) glimpse = tf.reshape(glimpse, [-1, self.attention_n**2]) # finally scale this glimpse w/ the gamma parameter return glimpse * tf.reshape(gamma, [-1, 1]) x = filter_img(x, Fx, Fy, gamma) x_hat = filter_img(x_hat, Fx, Fy, gamma) return tf.concat(1, [x, x_hat]) # encode an attention patch
def write_attention(self, hidden_layer): with tf.variable_scope("writeW", reuse=self.share_parameters): w = dense(hidden_layer, self.n_hidden, self.attention_n*self.attention_n*self.num_colors) w = tf.reshape(w, [self.batch_size, self.attention_n, self.attention_n, self.num_colors]) w_t = tf.transpose(w, perm=[3,0,1,2]) Fx, Fy, gamma = self.attn_window("write", hidden_layer) # color1, color2, color3, color1, color2, color3, etc. w_array = tf.reshape(w_t, [self.num_colors * self.batch_size, self.attention_n, self.attention_n]) Fx_array = tf.concat(0, [Fx, Fx, Fx]) Fy_array = tf.concat(0, [Fy, Fy, Fy]) Fyt = tf.transpose(Fy_array, perm=[0,2,1]) # [vert, attn_n] * [attn_n, attn_n] * [attn_n, horiz] wr = tf.batch_matmul(Fyt, tf.batch_matmul(w_array, Fx_array)) sep_colors = tf.reshape(wr, [self.batch_size, self.num_colors, self.img_size**2]) wr = tf.reshape(wr, [self.num_colors, self.batch_size, self.img_size, self.img_size]) wr = tf.transpose(wr, [1,2,3,0]) wr = tf.reshape(wr, [self.batch_size, self.img_size * self.img_size * self.num_colors]) return wr * tf.reshape(1.0/gamma, [-1, 1])
def test_MatMul(self): t = tf.matmul(*self.random((4, 3), (3, 5)), transpose_a=False, transpose_b=False) self.check(t) t = tf.matmul(*self.random((3, 4), (3, 5)), transpose_a=True, transpose_b=False) self.check(t) t = tf.matmul(*self.random((4, 3), (5, 3)), transpose_a=False, transpose_b=True) self.check(t) t = tf.matmul(*self.random((3, 4), (5, 3)), transpose_a=True, transpose_b=True) self.check(t) # def test_BatchMatMul(self): # t = tf.batch_matmul(*self.random((2, 4, 4, 3), (2, 4, 3, 5)), adj_x=False, adj_y=False) # self.check(t) # t = tf.batch_matmul(*self.random((2, 4, 3, 4), (2, 4, 3, 5)), adj_x=True, adj_y=False) # self.check(t) # t = tf.batch_matmul(*self.random((2, 4, 4, 3), (2, 4, 5, 3)), adj_x=False, adj_y=True) # self.check(t) # t = tf.batch_matmul(*self.random((2, 4, 3, 4), (2, 4, 5, 3)), adj_x=True, adj_y=True) # self.check(t)
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
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.
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)
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
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)
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
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])
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
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
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
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)
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)
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
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
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])
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])