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
python类batch_matmul()的实例源码
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
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
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])
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])
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 _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])