Python tensorflow.ones() Examples
The following are 30
code examples of tensorflow.ones().
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: box_list_ops_test.py From object_detector_app with MIT License | 6 votes |
def test_convert_to_normalized_and_back(self): coordinates = np.random.uniform(size=(100, 4)) coordinates = np.round(np.sort(coordinates) * 200) coordinates[:, 2:4] += 1 coordinates[99, :] = [0, 0, 201, 201] img = tf.ones((128, 202, 202, 3)) boxlist = box_list.BoxList(tf.constant(coordinates, tf.float32)) boxlist = box_list_ops.to_normalized_coordinates(boxlist, tf.shape(img)[1], tf.shape(img)[2]) boxlist = box_list_ops.to_absolute_coordinates(boxlist, tf.shape(img)[1], tf.shape(img)[2]) with self.test_session() as sess: out = sess.run(boxlist.get()) self.assertAllClose(out, coordinates)
Example #2
Source File: losses_test.py From DOTA_models with Apache License 2.0 | 6 votes |
def testReturnsCorrectNanLoss(self): batch_size = 3 num_anchors = 10 code_size = 4 prediction_tensor = tf.ones([batch_size, num_anchors, code_size]) target_tensor = tf.concat([ tf.zeros([batch_size, num_anchors, code_size / 2]), tf.ones([batch_size, num_anchors, code_size / 2]) * np.nan ], axis=2) weights = tf.ones([batch_size, num_anchors]) loss_op = losses.WeightedL2LocalizationLoss() loss = loss_op(prediction_tensor, target_tensor, weights=weights, ignore_nan_targets=True) expected_loss = (3 * 5 * 4) / 2.0 with self.test_session() as sess: loss_output = sess.run(loss) self.assertAllClose(loss_output, expected_loss)
Example #3
Source File: kfac.py From HardRLWithYoutube with MIT License | 6 votes |
def getStatsEigen(self, stats=None): if len(self.stats_eigen) == 0: stats_eigen = {} if stats is None: stats = self.stats tmpEigenCache = {} with tf.device('/cpu:0'): for var in stats: for key in ['fprop_concat_stats', 'bprop_concat_stats']: for stats_var in stats[var][key]: if stats_var not in tmpEigenCache: stats_dim = stats_var.get_shape()[1].value e = tf.Variable(tf.ones( [stats_dim]), name='KFAC_FAC/' + stats_var.name.split(':')[0] + '/e', trainable=False) Q = tf.Variable(tf.diag(tf.ones( [stats_dim])), name='KFAC_FAC/' + stats_var.name.split(':')[0] + '/Q', trainable=False) stats_eigen[stats_var] = {'e': e, 'Q': Q} tmpEigenCache[ stats_var] = stats_eigen[stats_var] else: stats_eigen[stats_var] = tmpEigenCache[ stats_var] self.stats_eigen = stats_eigen return self.stats_eigen
Example #4
Source File: losses_test.py From DOTA_models with Apache License 2.0 | 6 votes |
def testReturnsCorrectLoss(self): batch_size = 3 num_anchors = 10 code_size = 4 prediction_tensor = tf.ones([batch_size, num_anchors, code_size]) target_tensor = tf.zeros([batch_size, num_anchors, code_size]) weights = tf.constant([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]], tf.float32) loss_op = losses.WeightedL2LocalizationLoss() loss = loss_op(prediction_tensor, target_tensor, weights=weights) expected_loss = (3 * 5 * 4) / 2.0 with self.test_session() as sess: loss_output = sess.run(loss) self.assertAllClose(loss_output, expected_loss)
Example #5
Source File: losses_test.py From object_detector_app with MIT License | 6 votes |
def testReturnsCorrectLoss(self): batch_size = 3 num_anchors = 10 code_size = 4 prediction_tensor = tf.ones([batch_size, num_anchors, code_size]) target_tensor = tf.zeros([batch_size, num_anchors, code_size]) weights = tf.constant([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]], tf.float32) loss_op = losses.WeightedL2LocalizationLoss() loss = loss_op(prediction_tensor, target_tensor, weights=weights) expected_loss = (3 * 5 * 4) / 2.0 with self.test_session() as sess: loss_output = sess.run(loss) self.assertAllClose(loss_output, expected_loss)
Example #6
Source File: generator.py From UROP-Adversarial-Feature-Matching-for-Text-Generation with GNU Affero General Public License v3.0 | 6 votes |
def init_param(self): idm = self.input_dim hs = self.hidden_size ws = len(self.window) nf = idm * ws # author's special initlaization strategy. self.Wemb = tf.get_variable(name=self.name + '_Wemb', shape=[self.vocab_size, idm], dtype=tf.float32, initializer=tf.random_uniform_initializer()) self.bhid = tf.get_variable(name=self.name + '_bhid', shape=[self.vocab_size], dtype=tf.float32, initializer=tf.zeros_initializer()) self.Vhid = tf.get_variable(name=self.name + '_Vhid', shape=[hs, idm], dtype=tf.float32, initializer=tf.random_uniform_initializer()) self.Vhid = dot(self.Vhid, self.Wemb) # [hidden_size, vocab_size] self.i2h_W = tf.get_variable(name=self.name + '_i2h_W', shape=[idm, hs * 4], dtype=tf.float32, initializer=tf.random_uniform_initializer()) self.h2h_W = tf.get_variable(name=self.name + '_h2h_W', shape=[hs, hs * 4], dtype=tf.float32, initializer=tf.orthogonal_initializer()) self.z2h_W = tf.get_variable(name=self.name + '_z2h_W', shape=[nf, hs * 4], dtype=tf.float32, initializer=tf.random_uniform_initializer()) b_init_1 = tf.zeros((hs,)) b_init_2 = tf.ones((hs,)) * 3 b_init_3 = tf.zeros((hs,)) b_init_4 = tf.zeros((hs,)) b_init = tf.concat([b_init_1, b_init_2, b_init_3, b_init_4], axis=0) # b_init = tf.constant(b_init) # self.b = tf.get_variable(name=self.name + '_b', shape=[hs * 4], dtype=tf.float32, initializer=b_init) self.b = tf.get_variable(name=self.name + '_b', dtype=tf.float32, initializer=b_init) # ValueError: If initializer is a constant, do not specify shape. self.C0 = tf.get_variable(name=self.name + '_C0', shape=[nf, hs], dtype=tf.float32, initializer=tf.random_uniform_initializer()) self.b0 = tf.get_variable(name=self.name + '_b0', shape=[hs], dtype=tf.float32, initializer=tf.zeros_initializer())
Example #7
Source File: losses_test.py From object_detector_app with MIT License | 6 votes |
def testReturnsCorrectNanLoss(self): batch_size = 3 num_anchors = 10 code_size = 4 prediction_tensor = tf.ones([batch_size, num_anchors, code_size]) target_tensor = tf.concat([ tf.zeros([batch_size, num_anchors, code_size / 2]), tf.ones([batch_size, num_anchors, code_size / 2]) * np.nan ], axis=2) weights = tf.ones([batch_size, num_anchors]) loss_op = losses.WeightedL2LocalizationLoss() loss = loss_op(prediction_tensor, target_tensor, weights=weights, ignore_nan_targets=True) expected_loss = (3 * 5 * 4) / 2.0 with self.test_session() as sess: loss_output = sess.run(loss) self.assertAllClose(loss_output, expected_loss)
Example #8
Source File: kfac.py From lirpg with MIT License | 6 votes |
def getStatsEigen(self, stats=None): if len(self.stats_eigen) == 0: stats_eigen = {} if stats is None: stats = self.stats tmpEigenCache = {} with tf.device('/cpu:0'): for var in stats: for key in ['fprop_concat_stats', 'bprop_concat_stats']: for stats_var in stats[var][key]: if stats_var not in tmpEigenCache: stats_dim = stats_var.get_shape()[1].value e = tf.Variable(tf.ones( [stats_dim]), name='KFAC_FAC/' + stats_var.name.split(':')[0] + '/e', trainable=False) Q = tf.Variable(tf.diag(tf.ones( [stats_dim])), name='KFAC_FAC/' + stats_var.name.split(':')[0] + '/Q', trainable=False) stats_eigen[stats_var] = {'e': e, 'Q': Q} tmpEigenCache[ stats_var] = stats_eigen[stats_var] else: stats_eigen[stats_var] = tmpEigenCache[ stats_var] self.stats_eigen = stats_eigen return self.stats_eigen
Example #9
Source File: exporter_test.py From DOTA_models with Apache License 2.0 | 6 votes |
def _save_checkpoint_from_mock_model(self, checkpoint_path, use_moving_averages): g = tf.Graph() with g.as_default(): mock_model = FakeModel() preprocessed_inputs = mock_model.preprocess( tf.ones([1, 3, 4, 3], tf.float32)) predictions = mock_model.predict(preprocessed_inputs) mock_model.postprocess(predictions) if use_moving_averages: tf.train.ExponentialMovingAverage(0.0).apply() saver = tf.train.Saver() init = tf.global_variables_initializer() with self.test_session() as sess: sess.run(init) saver.save(sess, checkpoint_path)
Example #10
Source File: ops.py From object_detector_app with MIT License | 6 votes |
def expanded_shape(orig_shape, start_dim, num_dims): """Inserts multiple ones into a shape vector. Inserts an all-1 vector of length num_dims at position start_dim into a shape. Can be combined with tf.reshape to generalize tf.expand_dims. Args: orig_shape: the shape into which the all-1 vector is added (int32 vector) start_dim: insertion position (int scalar) num_dims: length of the inserted all-1 vector (int scalar) Returns: An int32 vector of length tf.size(orig_shape) + num_dims. """ with tf.name_scope('ExpandedShape'): start_dim = tf.expand_dims(start_dim, 0) # scalar to rank-1 before = tf.slice(orig_shape, [0], start_dim) add_shape = tf.ones(tf.reshape(num_dims, [1]), dtype=tf.int32) after = tf.slice(orig_shape, start_dim, [-1]) new_shape = tf.concat([before, add_shape, after], 0) return new_shape
Example #11
Source File: box_list_ops_test.py From object_detector_app with MIT License | 6 votes |
def test_convert_to_absolute_and_back(self): coordinates = np.random.uniform(size=(100, 4)) coordinates = np.sort(coordinates) coordinates[99, :] = [0, 0, 1, 1] img = tf.ones((128, 202, 202, 3)) boxlist = box_list.BoxList(tf.constant(coordinates, tf.float32)) boxlist = box_list_ops.to_absolute_coordinates(boxlist, tf.shape(img)[1], tf.shape(img)[2]) boxlist = box_list_ops.to_normalized_coordinates(boxlist, tf.shape(img)[1], tf.shape(img)[2]) with self.test_session() as sess: out = sess.run(boxlist.get()) self.assertAllClose(out, coordinates)
Example #12
Source File: box_list_ops_test.py From DOTA_models with Apache License 2.0 | 6 votes |
def test_convert_to_absolute_and_back(self): coordinates = np.random.uniform(size=(100, 4)) coordinates = np.sort(coordinates) coordinates[99, :] = [0, 0, 1, 1] img = tf.ones((128, 202, 202, 3)) boxlist = box_list.BoxList(tf.constant(coordinates, tf.float32)) boxlist = box_list_ops.to_absolute_coordinates(boxlist, tf.shape(img)[1], tf.shape(img)[2]) boxlist = box_list_ops.to_normalized_coordinates(boxlist, tf.shape(img)[1], tf.shape(img)[2]) with self.test_session() as sess: out = sess.run(boxlist.get()) self.assertAllClose(out, coordinates)
Example #13
Source File: ops.py From DOTA_models with Apache License 2.0 | 6 votes |
def expanded_shape(orig_shape, start_dim, num_dims): """Inserts multiple ones into a shape vector. Inserts an all-1 vector of length num_dims at position start_dim into a shape. Can be combined with tf.reshape to generalize tf.expand_dims. Args: orig_shape: the shape into which the all-1 vector is added (int32 vector) start_dim: insertion position (int scalar) num_dims: length of the inserted all-1 vector (int scalar) Returns: An int32 vector of length tf.size(orig_shape) + num_dims. """ with tf.name_scope('ExpandedShape'): start_dim = tf.expand_dims(start_dim, 0) # scalar to rank-1 before = tf.slice(orig_shape, [0], start_dim) add_shape = tf.ones(tf.reshape(num_dims, [1]), dtype=tf.int32) after = tf.slice(orig_shape, start_dim, [-1]) new_shape = tf.concat([before, add_shape, after], 0) return new_shape
Example #14
Source File: pix2pix_test.py From DeepLab_v3 with MIT License | 6 votes |
def test_output_size_nn_upsample_conv(self): batch_size = 2 height, width = 256, 256 num_outputs = 4 images = tf.ones((batch_size, height, width, 3)) with tf.contrib.framework.arg_scope(pix2pix.pix2pix_arg_scope()): logits, _ = pix2pix.pix2pix_generator( images, num_outputs, blocks=self._reduced_default_blocks(), upsample_method='nn_upsample_conv') with self.test_session() as session: session.run(tf.global_variables_initializer()) np_outputs = session.run(logits) self.assertListEqual([batch_size, height, width, num_outputs], list(np_outputs.shape))
Example #15
Source File: pix2pix_test.py From DeepLab_v3 with MIT License | 6 votes |
def test_output_size_conv2d_transpose(self): batch_size = 2 height, width = 256, 256 num_outputs = 4 images = tf.ones((batch_size, height, width, 3)) with tf.contrib.framework.arg_scope(pix2pix.pix2pix_arg_scope()): logits, _ = pix2pix.pix2pix_generator( images, num_outputs, blocks=self._reduced_default_blocks(), upsample_method='conv2d_transpose') with self.test_session() as session: session.run(tf.global_variables_initializer()) np_outputs = session.run(logits) self.assertListEqual([batch_size, height, width, num_outputs], list(np_outputs.shape))
Example #16
Source File: memory.py From DOTA_models with Apache License 2.0 | 6 votes |
def make_update_op(self, upd_idxs, upd_keys, upd_vals, batch_size, use_recent_idx, intended_output): """Function that creates all the update ops.""" mem_age_incr = self.mem_age.assign_add(tf.ones([self.memory_size], dtype=tf.float32)) with tf.control_dependencies([mem_age_incr]): mem_age_upd = tf.scatter_update( self.mem_age, upd_idxs, tf.zeros([batch_size], dtype=tf.float32)) mem_key_upd = tf.scatter_update( self.mem_keys, upd_idxs, upd_keys) mem_val_upd = tf.scatter_update( self.mem_vals, upd_idxs, upd_vals) if use_recent_idx: recent_idx_upd = tf.scatter_update( self.recent_idx, intended_output, upd_idxs) else: recent_idx_upd = tf.group() return tf.group(mem_age_upd, mem_key_upd, mem_val_upd, recent_idx_upd)
Example #17
Source File: pix2pix_test.py From DeepLab_v3 with MIT License | 6 votes |
def test_block_number_dictates_number_of_layers(self): batch_size = 2 height, width = 256, 256 num_outputs = 4 images = tf.ones((batch_size, height, width, 3)) blocks = [ pix2pix.Block(64, 0.5), pix2pix.Block(128, 0), ] with tf.contrib.framework.arg_scope(pix2pix.pix2pix_arg_scope()): _, end_points = pix2pix.pix2pix_generator( images, num_outputs, blocks) num_encoder_layers = 0 num_decoder_layers = 0 for end_point in end_points: if end_point.startswith('encoder'): num_encoder_layers += 1 elif end_point.startswith('decoder'): num_decoder_layers += 1 self.assertEqual(num_encoder_layers, len(blocks)) self.assertEqual(num_decoder_layers, len(blocks))
Example #18
Source File: pix2pix_test.py From DeepLab_v3 with MIT License | 6 votes |
def test_four_layers(self): batch_size = 2 input_size = 256 output_size = self._layer_output_size(input_size) output_size = self._layer_output_size(output_size) output_size = self._layer_output_size(output_size) output_size = self._layer_output_size(output_size, stride=1) output_size = self._layer_output_size(output_size, stride=1) images = tf.ones((batch_size, input_size, input_size, 3)) with tf.contrib.framework.arg_scope(pix2pix.pix2pix_arg_scope()): logits, end_points = pix2pix.pix2pix_discriminator( images, num_filters=[64, 128, 256, 512]) self.assertListEqual([batch_size, output_size, output_size, 1], logits.shape.as_list()) self.assertListEqual([batch_size, output_size, output_size, 1], end_points['predictions'].shape.as_list())
Example #19
Source File: common_layers.py From fine-lm with MIT License | 6 votes |
def ones_matrix_band_part(rows, cols, num_lower, num_upper, out_shape=None): """Matrix band part of ones.""" if all([isinstance(el, int) for el in [rows, cols, num_lower, num_upper]]): # Needed info is constant, so we construct in numpy if num_lower < 0: num_lower = rows - 1 if num_upper < 0: num_upper = cols - 1 lower_mask = np.tri(cols, rows, num_lower).T upper_mask = np.tri(rows, cols, num_upper) band = np.ones((rows, cols)) * lower_mask * upper_mask if out_shape: band = band.reshape(out_shape) band = tf.constant(band, tf.float32) else: band = tf.matrix_band_part( tf.ones([rows, cols]), tf.cast(num_lower, tf.int64), tf.cast(num_upper, tf.int64)) if out_shape: band = tf.reshape(band, out_shape) return band
Example #20
Source File: preprocessor.py From object_detector_app with MIT License | 5 votes |
def random_pixel_value_scale(image, minval=0.9, maxval=1.1, seed=None): """Scales each value in the pixels of the image. This function scales each pixel independent of the other ones. For each value in image tensor, draws a random number between minval and maxval and multiples the values with them. Args: image: rank 3 float32 tensor contains 1 image -> [height, width, channels] with pixel values varying between [0, 1]. minval: lower ratio of scaling pixel values. maxval: upper ratio of scaling pixel values. seed: random seed. Returns: image: image which is the same shape as input image. boxes: boxes which is the same shape as input boxes. """ with tf.name_scope('RandomPixelValueScale', values=[image]): color_coef = tf.random_uniform( tf.shape(image), minval=minval, maxval=maxval, dtype=tf.float32, seed=seed) image = tf.multiply(image, color_coef) image = tf.clip_by_value(image, 0.0, 1.0) return image
Example #21
Source File: box_list_ops_test.py From object_detector_app with MIT License | 5 votes |
def test_to_absolute_coordinates_already_abolute(self): coordinates = tf.constant([[0, 0, 100, 100], [25, 25, 75, 75]], tf.float32) img = tf.ones((128, 100, 100, 3)) boxlist = box_list.BoxList(coordinates) absolute_boxlist = box_list_ops.to_absolute_coordinates(boxlist, tf.shape(img)[1], tf.shape(img)[2]) with self.test_session() as sess: with self.assertRaisesOpError('assertion failed'): sess.run(absolute_boxlist.get())
Example #22
Source File: argmax_matcher_test.py From object_detector_app with MIT License | 5 votes |
def test_return_correct_matches_with_empty_rows(self): matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=None) sim = 0.2*tf.ones([0, 5]) match = matcher.match(sim) unmatched_cols = match.unmatched_column_indices() with self.test_session() as sess: res_unmatched_cols = sess.run(unmatched_cols) self.assertAllEqual(res_unmatched_cols, np.arange(5))
Example #23
Source File: normalizer.py From HardRLWithYoutube with MIT License | 5 votes |
def __init__(self, size, std=1.): self.size = size self.mean = tf.zeros(self.size, tf.float32) self.std = std * tf.ones(self.size, tf.float32)
Example #24
Source File: common_layers_test.py From fine-lm with MIT License | 5 votes |
def testSampleFromDiscretizedMixLogistic(self): batch = 2 height = 4 width = 4 num_mixtures = 5 seed = 42 logits = tf.concat( # assign all probability mass to first component [tf.ones([batch, height, width, 1]) * 1e8, tf.zeros([batch, height, width, num_mixtures - 1])], axis=-1) locs = tf.random_uniform([batch, height, width, num_mixtures * 3], minval=-.9, maxval=.9) log_scales = tf.ones([batch, height, width, num_mixtures * 3]) * -1e8 coeffs = tf.atanh(tf.zeros([batch, height, width, num_mixtures * 3])) pred = tf.concat([logits, locs, log_scales, coeffs], axis=-1) locs_0 = locs[..., :3] expected_sample = tf.clip_by_value(locs_0, -1., 1.) actual_sample = common_layers.sample_from_discretized_mix_logistic( pred, seed=seed) with self.test_session() as session: actual_sample_val, expected_sample_val = session.run( [actual_sample, expected_sample]) # Use a low tolerance: samples numerically differ, as the actual # implementation clips log-scales so they always contribute to sampling. self.assertAllClose(actual_sample_val, expected_sample_val, atol=1e-2)
Example #25
Source File: normalizer.py From lirpg with MIT License | 5 votes |
def __init__(self, size, std=1.): self.size = size self.mean = tf.zeros(self.size, tf.float32) self.std = std * tf.ones(self.size, tf.float32)
Example #26
Source File: common_attention.py From fine-lm with MIT License | 5 votes |
def make_2d_block_raster_mask(query_shape, memory_flange): """Creates a mask for 2d block raster scan. The query mask can look to the left, top left, top, and top right, but not to the right. Inside the query, we have the standard raster scan masking. Args: query_shape: A tuple of ints (query_height, query_width) memory_flange: A tuple of ints (memory_flange_height, memory_flange_width) Returns: A tensor of shape query_size, memory_size """ # mask inside the query block query_triangle = common_layers.ones_matrix_band_part( np.prod(query_shape), np.prod(query_shape), -1, 0) split_query_masks = tf.split(query_triangle, query_shape[0], axis=1) # adding mask for left and right mask_pieces = [ tf.concat( [ tf.ones([np.prod(query_shape), memory_flange[1]]), split_query_masks[i], tf.zeros([np.prod(query_shape), memory_flange[1]]) ], axis=1) for i in range(query_shape[0]) ] # adding mask for top final_mask = tf.concat( [ tf.ones([ np.prod(query_shape), (query_shape[1] + 2 * memory_flange[1]) * memory_flange[0] ]), tf.concat(mask_pieces, axis=1) ], axis=1) # 0.0 is visible location, 1.0 is masked. return 1. - final_mask
Example #27
Source File: discretization_test.py From fine-lm with MIT License | 5 votes |
def testSliceHiddenOnes(self): hidden_size = 60 block_dim = 20 num_blocks = 3 x = tf.ones(shape=[1, hidden_size], dtype=tf.float32) x_sliced = discretization.slice_hidden(x, hidden_size, num_blocks) with self.test_session() as sess: tf.global_variables_initializer().run() x_sliced_eval = sess.run(x_sliced) self.assertEqual(np.shape(x_sliced_eval), (1, num_blocks, block_dim)) self.assertTrue(np.all(x_sliced_eval == 1))
Example #28
Source File: discretization_test.py From fine-lm with MIT License | 5 votes |
def testIntToBitOnes(self): x_bit = tf.ones(shape=[1, 3], dtype=tf.float32) x_int = 7 * tf.ones(shape=[1], dtype=tf.int32) diff = discretization.int_to_bit(x_int, num_bits=3) - x_bit with self.test_session() as sess: tf.global_variables_initializer().run() d = sess.run(diff) self.assertTrue(np.all(d == 0))
Example #29
Source File: discretization_test.py From fine-lm with MIT License | 5 votes |
def testBitToIntOnes(self): x_bit = tf.ones(shape=[1, 3], dtype=tf.float32) x_int = 7 * tf.ones(shape=[1], dtype=tf.int32) diff = discretization.bit_to_int(x_bit, num_bits=3) - x_int with self.test_session() as sess: tf.global_variables_initializer().run() d = sess.run(diff) self.assertEqual(d, 0)
Example #30
Source File: pix2pix_test.py From DeepLab_v3 with MIT License | 5 votes |
def test_four_layers_negative_padding(self): batch_size = 2 input_size = 256 images = tf.ones((batch_size, input_size, input_size, 3)) with tf.contrib.framework.arg_scope(pix2pix.pix2pix_arg_scope()): with self.assertRaises(ValueError): pix2pix.pix2pix_discriminator( images, num_filters=[64, 128, 256, 512], padding=-1)