Python tensorflow.unpack() Examples
The following are 30
code examples of tensorflow.unpack().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow
, or try the search function
.
Example #1
Source File: tflearn_seq2seq.py From tflearn_seq2seq with MIT License | 6 votes |
def sequence_loss(self, y_pred, y_true): ''' Loss function for the seq2seq RNN. Reshape predicted and true (label) tensors, generate dummy weights, then use seq2seq.sequence_loss to actually compute the loss function. ''' if self.verbose > 2: print ("my_sequence_loss y_pred=%s, y_true=%s" % (y_pred, y_true)) logits = tf.unpack(y_pred, axis=1) # list of [-1, num_decoder_synbols] elements targets = tf.unpack(y_true, axis=1) # y_true has shape [-1, self.out_seq_len]; unpack to list of self.out_seq_len [-1] elements if self.verbose > 2: print ("my_sequence_loss logits=%s" % (logits,)) print ("my_sequence_loss targets=%s" % (targets,)) weights = [tf.ones_like(yp, dtype=tf.float32) for yp in targets] if self.verbose > 4: print ("my_sequence_loss weights=%s" % (weights,)) sl = seq2seq.sequence_loss(logits, targets, weights) if self.verbose > 2: print ("my_sequence_loss return = %s" % sl) return sl
Example #2
Source File: dm_utils.py From deep-makeover with MIT License | 6 votes |
def distort_image(image): """Perform random distortions to the given 4D image and return result""" # Switch to 3D as that's what these operations require slices = tf.unpack(image) output = [] # Perform pixel-wise distortions for image in slices: image = tf.image.random_flip_left_right(image) image = tf.image.random_saturation(image, .2, 2.) image += tf.truncated_normal(image.get_shape(), stddev=.05) image = tf.image.random_contrast(image, .85, 1.15) image = tf.image.random_brightness(image, .3) output.append(image) # Go back to 4D image = tf.pack(output) return image
Example #3
Source File: factorizations.py From elbow with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _logp(self, result, weights, centers, std): total_logps = None # loop over clusters for i, center in enumerate(tf.unpack(centers)): # compute vector of likelihoods that each point could be generated from *this* cluster cluster_lls = tf.reduce_sum(util.dists.gaussian_log_density(result, center, std), 1) # sum these likelihoods, weighted by cluster probabilities cluster_logps = tf.log(weights[i]) + cluster_lls if total_logps is not None: total_logps = util.logsumexp(total_logps, cluster_logps) else: total_logps = cluster_logps # finally sum the log probabilities of all points to get a likelihood for the dataset obs_lp = tf.reduce_sum(total_logps) return obs_lp
Example #4
Source File: bernoulli_raw_beta_scorefn.py From elbow with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, N, n_thetas=1): self.N = N self.theta_q_alpha = tf.Variable(1.0, name="theta_q_alpha") self.theta_q_beta = tf.Variable(2.0, name="theta_q_beta") self.data = tf.placeholder(dtype=tf.float32, shape=(N,), name="data") self.thetas = tf.placeholder(shape=(n_thetas,), dtype=tf.float32, name="thetas") self.thetas_q_log_density = tf.reduce_sum(dists.beta_log_density(self.thetas, alpha=self.theta_q_alpha, beta=self.theta_q_beta)) self.thetas_prior = tf.reduce_sum(dists.beta_log_density(self.thetas, alpha=1., beta=1.) ) self.data_liks = tf.pack([tf.reduce_sum(dists.bernoulli_log_density(self.data, theta)) for theta in tf.unpack(self.thetas)]) self.joint_density = self.data_liks + self.thetas_prior self.stochastic_elbo = self.joint_density - self.thetas_q_log_density # TODO: add control variates self.surrogate = tf.reduce_mean(self.thetas_q_log_density * tf.stop_gradient(self.stochastic_elbo) + self.stochastic_elbo)
Example #5
Source File: KBQA.py From TextKBQA with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __call__(self, memory, question, question_lengths): # split memory and get corresponding embeddings e1, r, e2 = tf.unpack(memory, axis=2) C = tf.ones_like(e1, dtype='float32') * -1000 mask = tf.not_equal(e1, self.entity_vocab_size - 1) key = self.get_key_embedding(e1, r) value = self.get_value_embedding(e2) ques = self.get_question_embedding(question, question_lengths) # get attention on retrived informations based on the question attn_ques = self.seek_attention(ques, key, value, C, mask) # output embeddings - share with entity lookup table # B = tf.slice(self.entity_lookup_table, [0, 0], [1789936, -1]) B = self.entity_lookup_table_extended # project down model_answer = tf.add(tf.matmul(attn_ques, self.W1), self.b1) # model_answer: [B, D] logits = tf.matmul(model_answer, B, transpose_b=True, name='ent_mul_manzil') # scores: [B, num_entities] return logits
Example #6
Source File: evaluate.py From iLID with MIT License | 6 votes |
def kernel_summary(sess): with sess.as_default(): for layer in ["conv1", "conv2", "conv3"]: with tf.variable_scope(layer, reuse=True): weights = tf.get_variable('weights') kernels = tf.unpack(tf.transpose(weights, perm=[3,2,0,1])) for i,kernel in enumerate(kernels): #[12, 6, 6] -> 12 x [8, 8] padding = [[1,1], [1,1]] padded_kernels = [tf.pad(single_kernel, padding) for single_kernel in tf.unpack(kernel)] #12 x [8, 8] -> [6, 12 * 8] horizontally_concatenated = tf.concat(1, padded_kernels) image = horizontally_concatenated.eval() misc.imsave(layer + "_" + str(i) + ".png", image)
Example #7
Source File: build_resnet_sdc.py From tensorflow-litterbox with Apache License 2.0 | 6 votes |
def _build_global_context( net, is_training=False, bayesian=False, dropout_keep_prob=0.8): with tf.variable_scope('GlobalContext'): # Reduce feature dimension before LSTM to reduce param count net = slim.conv2d(net, 1024, 1, padding='VALID', scope='conv_reduce_1x1') #net = slim.dropout(net, dropout_keep_prob, is_training=bayesian or is_training, scope='Dropout') rows = tf.unpack(net, axis=1) net = tf.pack( [lstm.bidir_lstm(r, 512, scope='row%d' % i) for i, r in enumerate(rows)], axis=1) print('Horizontal LSTM', net.get_shape()) cols = tf.unpack(net, axis=2) net = tf.pack( [lstm.bidir_lstm(r, 512, scope='col%d' % i) for i, r in enumerate(cols)], axis=2) print('Vertical LSTM', net.get_shape()) return net
Example #8
Source File: deepaudio.py From iLID with MIT License | 6 votes |
def _kernel_summary(conv_weights, sess): """Helper to create image summaries for convolutional kernels Creates an image summary of a convolutional kernel Args: kernel: Variable Returns: nothing """ #[6,6,12,12] -> [12,12,6,6] -> 12 x [12,6,6] kernels = tf.unpack(tf.transpose(conv_weights, perm=[3,2,0,1])) for i,kernel in enumerate(kernels): #[12, 6, 6] -> 12 x [8, 8] padding = [[1,1], [1,1]] padded_kernels = [tf.pad(single_kernel, padding) for single_erknel in tf.unpack(kernel)] #12 x [8, 8] -> [6, 12 * 8] horizontally_concatenated = tf.concat(1, single_layer_kernels)
Example #9
Source File: pca.py From elbow with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _logp(self, result, Z, mu, std): n, d_output = self.shape cov = tf.matmul(Z, tf.transpose(Z)) + tf.diag(tf.ones(n,)*std) L = tf.cholesky(cov) r = result - mu out_cols = tf.unpack(r, axis=1) lps = [multivariate_gaussian_log_density(col, mu=0., L=L) for col in out_cols] return tf.reduce_sum(tf.pack(lps))
Example #10
Source File: pca.py From elbow with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _logp(self, result, X, W, mu, std): pred_z, L, std = self._build_inverse_projection(X, W, mu, std) rows = tf.unpack(result - pred_z) lps = [multivariate_gaussian_log_density(r, mu=0, L_prec=L/std) for r in rows] return tf.reduce_sum(tf.pack(lps))
Example #11
Source File: symmetry_qs.py From elbow with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _logp(self, result, **kwargs): cols = tf.unpack(result, axis=1) lps = [] for perm_cols in itertools.permutations(cols): permuted = tf.pack(perm_cols, axis=1) lp_base = self.dist._logp(permuted, **kwargs) lps.append(lp_base) packed_lps = tf.pack(lps) self.cols = cols self.packed_lps = packed_lps return util.reduce_logsumexp(packed_lps) - np.log(len(lps))
Example #12
Source File: time_series.py From elbow with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _sample(self, prior_mean, prior_cov, transition_mat, transition_mean, transition_cov, observation_mat=None, observation_mean=None, observation_cov=None): transition_mean = tf.reshape(transition_mean, shape=(self.D, 1)) transition_eps = tf.random_normal(shape=(self.T, self.D)) transition_epses = [tf.reshape(n, shape=(self.D, 1)) for n in tf.unpack(transition_eps)] prior_mean = tf.reshape(prior_mean, shape=(self.D, 1)) prior_cov_L = tf.cholesky(prior_cov) state = prior_mean + tf.matmul(prior_cov_L, transition_epses[0]) transition_cov_L = tf.cholesky(transition_cov) if not self._flag_no_obs: observation_cov_L = tf.cholesky(observation_cov) obs_eps = tf.random_normal(shape=(self.T, self.K)) obs_epses = [tf.reshape(n, shape=(self.K, 1)) for n in tf.unpack(obs_eps)] observation_mean = tf.reshape(observation_mean, shape=(self.K, 1)) output = [] hidden = [] for t in range(self.T): if not self._flag_no_obs: pred_obs = tf.matmul(observation_mat, state) + observation_mean output.append(pred_obs + tf.matmul(observation_cov_L, obs_epses[t])) else: output.append(state) hidden.append(state) if t < self.T-1: state_noise = transition_mean + tf.matmul(transition_cov_L, transition_epses[t+1]) state = tf.matmul(transition_mat, state) + state_noise self._sampled_hidden = hidden return tf.pack(tf.squeeze(output))
Example #13
Source File: processor_sdc.py From tensorflow-litterbox with Apache License 2.0 | 5 votes |
def process_example(self, tensors, mode='eval', thread_id=0): train = (mode == 'train') image, image_timestamp, camera_id = tensors[:3] #FIXME push single/multi image handling into image_process_sdc if we want to share random augmentations if self.num_input_images > 1: assert(len(image.get_shape()) > 0) print('Multi image', image.get_shape()) split_image = tf.unpack(image) split_processed = [] for i, x in enumerate(split_image): suffix = '%d' % i xp, _ = image_preprocess_sdc( x, camera_id, height=self.height, width=self.width, image_fmt=self.image_fmt, normalize=self.standardize_input, train=train, summary_suffix=suffix, thread_id=thread_id) split_processed.append(xp) processed_image = tf.pack(split_processed) #FIXME need to sort out flip across mult-images flip_coeff = tf.constant(1.0, dtype=tf.float32) else: print('Single image') processed_image, flip_coeff = image_preprocess_sdc( image, camera_id, height=self.height, width=self.width, image_fmt=self.image_fmt, normalize=self.standardize_input, train=train, thread_id=thread_id) if mode != 'pred': steering_angle, gps_coord = tensors[-2:] if steering_angle is not None: steering_angle = tf.mul(steering_angle, flip_coeff) if self.standardize_labels: steering_angle /= STEERING_STD elif self.mu_law_steering: print("Encode mu-law angles") steering_angle = mu_law_steering_enc(steering_angle) if gps_coord is not None and self.standardize_labels: gps_coord = (gps_coord - GPS_MEAN) / GPS_STD return processed_image, image_timestamp, steering_angle, gps_coord else: return processed_image, image_timestamp, tf.zeros((1,)), tf.zeros((2,))
Example #14
Source File: time_series.py From elbow with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _sample_and_entropy(self, transition_matrices, step_noise_means, step_noise_covs, unary_means, unary_variances): T, d = self.shape upwards_means = tf.unpack(unary_means) upwards_vars = tf.unpack(unary_variances) unary_factors = [MVGaussianMeanCov(mean, tf.diag(vs)) for (mean, vs) in zip(upwards_means, upwards_vars)] # transition_matrices is either a d x d matrix, or a T x d x d tensor if len(transition_matrices.get_shape()) == 2: transition_matrices = [transition_matrices for i in range(T)] # step noise mean is either a (d,)-vector or a T x d matrix if len(step_noise_means.get_shape()) == 1: step_noise_means = [step_noise_means for i in range(T)] # step noise cov is either a d x d matrix or a T x d x d tensor if len(step_noise_covs.get_shape()) == 2: step_noise_covs = [step_noise_covs for i in range(T)] step_noise_factors = [MVGaussianMeanCov(step_noise_means[t], step_noise_covs[t]) for t in range(T)] back_filtered, logZ = self._pass_messages_backwards(transition_matrices, step_noise_factors, unary_factors) self._back_filtered = back_filtered self._logZ = logZ eps = tf.random_normal(shape=self.shape) sample, entropy = self._sample_forward(back_filtered, transition_matrices, step_noise_factors, eps) return sample, entropy
Example #15
Source File: time_series.py From elbow with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _sample_forward(self, back_filtered, transition_matrices, step_noise_factors, eps): samples = [] T, d = self.shape epses = tf.unpack(eps) sampling_dist = back_filtered[0] z_i = sampling_dist.sample(epses[0]) samples.append(z_i) sampling_dists = [sampling_dist] entropies = [sampling_dist.entropy()] for t in np.arange(1, T): pred_mean = tf.matmul(transition_matrices[t-1], z_i) noise = step_noise_factors[t-1] incoming = MVGaussianMeanCov(noise.mean() + pred_mean, noise.cov()) sampling_dist = back_filtered[t].multiply_density(incoming) sampling_dists.append(sampling_dist) z_i = sampling_dist.sample(epses[t]) entropies.append(sampling_dist.entropy()) samples.append(z_i) self.sampling_dists = sampling_dists self.entropies = entropies entropy = tf.reduce_sum(tf.pack(entropies)) sample = tf.reshape(tf.squeeze(tf.pack(samples)), self.shape) return sample, entropy
Example #16
Source File: neural.py From elbow with BSD 3-Clause "New" or "Revised" License | 5 votes |
def layer(inp, w, b): if len(inp.get_shape()) == 2: return tf.matmul(inp, w) + b else: return tf.pack([tf.matmul(inp_slice, w) + b for inp_slice in tf.unpack(inp)])
Example #17
Source File: my_lstm_test.py From ChatBotCourse with MIT License | 5 votes |
def sequence_loss(y_pred, y_true): logits = tf.unpack(y_pred, axis=1) targets = tf.unpack(y_true, axis=1) weights = [tf.ones_like(yp, dtype=tf.float32) for yp in targets] return seq2seq.sequence_loss(logits, targets, weights)
Example #18
Source File: seq2seq_example.py From ChatBotCourse with MIT License | 5 votes |
def sequence_loss(self, y_pred, y_true): ''' Loss function for the seq2seq RNN. Reshape predicted and true (label) tensors, generate dummy weights, then use seq2seq.sequence_loss to actually compute the loss function. ''' #print ("my_sequence_loss y_pred=%s, y_true=%s" % (y_pred, y_true)) logits = tf.unpack(y_pred, axis=1) # list of [-1, num_decoder_synbols] elements targets = tf.unpack(y_true, axis=1) # y_true has shape [-1, self.out_seq_len]; unpack to list of self.out_seq_len [-1] elements #print ("my_sequence_loss logits=%s" % (logits,)) #print ("my_sequence_loss targets=%s" % (targets,)) weights = [tf.ones_like(yp, dtype=tf.float32) for yp in targets] #print ("my_sequence_loss weights=%s" % (weights,)) sl = seq2seq.sequence_loss(logits, targets, weights) #print ("my_sequence_loss return = %s" % sl) return sl
Example #19
Source File: lstm_train.py From ChatBotCourse with MIT License | 5 votes |
def sequence_loss(y_pred, y_true): logits = tf.unpack(y_pred, axis=1) targets = tf.unpack(y_true, axis=1) weights = [tf.ones_like(yp, dtype=tf.float32) for yp in targets] return seq2seq.sequence_loss(logits, targets, weights)
Example #20
Source File: transforms.py From elbow with BSD 3-Clause "New" or "Revised" License | 5 votes |
def inverse(cls, transformed, return_log_jac=False, **kwargs): # first scale to have a unit column cols = tf.unpack(transformed, axis=1) k = len(cols) last_col = cols[-1] x = tf.pack( [col / last_col for col in cols[:-1]], axis=1) if return_log_jac: log_jacobian = -(k-1) * tf.reduce_sum(tf.log(last_col)) return x, log_jacobian else: return x
Example #21
Source File: array_ops.py From deep_image_model with Apache License 2.0 | 5 votes |
def unpack(value, num=None, axis=0, name="unpack"): """DEPRECATED: Use unstack. Unpacks the given dimension of a rank-`R` tensor into rank-`(R-1)` tensors. Unpacks `num` tensors from `value` by chipping it along the `axis` dimension. If `num` is not specified (the default), it is inferred from `value`'s shape. If `value.shape[axis]` is not known, `ValueError` is raised. For example, given a tensor of shape `(A, B, C, D)`; If `axis == 0` then the i'th tensor in `output` is the slice `value[i, :, :, :]` and each tensor in `output` will have shape `(B, C, D)`. (Note that the dimension unpacked along is gone, unlike `split`). If `axis == 1` then the i'th tensor in `output` is the slice `value[:, i, :, :]` and each tensor in `output` will have shape `(A, C, D)`. Etc. This is the opposite of pack. The numpy equivalent is tf.unpack(x, n) = list(x) Args: value: A rank `R > 0` `Tensor` to be unpacked. num: An `int`. The length of the dimension `axis`. Automatically inferred if `None` (the default). axis: An `int`. The axis to unpack along. Defaults to the first dimension. Supports negative indexes. name: A name for the operation (optional). Returns: The list of `Tensor` objects unpacked from `value`. Raises: ValueError: If `num` is unspecified and cannot be inferred. ValueError: If `axis` is out of the range [-R, R). """ return unstack(value, num, axis, name)
Example #22
Source File: array_ops.py From deep_image_model with Apache License 2.0 | 5 votes |
def pack(values, axis=0, name="pack"): """Packs a list of rank-`R` tensors into one rank-`(R+1)` tensor. Packs the list of tensors in `values` into a tensor with rank one higher than each tensor in `values`, by packing them along the `axis` dimension. Given a list of length `N` of tensors of shape `(A, B, C)`; if `axis == 0` then the `output` tensor will have the shape `(N, A, B, C)`. if `axis == 1` then the `output` tensor will have the shape `(A, N, B, C)`. Etc. For example: ```prettyprint # 'x' is [1, 4] # 'y' is [2, 5] # 'z' is [3, 6] pack([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # Pack along first dim. pack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]] ``` This is the opposite of unpack. The numpy equivalent is tf.pack([x, y, z]) = np.asarray([x, y, z]) Args: values: A list of `Tensor` objects with the same shape and type. axis: An `int`. The axis to pack along. Defaults to the first dimension. Supports negative indexes. name: A name for this operation (optional). Returns: output: A packed `Tensor` with the same type as `values`. Raises: ValueError: If `axis` is out of the range [-(R+1), R+1). """ return stack(values, axis, name) # pylint: disable=invalid-name
Example #23
Source File: unpack_op_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testAxisOutOfNegativeRange(self): a = tf.constant([[1, 2, 3], [4, 5, 6]], name='a') with self.assertRaisesRegexp(ValueError, r'axis = -3 not in \[-2, 2\)'): tf.unpack(a, axis=-3) with self.assertRaisesRegexp(ValueError, r'axis = -3 not in \[-2, 2\)'): tf.unstack(a, axis=-3)
Example #24
Source File: unpack_op_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testAxisOutOfRange(self): a = tf.constant([[1, 2, 3], [4, 5, 6]], name='a') with self.assertRaisesRegexp(ValueError, r'axis = 2 not in \[-2, 2\)'): tf.unpack(a, axis=2) with self.assertRaisesRegexp(ValueError, r'axis = 2 not in \[-2, 2\)'): tf.unstack(a, axis=2)
Example #25
Source File: unpack_op_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testAxis0Default(self): with self.test_session() as sess: a = tf.constant([[1, 2, 3], [4, 5, 6]], name='a') unpacked = sess.run(tf.unpack(a)) unstacked = sess.run(tf.unstack(a)) self.assertEqual(len(unpacked), 2) self.assertAllEqual(unpacked[0], [1, 2, 3]) self.assertAllEqual(unpacked[1], [4, 5, 6]) self.assertEqual(len(unstacked), 2) self.assertAllEqual(unstacked[0], [1, 2, 3]) self.assertAllEqual(unstacked[1], [4, 5, 6])
Example #26
Source File: unpack_op_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testAgainstNumpy(self): # For 1 to 5 dimensions. for i in range(1, 6): a = np.random.random(np.random.permutation(i) + 1) # For all the possible axis to split it, including negative indices. for j in range(-i, i): expected = np_split_squeeze(a, j) with self.test_session() as sess: actual_unpack = sess.run(tf.unpack(a, axis=j)) actual_unstack = sess.run(tf.unstack(a, axis=j)) self.assertAllEqual(expected, actual_unpack) self.assertAllEqual(expected, actual_unstack)
Example #27
Source File: unpack_op_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testCannotInferNumFromNoneShape(self): x = tf.placeholder(np.float32, shape=(None,)) with self.assertRaisesRegexp(ValueError, r'Cannot infer num from shape \(\?,\)'): tf.unpack(x) with self.assertRaisesRegexp(ValueError, r'Cannot infer num from shape \(\?,\)'): tf.unstack(x)
Example #28
Source File: unpack_op_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testCannotInferNumFromUnknownShape(self): x = tf.placeholder(np.float32) with self.assertRaisesRegexp( ValueError, r'Cannot infer num from shape <unknown>'): tf.unpack(x) with self.assertRaisesRegexp( ValueError, r'Cannot infer num from shape <unknown>'): tf.unstack(x)
Example #29
Source File: unpack_op_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testInferNum(self): with self.test_session(): for shape in (2,), (3,), (2, 3), (3, 2), (4, 3, 2): x = tf.placeholder(np.float32, shape=shape) cs = tf.unpack(x) self.assertEqual(type(cs), list) self.assertEqual(len(cs), shape[0]) cs = tf.unstack(x) self.assertEqual(type(cs), list) self.assertEqual(len(cs), shape[0])
Example #30
Source File: unpack_op_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testGradientsAxis1(self): for shape in (2, 3), (3, 2), (4, 3, 2): data = np.random.randn(*shape) out_shape = list(shape) del out_shape[1] for i in xrange(shape[1]): with self.test_session(use_gpu=True): x = tf.constant(data) cs = tf.unpack(x, num=shape[1], axis=1) err = tf.test.compute_gradient_error(x, shape, cs[i], out_shape) self.assertLess(err, 1e-6) cs = tf.unstack(x, num=shape[1], axis=1) err = tf.test.compute_gradient_error(x, shape, cs[i], out_shape) self.assertLess(err, 1e-6)