本文整理汇总了Python中tensorflow.pack函数的典型用法代码示例。如果您正苦于以下问题:Python pack函数的具体用法?Python pack怎么用?Python pack使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pack函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: FixedUnPooling
def FixedUnPooling(x, shape, unpool_mat=None):
"""
Unpool the input with a fixed mat to perform kronecker product with.
:param input: NHWC tensor
:param shape: int or [h, w]
:param unpool_mat: a tf/np matrix with size=shape. If None, will use a mat
with 1 at top-left corner.
:returns: NHWC tensor
"""
shape = shape2d(shape)
input_shape = tf.shape(x)
if unpool_mat is None:
mat = np.zeros(shape, dtype='float32')
mat[0][0] = 1
unpool_mat = tf.Variable(mat, trainable=False, name='unpool_mat')
elif isinstance(unpool_mat, np.ndarray):
unpool_mat = tf.Variable(unpool_mat, trainable=False, name='unpool_mat')
assert unpool_mat.get_shape().as_list() == list(shape)
# perform a tensor-matrix kronecker product
fx = flatten(tf.transpose(x, [0, 3, 1, 2]))
fx = tf.expand_dims(fx, -1) # (bchw)x1
mat = tf.expand_dims(flatten(unpool_mat), 0) #1x(shxsw)
prod = tf.matmul(fx, mat) #(bchw) x(shxsw)
prod = tf.reshape(prod, tf.pack(
[-1, input_shape[3], input_shape[1], input_shape[2], shape[0], shape[1]]))
prod = tf.transpose(prod, [0, 2, 4, 3, 5, 1])
prod = tf.reshape(prod, tf.pack(
[-1, input_shape[1] * shape[0], input_shape[2] * shape[1], input_shape[3]]))
return prod
示例2: iou
def iou(self, boxes1, boxes2):
"""calculate ious
Args:
boxes1: 4-D tensor [CELL_SIZE, CELL_SIZE, BOXES_PER_CELL, 4] ====> (x_center, y_center, w, h)
boxes2: 1-D tensor [4] ===> (x_center, y_center, w, h)
Return:
iou: 3-D tensor [CELL_SIZE, CELL_SIZE, BOXES_PER_CELL]
"""
boxes1 = tf.pack([boxes1[:, :, :, 0] - boxes1[:, :, :, 2] / 2, boxes1[:, :, :, 1] - boxes1[:, :, :, 3] / 2,
boxes1[:, :, :, 0] + boxes1[:, :, :, 2] / 2, boxes1[:, :, :, 1] + boxes1[:, :, :, 3] / 2])
boxes1 = tf.transpose(boxes1, [1, 2, 3, 0])
boxes2 = tf.pack([boxes2[0] - boxes2[2] / 2, boxes2[1] - boxes2[3] / 2,
boxes2[0] + boxes2[2] / 2, boxes2[1] + boxes2[3] / 2])
#calculate the left up point
lu = tf.maximum(boxes1[:, :, :, 0:2], boxes2[0:2])
rd = tf.minimum(boxes1[:, :, :, 2:], boxes2[2:])
#intersection
intersection = rd - lu
inter_square = intersection[:, :, :, 0] * intersection[:, :, :, 1]
mask = tf.cast(intersection[:, :, :, 0] > 0, tf.float32) * tf.cast(intersection[:, :, :, 1] > 0, tf.float32)
inter_square = mask * inter_square
#calculate the boxs1 square and boxs2 square
square1 = (boxes1[:, :, :, 2] - boxes1[:, :, :, 0]) * (boxes1[:, :, :, 3] - boxes1[:, :, :, 1])
square2 = (boxes2[2] - boxes2[0]) * (boxes2[3] - boxes2[1])
return inter_square/(square1 + square2 - inter_square + 1e-6)
示例3: _define_distance_to_clusters
def _define_distance_to_clusters(self, data):
"""Defines the Mahalanobis distance to the assigned Gaussian."""
# TODO(xavigonzalvo): reuse (input - mean) * cov^-1 * (input -
# mean) from log probability function.
self._all_scores = []
for shard in data:
all_scores = []
shard = tf.expand_dims(shard, 0)
for c in xrange(self._num_classes):
if self._covariance_type == FULL_COVARIANCE:
cov = self._covs[c, :, :]
elif self._covariance_type == DIAG_COVARIANCE:
cov = tf.diag(self._covs[c, :])
inverse = tf.matrix_inverse(cov + self._min_var)
inv_cov = tf.tile(
tf.expand_dims(inverse, 0),
tf.pack([self._num_examples, 1, 1]))
diff = tf.transpose(shard - self._means[c, :, :], perm=[1, 0, 2])
m_left = tf.batch_matmul(diff, inv_cov)
all_scores.append(tf.sqrt(tf.batch_matmul(
m_left, tf.transpose(diff, perm=[0, 2, 1])
)))
self._all_scores.append(tf.reshape(
tf.concat(1, all_scores),
tf.pack([self._num_examples, self._num_classes])))
# Distance to the associated class.
self._all_scores = tf.concat(0, self._all_scores)
assignments = tf.concat(0, self.assignments())
rows = tf.to_int64(tf.range(0, self._num_examples))
indices = tf.concat(1, [tf.expand_dims(rows, 1),
tf.expand_dims(assignments, 1)])
self._scores = tf.gather_nd(self._all_scores, indices)
示例4: build_reparam_loss_kl
def build_reparam_loss_kl(self):
"""Build loss function. Its automatic differentiation
is a stochastic gradient of
.. math::
-ELBO = - ( E_{q(z; \lambda)} [ \log p(x | z) ]
+ KL(q(z; \lambda) || p(z)) )
based on the reparameterization trick. (Kingma and Welling, 2014)
It assumes the KL is analytic.
It assumes the prior is :math:`p(z) = \mathcal{N}(z; 0, 1)`
Computed by sampling from :math:`q(z;\lambda)` and evaluating the
expectation using Monte Carlo sampling.
"""
x = self.data
z = self.variational.sample(self.n_samples)
mu = tf.pack([layer.loc for layer in self.variational.layers])
sigma = tf.pack([layer.scale for layer in self.variational.layers])
self.loss = tf.reduce_mean(self.model.log_lik(x, z)) - \
kl_multivariate_normal(mu, sigma)
return -self.loss
示例5: inputs
def inputs(path):
whole = read_csv(FLAGS.batch_size, path)
features = tf.transpose(tf.pack(whole[0:FLAGS.max_sentence_len]))
label = tf.one_hot(
tf.transpose(tf.pack(whole[FLAGS.max_sentence_len])),
depth=2)
return features, label
示例6: build_model
def build_model(self):
video = tf.placeholder(tf.float32, [self.batch_size, self.n_lstm_steps, self.dim_image])
video_mask = tf.placeholder(tf.float32, [self.batch_size, self.n_lstm_steps])
HLness = tf.placeholder(tf.int32, [self.batch_size, self.n_lstm_steps])
HLness_mask = tf.placeholder(tf.float32, [self.batch_size, self.n_lstm_steps])
video_flat = tf.reshape(video, [-1, self.dim_image])
image_emb = tf.nn.xw_plus_b( video_flat, self.encode_image_W, self.encode_image_b) # (batch_size*n_lstm_steps, dim_hidden)
image_emb = tf.reshape(image_emb, [self.batch_size, self.n_lstm_steps, self.dim_hidden])
image_emb = tf.transpose(image_emb, [1,0,2]) # n x b x h
state2 = tf.zeros([self.batch_size, self.lstm2.state_size])
loss_HL = 0.0
_X = tf.reshape(image_emb, [-1, self.dim_hidden]) # (n x b) x h
_X = tf.split(0, self.n_lstm_steps, _X) # n x (b x h)
[output2, state2] = rnn.rnn(self.lstm_HL_net,_X,dtype=tf.float32) # n x (b x h)
output2 = tf.transpose(tf.pack(output2), [1,0,2]) # b x n x h
onehot_labels = []
logit_words = []
indices = tf.expand_dims(tf.range(0, self.n_lstm_steps, 1), 1) # n x 1
for ii in xrange(10):
labels = tf.expand_dims(HLness[ii,:], 1) # n x 1
concated = tf.concat(1, [indices, labels]) # n x 2
onehot_labels = tf.sparse_to_dense(concated, tf.pack([self.n_lstm_steps, 2]), 1.0, 0.0) # n x 2
logit_words = tf.nn.xw_plus_b(output2[ii,:,:], self.embed_HL_W, self.embed_HL_b) # n x 2
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logit_words, onehot_labels) # n x 1
cross_entropy = tf.mul(cross_entropy, HLness_mask[ii,:]) # n x 1
loss_HL += tf.reduce_sum(cross_entropy) # 1
loss_HL = loss_HL / tf.reduce_sum(HLness_mask)
loss = loss_HL
return loss, video, video_mask, HLness, HLness_mask
示例7: log_prob
def log_prob(self, xs, zs):
"""Return a vector [log p(xs, zs[1,:]), ..., log p(xs, zs[S,:])]."""
x = xs['x']
pi, mus, sigmas = zs
log_prior = dirichlet.logpdf(pi, self.alpha)
log_prior += tf.reduce_sum(norm.logpdf(mus, 0, np.sqrt(self.c)), 1)
log_prior += tf.reduce_sum(invgamma.logpdf(sigmas, self.a, self.b), 1)
# Loop over each sample zs[s, :].
log_lik = []
N = get_dims(x)[0]
n_samples = get_dims(pi)[0]
for s in range(n_samples):
# log-likelihood is
# sum_{n=1}^N log sum_{k=1}^K exp( log pi_k + log N(x_n; mu_k, sigma_k) )
# Create a K x N matrix, whose entry (k, n) is
# log pi_k + log N(x_n; mu_k, sigma_k).
matrix = []
for k in range(self.K):
matrix += [tf.ones(N)*tf.log(pi[s, k]) +
multivariate_normal.logpdf(x,
mus[s, (k*self.D):((k+1)*self.D)],
sigmas[s, (k*self.D):((k+1)*self.D)])]
matrix = tf.pack(matrix)
# log_sum_exp() along the rows is a vector, whose nth
# element is the log-likelihood of data point x_n.
vector = log_sum_exp(matrix, 0)
# Sum over data points to get the full log-likelihood.
log_lik_z = tf.reduce_sum(vector)
log_lik += [log_lik_z]
return log_prior + tf.pack(log_lik)
示例8: _composition_function
def _composition_function(self, inputs, length, init_state=None):
if self._composition == "GRU":
cell = GRUCell(self._size)
return dynamic_rnn(cell, inputs, sequence_length=length, time_major=True,
initial_state=init_state, dtype=tf.float32)[0]
elif self._composition == "LSTM":
cell = BasicLSTMCell(self._size)
init_state = tf.concat(1, [tf.zeros_like(init_state, tf.float32), init_state]) if init_state else None
outs = dynamic_rnn(cell, inputs, sequence_length=length, time_major=True,
initial_state=init_state, dtype=tf.float32)[0]
return outs
elif self._composition == "BiGRU":
cell = GRUCell(self._size // 2, self._size)
init_state_fw, init_state_bw = tf.split(1, 2, init_state) if init_state else (None, None)
with tf.variable_scope("forward"):
fw_outs = dynamic_rnn(cell, inputs, sequence_length=length, time_major=True,
initial_state=init_state_fw, dtype=tf.float32)[0]
with tf.variable_scope("backward"):
rev_inputs = tf.reverse_sequence(tf.pack(inputs), length, 0, 1)
rev_inputs = [tf.reshape(x, [-1, self._size]) for x in tf.split(0, len(inputs), rev_inputs)]
bw_outs = dynamic_rnn(cell, rev_inputs, sequence_length=length, time_major=True,
initial_state=init_state_bw, dtype=tf.float32)[0]
bw_outs = tf.reverse_sequence(tf.pack(bw_outs), length, 0, 1)
bw_outs = [tf.reshape(x, [-1, self._size]) for x in tf.split(0, len(inputs), bw_outs)]
return [tf.concat(1, [fw_out, bw_out]) for fw_out, bw_out in zip(fw_outs, bw_outs)]
else:
raise NotImplementedError("Other compositions not implemented yet.")
示例9: compute_loss
def compute_loss(self,emb_batch,curr_batch_size=None):
outloss=[]
prediction=[]
for idx_batch in range(self.config.batch_size):
tree_states=self.compute_states(emb_batch,idx_batch)
logits = self.create_output(tree_states)
labels1=tf.gather(self.labels,idx_batch)
labels2=tf.reduce_sum(tf.to_int32(tf.not_equal(labels1,-1)))
labels=tf.gather(labels1,tf.range(labels2))
loss = self.calc_loss(logits,labels)
pred = tf.nn.softmax(logits)
pred_root=tf.gather(pred,labels2-1)
prediction.append(pred_root)
outloss.append(loss)
batch_loss=tf.pack(outloss)
self.pred = tf.pack(prediction)
return batch_loss
示例10: inference1
def inference1(data):
data_shape_l = data.get_shape().as_list()
with tf.variable_scope('conv1') as scope:
weights = _variable_with_weight_decay('weights', shape=[3, 3, 3, 32],wd=0.0)
biases = _variable_on_cpu('biases', [32], tf.constant_initializer(0.0))
h_conv1 = _conv2d(data, weights, biases, [1,2,2,1])
with tf.variable_scope('conv2') as scope:
weights = _variable_with_weight_decay('weights', shape=[3, 3, 32, 32],wd=0.0)
biases = _variable_on_cpu('biases', [32], tf.constant_initializer(0.0))
h_conv2 = _conv2d(h_conv1, weights, biases, [1,1,1,1])
with tf.variable_scope('deconv1') as scope:
weights = _variable_with_weight_decay('weights', shape=[3, 3, 32, 32],wd=0.0)
biases = _variable_on_cpu('biases', [32], tf.constant_initializer(0.0))
output_shape = tf.pack(h_conv1.get_shape().as_list())
h_dconv1 = _dconv2d(h_conv2, weights, biases, output_shape, [1,1,1,1])
with tf.variable_scope('deconv2') as scope:
weights = _variable_with_weight_decay('weights', shape=[3, 3, 3, 32],wd=0.0)
biases = _variable_on_cpu('biases', [3], tf.constant_initializer(0.0))
output_shape = tf.pack(data_shape_l)
h_dconv2 = _dconv2d(h_dconv1, weights, biases, output_shape, [1,2,2,1])
# with tf.variable_scope('deconv1') as scope:
# weights = _variable_with_weight_decay('weights', shape=[3, 3, 3, 32],
# stddev=1e-4, wd=0.0)
# biases = _variable_on_cpu('biases', [3], tf.constant_initializer(0.0))
# output_shape = tf.pack(data_shape_l)
# h_dconv1 = _dconv2d(h_conv1, weights, biases, output_shape, [1,2,2,1])
return h_dconv2
示例11: lstm_cell
def lstm_cell(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.
"""
i_list = tf.pack([i, i, i, i])
#print i_list.get_shape().as_list()
o_list = tf.pack([o, o, o, o])
ins = tf.batch_matmul(i_list, fico_x)
outs = tf.batch_matmul(o_list, fico_m)
h_x = ins + outs + fico_b
#print h_x.get_shape().as_list()
#forget_gate = tf.sigmoid(tf.matmul(i, fx) + tf.matmul(o, fm) + fb)
forget_gate = tf.sigmoid(h_x[0,:,:])
#input_gate = tf.sigmoid(tf.matmul(i, ix) + tf.matmul(o, im) + ib)
input_gate = tf.sigmoid(h_x[1,:,:])
#update = tf.tanh(tf.matmul(i, cx) + tf.matmul(o, cm) + cb)
update = tf.tanh(h_x[2,:,:])
state = forget_gate*state + input_gate*update
#output_gate = tf.sigmoid(tf.matmul(i, ox) + tf.matmul(o, om) + ob)
output_gate = tf.sigmoid(h_x[3,:,:])
h = output_gate * tf.tanh(state)
#print 'h', h.get_shape().as_list()
return h, state
示例12: _build_annealed_losses
def _build_annealed_losses(self, outputs, labels, anneal_factors):
sequence_length = len(outputs)
packed_outputs = tf.pack(outputs)
tiled_labels = tf.pack([labels for i in range(sequence_length)])
accumulated_losses = -tf.reduce_sum(tiled_labels * tf.log(packed_outputs), [1, 2])
annealed_losses = tf.mul(anneal_factors, tf.concat(0, accumulated_losses))
return annealed_losses
示例13: build_predict
def build_predict(self, Xnew, full_cov=False):
"""
Compute the mean and variance of the latent function at some new points
Xnew. Note that this is very similar to the SGPR prediction, for whcih
there are notes in the SGPR notebook.
"""
num_inducing = tf.shape(self.Z)[0]
psi0, psi1, psi2 = ke.build_psi_stats(self.Z, self.kern, self.X_mean, self.X_var)
Kuu = self.kern.K(self.Z) + eye(num_inducing) * 1e-6
Kus = self.kern.K(self.Z, Xnew)
sigma2 = self.likelihood.variance
sigma = tf.sqrt(sigma2)
L = tf.cholesky(Kuu)
A = tf.matrix_triangular_solve(L, tf.transpose(psi1), lower=True) / sigma
tmp = tf.matrix_triangular_solve(L, psi2, lower=True)
AAT = tf.matrix_triangular_solve(L, tf.transpose(tmp), lower=True) / sigma2
B = AAT + eye(num_inducing)
LB = tf.cholesky(B)
c = tf.matrix_triangular_solve(LB, tf.matmul(A, self.Y), lower=True) / sigma
tmp1 = tf.matrix_triangular_solve(L, Kus, lower=True)
tmp2 = tf.matrix_triangular_solve(LB, tmp1, lower=True)
mean = tf.matmul(tf.transpose(tmp2), c)
if full_cov:
var = self.kern.K(Xnew) + tf.matmul(tf.transpose(tmp2), tmp2)\
- tf.matmul(tf.transpose(tmp1), tmp1)
shape = tf.pack([1, 1, tf.shape(self.Y)[1]])
var = tf.tile(tf.expand_dims(var, 2), shape)
else:
var = self.kern.Kdiag(Xnew) + tf.reduce_sum(tf.square(tmp2), 0)\
- tf.reduce_sum(tf.square(tmp1), 0)
shape = tf.pack([1, tf.shape(self.Y)[1]])
var = tf.tile(tf.expand_dims(var, 1), shape)
return mean + self.mean_function(Xnew), var
示例14: _rnn_template
def _rnn_template(incoming, cell, dropout=None, return_seq=False,
return_state=False, initial_state=None, dynamic=False,
scope=None, name="LSTM"):
""" RNN Layer Template. """
sequence_length = None
if dynamic:
sequence_length = retrieve_seq_length_op(
incoming if isinstance(incoming, tf.Tensor) else tf.pack(incoming))
input_shape = utils.get_incoming_shape(incoming)
with tf.variable_op_scope([incoming], scope, name) as scope:
name = scope.name
_cell = cell
# Apply dropout
if dropout:
if type(dropout) in [tuple, list]:
in_keep_prob = dropout[0]
out_keep_prob = dropout[1]
elif isinstance(dropout, float):
in_keep_prob, out_keep_prob = dropout, dropout
else:
raise Exception("Invalid dropout type (must be a 2-D tuple of "
"float)")
cell = DropoutWrapper(cell, in_keep_prob, out_keep_prob)
inference = incoming
# If a tensor given, convert it to a per timestep list
if type(inference) not in [list, np.array]:
ndim = len(input_shape)
assert ndim >= 3, "Input dim should be at least 3."
axes = [1, 0] + list(range(2, ndim))
inference = tf.transpose(inference, (axes))
inference = tf.unpack(inference)
outputs, state = _rnn(cell, inference, dtype=tf.float32,
initial_state=initial_state, scope=name,
sequence_length=sequence_length)
# Retrieve RNN Variables
c = tf.GraphKeys.LAYER_VARIABLES + '/' + scope.name
for v in [_cell.W, _cell.b]:
if hasattr(v, "__len__"):
for var in v: tf.add_to_collection(c, var)
else:
tf.add_to_collection(c, v)
# Track activations.
tf.add_to_collection(tf.GraphKeys.ACTIVATIONS, outputs[-1])
if dynamic:
outputs = tf.transpose(tf.pack(outputs), [1, 0, 2])
o = advanced_indexing_op(outputs, sequence_length)
else:
o = outputs if return_seq else outputs[-1]
# Track output tensor.
tf.add_to_collection(tf.GraphKeys.LAYER_TENSOR + '/' + name, o)
return (o, state) if return_state else o
示例15: total_variation_loss
def total_variation_loss(layer):
shape = tf.shape(layer)
height = shape[1]
width = shape[2]
y = tf.slice(layer, [0,0,0,0], tf.pack([-1,height-1,-1,-1])) - tf.slice(layer, [0,1,0,0], [-1,-1,-1,-1])
x = tf.slice(layer, [0,0,0,0], tf.pack([-1,-1,width-1,-1])) - tf.slice(layer, [0,0,1,0], [-1,-1,-1,-1])
return tf.nn.l2_loss(x) / tf.to_float(tf.size(x)) + tf.nn.l2_loss(y) / tf.to_float(tf.size(y))