我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.size()。
def decode(self, data, items): decoded_items = {} # Split tokens tokens = tf.string_split([data], delimiter=self.delimiter).values # Optionally prepend a special token if self.prepend_token is not None: tokens = tf.concat([[self.prepend_token], tokens], 0) # Optionally append a special token if self.append_token is not None: tokens = tf.concat([tokens, [self.append_token]], 0) decoded_items[self.length_feature_name] = tf.size(tokens) decoded_items[self.tokens_feature_name] = tokens return [decoded_items[_] for _ in items]
def image_reading(path: str, resized_size: Tuple[int, int]=None, data_augmentation: bool=False, padding: bool=False) -> Tuple[tf.Tensor, tf.Tensor]: # Read image image_content = tf.read_file(path, name='image_reader') image = tf.cond(tf.equal(tf.string_split([path], '.').values[1], tf.constant('jpg', dtype=tf.string)), true_fn=lambda: tf.image.decode_jpeg(image_content, channels=1, try_recover_truncated=True), # TODO channels = 3 ? false_fn=lambda: tf.image.decode_png(image_content, channels=1), name='image_decoding') # Data augmentation if data_augmentation: image = augment_data(image) # Padding if padding: with tf.name_scope('padding'): image, img_width = padding_inputs_width(image, resized_size, increment=CONST.DIMENSION_REDUCTION_W_POOLING) # Resize else: image = tf.image.resize_images(image, size=resized_size) img_width = tf.shape(image)[1] with tf.control_dependencies([tf.assert_equal(image.shape[:2], resized_size)]): return image, img_width
def random_rotation(img: tf.Tensor, max_rotation: float=0.1, crop: bool=True) -> tf.Tensor: # from SeguinBe with tf.name_scope('RandomRotation'): rotation = tf.random_uniform([], -max_rotation, max_rotation) rotated_image = tf.contrib.image.rotate(img, rotation, interpolation='BILINEAR') if crop: rotation = tf.abs(rotation) original_shape = tf.shape(rotated_image)[:2] h, w = original_shape[0], original_shape[1] # see https://stackoverflow.com/questions/16702966/rotate-image-and-crop-out-black-borders for formulae old_l, old_s = tf.cond(h > w, lambda: [h, w], lambda: [w, h]) old_l, old_s = tf.cast(old_l, tf.float32), tf.cast(old_s, tf.float32) new_l = (old_l * tf.cos(rotation) - old_s * tf.sin(rotation)) / tf.cos(2*rotation) new_s = (old_s - tf.sin(rotation) * new_l) / tf.cos(rotation) new_h, new_w = tf.cond(h > w, lambda: [new_l, new_s], lambda: [new_s, new_l]) new_h, new_w = tf.cast(new_h, tf.int32), tf.cast(new_w, tf.int32) bb_begin = tf.cast(tf.ceil((h-new_h)/2), tf.int32), tf.cast(tf.ceil((w-new_w)/2), tf.int32) rotated_image_crop = rotated_image[bb_begin[0]:h - bb_begin[0], bb_begin[1]:w - bb_begin[1], :] # If crop removes the entire image, keep the original image rotated_image = tf.cond(tf.equal(tf.size(rotated_image_crop), 0), true_fn=lambda: img, false_fn=lambda: rotated_image_crop) return rotated_image
def instantiate_batch(self, inputs): """Instantiate the TensorFlow ops for running this loom Op. Args: inputs: The types of the inputs should match up with the types of the input_type_shapes. The shapes of the inputs should also match with the shapes of the TypeShapes with one additional dimension at the start for the batch size. Returns: A list of TensorFlow objects which will contain tensors whose dimensions match up with those of the corresponding output_type_shapes (with an additional dimension at the start for the batch size as before.) """ _ = inputs raise NotImplementedError( 'LoomOp needs a definition for instantiate_batch.')
def rotate_crop(img, rotation, crop=True, interpolation='NEAREST'): with tf.name_scope('RotateCrop'): rotated_image = tf_rotate(img, rotation, interpolation) if crop: rotation = tf.abs(rotation) original_shape = tf.shape(rotated_image)[:2] h, w = original_shape[0], original_shape[1] # see https://stackoverflow.com/questions/16702966/rotate-image-and-crop-out-black-borders for formulae old_l, old_s = tf.cond(h > w, lambda: [h, w], lambda: [w, h]) old_l, old_s = tf.cast(old_l, tf.float32), tf.cast(old_s, tf.float32) new_l = (old_l * tf.cos(rotation) - old_s * tf.sin(rotation)) / tf.cos(2 * rotation) new_s = (old_s - tf.sin(rotation) * new_l) / tf.cos(rotation) new_h, new_w = tf.cond(h > w, lambda: [new_l, new_s], lambda: [new_s, new_l]) new_h, new_w = tf.cast(new_h, tf.int32), tf.cast(new_w, tf.int32) bb_begin = tf.cast(tf.ceil((h - new_h) / 2), tf.int32), tf.cast(tf.ceil((w - new_w) / 2), tf.int32) rotated_image_crop = rotated_image[bb_begin[0]:h - bb_begin[0], bb_begin[1]:w - bb_begin[1], :] # If crop removes the entire image, keep the original image rotated_image = tf.cond(tf.equal(tf.size(rotated_image_crop), 0), true_fn=lambda: img, false_fn=lambda: rotated_image_crop) return rotated_image
def set_parameters(epochs, minibatch, iterations, device_id): """ iterations means the number of iterations in each epoch """ tf.app.flags.DEFINE_integer('batch_size', minibatch, """Batch size.""") #tf.app.flags.DEFINE_integer('num_batches', 500, tf.app.flags.DEFINE_integer('num_batches', iterations*epochs, """Number of batches to run.""") tf.app.flags.DEFINE_boolean('forward_only', False, """Only run the forward pass.""") tf.app.flags.DEFINE_boolean('forward_backward_only', True, """Only run the forward-forward pass.""") tf.app.flags.DEFINE_string('data_format', 'NHWC', """The data format for Convnet operations. Can be either NHWC or NCHW. """) global device_str if int(device_id) >= 0: device_str = '/gpu:%d'%int(device_id) else: # cpus device_str = '/cpu:0'
def _setup_classification_predictions_and_loss(self): self.inputs, self.target = self.input_queue.dequeue() self.num_batch_elems = tf.size(self.target) self.inputs = tf.reshape(self.inputs, self.input_shape) self.training_end_points = self.model(is_training=True, reuse=None, inputs=self.inputs) training_logits, self.training_predictions = self.training_end_points['logits'], self.training_end_points[ 'predictions'] self.validation_end_points = self.model(is_training=False, reuse=True, inputs=self.inputs) validation_logits, self.validation_predictions = self.validation_end_points['logits'], \ self.validation_end_points[ 'predictions'] with tf.name_scope('loss'): training_loss = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits( logits=training_logits, labels=self.target)) self.validation_loss = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits( logits=validation_logits, labels=self.target)) l2_loss = tf.add_n(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)) self.regularized_training_loss = training_loss + l2_loss * self.cnf.get('l2_reg', 0.0)
def _setup_regression_predictions_and_loss(self): self.inputs, self.target = self.input_queue.dequeue() self.num_batch_elems = tf.size(self.target) self.inputs = tf.reshape(self.inputs, self.input_shape) self.training_end_points = self.model(is_training=True, reuse=None, inputs=self.inputs) self.training_predictions = self.training_end_points['predictions'] self.validation_end_points = self.model(is_training=False, reuse=True, inputs=self.inputs) self.validation_predictions = self.validation_end_points['predictions'] with tf.name_scope('loss'): training_loss = tf.reduce_mean( tf.square(tf.subtract(self.training_predictions, self.target))) self.validation_loss = tf.reduce_mean( tf.square(tf.subtract(self.validation_predictions, self.target))) l2_loss = tf.add_n(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)) self.regularized_training_loss = training_loss + l2_loss * self.cnf.get('l2_reg', 0.0)
def get_gt_attn(y_gt, filter_height, filter_width, padding_ratio=0.0, center_shift_ratio=0.0, min_padding=10.0): """Get groundtruth attention box given segmentation.""" top_left, bot_right, box = get_gt_box( y_gt, padding_ratio=padding_ratio, center_shift_ratio=center_shift_ratio, min_padding=min_padding) ctr, size = get_box_ctr_size(top_left, bot_right) # lg_var = tf.zeros(tf.shape(ctr)) + 1.0 lg_var = get_normalized_var(size, filter_height, filter_width) lg_gamma = get_normalized_gamma(size, filter_height, filter_width) return ctr, size, lg_var, lg_gamma, box, top_left, bot_right
def ptb_producer(raw_data, batch_size, num_steps, name=None): with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]): raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32) data_len = tf.size(raw_data) batch_len = data_len // batch_size data = tf.reshape(raw_data[0 : batch_size * batch_len], [batch_size, batch_len]) epoch_size = (batch_len - 1) // num_steps epoch_size = tf.identity(epoch_size, name="epoch_size") i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue() x = tf.strided_slice(data, [0, i * num_steps], [batch_size, (i + 1) * num_steps], #tf.ones_like([0, i * num_steps])) [1,1]) x.set_shape([batch_size, num_steps]) y = tf.strided_slice(data, [0, i * num_steps + 1], [batch_size, (i + 1) * num_steps + 1], #tf.ones_like([0, i * num_steps])) [1,1]) y.set_shape([batch_size, num_steps]) return x, y
def lookup_last_idx(a, inds, name=None): """ Looks up indices in a. e.g. a[[1, 2, 3]] = [a[1], a[2], a[3]] a is a d1 x d2 ... dn tensor inds is a d1 x d2 ... d(n-1) tensor of integers returns the tensor out[i_1,...,i_{n-1}] = a[i_1,...,i_{n-1}, inds[i_1,...,i_{n-1}]] """ with tf.op_scope([a, inds], name, 'lookup_last_idx') as scope: a = tf.convert_to_tensor(a, name='a') inds = tf.convert_to_tensor(inds, name='inds') # Flatten the arrays ashape, indsshape = tf.shape(a), tf.shape(inds) aflat, indsflat = tf.reshape(a, [-1]), tf.reshape(inds, [-1]) # Compute the indices corresponding to inds in the flattened array # TODO Causes UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. delta = tf.gather(ashape, tf.size(ashape) - 1) # i.e. delta = ashape[-1], aflatinds = tf.range(0, limit=tf.size(a), delta=delta) + indsflat # Look up the desired elements in the flattened array, and reshape # to the original shape return tf.reshape(tf.gather(aflat, aflatinds), indsshape, name=scope)
def decode(self, sess): """Decode a batch. Args: sess: tensorflow session to use. Returns: A tuple consiting of outputs, infer_summary. outputs: of size [batch_size, time] """ _, infer_summary, _, sample_words = self.infer(sess) # make sure outputs is of shape [batch_size, time] or [beam_width, # batch_size, time] when using beam search. if self.time_major: sample_words = sample_words.transpose() elif sample_words.ndim == 3: # beam search output in [batch_size, # time, beam_width] shape. sample_words = sample_words.transpose([2, 0, 1]) return sample_words, infer_summary
def bbox_to_mask(bbox, region_size, output_size, dtype=tf.float32): """Creates a binary mask of size `region_size` where rectangle given by `bbox` is filled with ones and the rest is zeros. Finally, the binary mask is resized to `output_size` with bilinear interpolation. :param bbox: tensor of shape (..., 4) :param region_size: tensor of shape (..., 2) :param output_size: 2-tuple of ints :param dtype: tf.dtype :return: a tensor of shape = (..., output_size) """ shape = tf.concat(axis=0, values=(tf.shape(bbox)[:-1], output_size)) bbox = tf.reshape(bbox, (-1, 4)) region_size = tf.reshape(region_size, (-1, 2)) def create_mask(args): yy, region_size = args return _bbox_to_mask_fixed_size(yy, region_size, output_size, dtype) mask = tf.map_fn(create_mask, (bbox, region_size), dtype=dtype) return tf.reshape(mask, shape)
def batch_producer(raw_data, batch_size, num_steps): raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32) data_len = tf.size(raw_data) batch_len = data_len // batch_size data = tf.reshape(raw_data[0: batch_size * batch_len], [batch_size, batch_len]) epoch_size = (batch_len - 1) // num_steps i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue() x = data[:, i * num_steps:(i + 1) * num_steps] x.set_shape([batch_size, num_steps]) y = data[:, i * num_steps + 1: (i + 1) * num_steps + 1] y.set_shape([batch_size, num_steps]) return x, y
def _grad_sparsity(self): """Gradient sparsity.""" # If the sparse minibatch gradient has 10 percent of its entries # non-zero, its sparsity is 0.1. # The norm of dense gradient averaged from full dataset # are roughly estimated norm of minibatch # sparse gradient norm * sqrt(sparsity) # An extension maybe only correct the sparse blob. non_zero_cnt = tf.add_n([tf.count_nonzero(g) for g in self._grad]) all_entry_cnt = tf.add_n([tf.size(g) for g in self._grad]) self._sparsity = tf.cast(non_zero_cnt, self._grad[0].dtype) self._sparsity /= tf.cast(all_entry_cnt, self._grad[0].dtype) avg_op = self._moving_averager.apply([self._sparsity, ]) with tf.control_dependencies([avg_op]): self._sparsity_avg = self._moving_averager.average(self._sparsity) return avg_op
def _init_clusters_random(self): """Does random initialization of clusters. Returns: Tensor of randomly initialized clusters. """ num_data = tf.add_n([tf.shape(inp)[0] for inp in self._inputs]) # Note that for mini-batch k-means, we should ensure that the batch size of # data used during initialization is sufficiently large to avoid duplicated # clusters. with tf.control_dependencies( [tf.assert_less_equal(self._num_clusters, num_data)]): indices = tf.random_uniform(tf.reshape(self._num_clusters, [-1]), minval=0, maxval=tf.cast(num_data, tf.int64), seed=self._random_seed, dtype=tf.int64) clusters_init = embedding_lookup(self._inputs, indices, partition_strategy='div') return clusters_init
def testDensePartialFlatten(self): """Test `_inner_flatten` on `Tensor`s.""" shape = [2, 3, 4, 5, 6] np.random.seed(5446) inputs = np.random.randint(0, 100, size=shape) for new_rank in [1, 2, 3, 4, 5]: expected_new_shape = (shape[:new_rank - 1] + [np.prod(shape[new_rank - 1:])]) expected_flattened = np.reshape(inputs, expected_new_shape) flattened_t = _layers._inner_flatten(inputs, new_rank) static_shape = flattened_t.get_shape().as_list() self.assertEqual(static_shape, expected_new_shape) with self.test_session() as sess: flattened = sess.run(flattened_t) np.testing.assert_array_equal(expected_flattened, flattened)
def decode(self, sess): """Decode a batch. Args: sess: tensorflow session to use. Returns: A tuple consiting of outputs, infer_summary. outputs: of size [batch_size, time] """ _, infer_summary, _, sample_words = self.infer(sess) # make sure outputs is of shape [batch_size, time] if self.time_major: sample_words = sample_words.transpose() return sample_words, infer_summary
def train_model(self, data, dropout_prob, filter_sizes, weights_conv_i, biases_conv_i, sequence_length, num_filters_total, weight_output, bias_output): pooled_outputs = [] # lookup table for i, filter_size in enumerate(filter_sizes): # convolution layer with different filter size conv = tf.nn.conv2d(data, weights_conv_i, strides=[1, 1, 1, 1], padding="VALID") # non-linearitye h = tf.nn.relu(tf.nn.bias_add(conv, biases_conv_i)) pooled = tf.nn.max_pool(h, ksize=[1, sequence_length - filter_size + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID') pooled_outputs.append(pooled) h_pool = tf.concat(3, pooled_outputs) h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total]) h_drop = tf.nn.dropout(h_pool_flat, dropout_prob) scores = tf.nn.xw_plus_b(h_drop, weight_output, bias_output) return scores
def _build_graph(self): def sq_dist(mat1, mat2): return 0.5*tf.reduce_sum(tf.square(mat1 - mat2)) def gram(mat1): tmp = tf.reshape(mat1, (-1, mat1.shape[-1])) size = tf.size(tmp, out_type = tf.int64) return tf.matmul(tmp, tmp, transpose_a = True)/tf.cast(size, tf.float32) def gram_dist(mat1, mat2): return 0.25*sq_dist(gram(mat1), gram(mat2)) _, self.target_layers = nets.vgg.vgg_19(tf.expand_dims(self.target - tf.reshape(tf.constant(self.VGG_MEAN), [1, 1, 3]), \ axis = 0), is_training = False, spatial_squeeze = False) self.loss1 = tf.reduce_sum([sq_dist(tf.constant(self.content_map[layer_name]), \ self.target_layers[layer_name]) for layer_name in self.content_map]) self.loss2 = tf.reduce_mean([gram_dist(tf.constant(self.style_map[layer_name]), \ self.target_layers[layer_name]) for layer_name in self.style_map]) self.loss = 1e-4*self.loss1 + self.loss2
def select_dim_value(x, indices, name=None): with tf.name_scope(name, "select-dim-value", values=[x, indices]): # x.shape = (rest..., dims) rest = tf.shape(x)[:-1] dims = tf.shape(x)[-1] size = tf.size(indices, out_type=indices.dtype) # reshape to (size, dims) t = tf.reshape(x, shape=[-1, dims]) # then index as ([1,2,3,...,size], indices.ravel()) nd_indices = tf.stack([ tf.range(0, size, dtype=indices.dtype), tf.reshape(indices, shape=[-1]) ], axis=1) t = tf.gather_nd(t, indices=nd_indices) # reshape back to (rest...) t = tf.reshape(t, rest) t.set_shape(x.get_shape()[:-1]) return t
def tv_loss(bottom): """ the tv loss :param bottom: :return: """ shape = tf.shape(bottom) height = shape[1] width = shape[2] y = tf.slice(bottom, [0, 0, 0, 0], tf.stack([-1, height - 1, -1, -1])) - tf.slice(bottom, [0, 1, 0, 0], [-1, -1, -1, -1]) x = tf.slice(bottom, [0, 0, 0, 0], tf.stack([-1, -1, width - 1, -1])) - tf.slice(bottom, [0, 0, 1, 0], [-1, -1, -1, -1]) loss = tf.nn.l2_loss(x) / tf.to_float(tf.size(x)) + tf.nn.l2_loss(y) / tf.to_float(tf.size(y)) return loss
def __init__(self, cfg, data, name): self.steps = ((len(data) // cfg.batch_size) - 1) // cfg.num_steps with tf.name_scope(name, values=[data, cfg.batch_size, cfg.num_steps]): raw_data = tf.convert_to_tensor(data) data_len = tf.size(raw_data) batch_len = data_len // cfg.batch_size data = tf.reshape(raw_data[0: cfg.batch_size * batch_len], [cfg.batch_size, batch_len]) epoch_size = (batch_len - 1) // cfg.num_steps epoch_size = tf.identity(epoch_size, name="epoch_size") i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue() begin_x = [0, i * cfg.num_steps] self.inputs = tf.strided_slice( data, begin_x, [cfg.batch_size, (i + 1) * cfg.num_steps], tf.ones_like(begin_x)) self.inputs.set_shape([cfg.batch_size, cfg.num_steps]) begin_y = [0, i * cfg.num_steps + 1] self.targets = tf.strided_slice( data, begin_y, [cfg.batch_size, (i + 1) * cfg.num_steps + 1], tf.ones_like(begin_y)) self.targets.set_shape([cfg.batch_size, cfg.num_steps])
def _generate_relative_positions_embeddings(heads, length, depth, max_relative_position, name): """Generates tensor of size [heads, length, length, depth].""" with tf.variable_scope(name): relative_positions_matrix = _generate_relative_positions_matrix( length, max_relative_position) vocab_size = max_relative_position * 2 + 1 # Generates embedding for each relative position of dimension heads * depth. embeddings_table = tf.get_variable("embeddings", [vocab_size, heads * depth]) embeddings = tf.gather(embeddings_table, relative_positions_matrix) # Split embeddings per head. embeddings = tf.reshape(embeddings, [length, length, heads, depth]) # Transpose to shape [heads, length, length, depth]. embeddings = tf.transpose(embeddings, [2, 0, 1, 3]) return embeddings
def _grad_sparsity(self): """Gradient sparsity.""" # If the sparse minibatch gradient has 10 percent of its entries # non-zero, its sparsity is 0.1. # The norm of dense gradient averaged from full dataset # are roughly estimated norm of minibatch # sparse gradient norm * sqrt(sparsity) # An extension maybe only correct the sparse blob. non_zero_cnt = tf.add_n([tf.count_nonzero(g) for g in self._grad]) all_entry_cnt = tf.add_n([tf.size(g) for g in self._grad]) self._sparsity = tf.cast(non_zero_cnt, self._grad[0].dtype) self._sparsity /= tf.cast(all_entry_cnt, self._grad[0].dtype) avg_op = self._moving_averager.apply([self._sparsity,]) with tf.control_dependencies([avg_op]): self._sparsity_avg = self._moving_averager.average(self._sparsity) return avg_op
def unpool(net, mask, stride): assert mask is not None with tf.name_scope('UnPool2D'): ksize = [1, stride, stride, 1] input_shape = net.get_shape().as_list() # calculation new shape output_shape = (input_shape[0], input_shape[1] * ksize[1], input_shape[2] * ksize[2], input_shape[3]) # calculation indices for batch, height, width and feature maps one_like_mask = tf.ones_like(mask) batch_range = tf.reshape(tf.range(output_shape[0], dtype=tf.int64), shape=[input_shape[0], 1, 1, 1]) b = one_like_mask * batch_range y = mask // (output_shape[2] * output_shape[3]) x = mask % (output_shape[2] * output_shape[3]) // output_shape[3] feature_range = tf.range(output_shape[3], dtype=tf.int64) f = one_like_mask * feature_range # transpose indices & reshape update values to one dimension updates_size = tf.size(net) indices = tf.transpose(tf.reshape(tf.stack([b, y, x, f]), [4, updates_size])) values = tf.reshape(net, [updates_size]) ret = tf.scatter_nd(indices, values, output_shape) return ret
def upsample(net, stride, mode='ZEROS'): """ Imitate reverse operation of Max-Pooling by either placing original max values into a fixed postion of upsampled cell: [0.9] =>[[.9, 0], (stride=2) [ 0, 0]] or copying the value into each cell: [0.9] =>[[.9, .9], (stride=2) [ .9, .9]] :param net: 4D input tensor with [batch_size, width, heights, channels] axis :param stride: :param mode: string 'ZEROS' or 'COPY' indicating which value to use for undefined cells :return: 4D tensor of size [batch_size, width*stride, heights*stride, channels] """ assert mode in ['COPY', 'ZEROS'] with tf.name_scope('Upsampling'): net = _upsample_along_axis(net, 2, stride, mode=mode) net = _upsample_along_axis(net, 1, stride, mode=mode) return net
def unpool(net, mask, stride=2): assert mask is not None with tf.name_scope('UnPool2D'): ksize = [1, stride, stride, 1] input_shape = net.get_shape().as_list() # calculation new shape output_shape = (input_shape[0], input_shape[1] * ksize[1], input_shape[2] * ksize[2], input_shape[3]) # calculation indices for batch, height, width and feature maps one_like_mask = tf.ones_like(mask) batch_range = tf.reshape(tf.range(output_shape[0], dtype=tf.int64), shape=[input_shape[0], 1, 1, 1]) b = one_like_mask * batch_range y = mask // (output_shape[2] * output_shape[3]) x = mask % (output_shape[2] * output_shape[3]) // output_shape[3] feature_range = tf.range(output_shape[3], dtype=tf.int64) f = one_like_mask * feature_range # transpose indices & reshape update values to one dimension updates_size = tf.size(net) indices = tf.transpose(tf.reshape(tf.stack([b, y, x, f]), [4, updates_size])) values = tf.reshape(net, [updates_size]) ret = tf.scatter_nd(indices, values, output_shape) return ret
def set_parameters(epochs, minibatch, iterations, device_id): """ iterations means the number of iterations in each epoch """ tf.app.flags.DEFINE_integer('batch_size', minibatch, """Batch size.""") #tf.app.flags.DEFINE_integer('num_batches', 500, tf.app.flags.DEFINE_integer('num_batches', iterations*epochs, """Number of batches to run.""") #tf.app.flags.DEFINE_boolean('forward_only', False, tf.app.flags.DEFINE_boolean('forward_only', False, """Only run the forward pass.""") tf.app.flags.DEFINE_boolean('forward_backward_only', True, """Only run the forward-forward pass.""") tf.app.flags.DEFINE_string('data_format', 'NHWC', """The data format for Convnet operations. Can be either NHWC or NCHW. """) global device_str if int(device_id) >= 0: device_str = '/gpu:%d'%int(device_id) else: # cpus device_str = '/cpu:0'
def __init__(self, training, cell, embedding, start_tokens, end_token, initial_state, beam_width, output_layer=None, gold_sequence=None, gold_sequence_length=None): self._training = training self._cell = cell self._output_layer = output_layer self._embedding_fn = lambda ids: tf.nn.embedding_lookup(embedding, ids) self._output_size = output_layer.units if output_layer is not None else self._output.output_size self._batch_size = tf.size(start_tokens) self._beam_width = beam_width self._tiled_initial_cell_state = nest.map_structure(self._maybe_split_batch_beams, initial_state, self._cell.state_size) self._start_tokens = start_tokens self._tiled_start_tokens = self._maybe_tile_batch(start_tokens) self._end_token = end_token self._original_gold_sequence = gold_sequence self._gold_sequence = gold_sequence self._gold_sequence_length = gold_sequence_length if training: assert self._gold_sequence is not None assert self._gold_sequence_length is not None self._max_time = int(self._gold_sequence.shape[1]) # transpose gold sequence to be time major and make it into a TensorArray self._gold_sequence = tf.TensorArray(dtype=tf.int32, size=self._max_time) self._gold_sequence = self._gold_sequence.unstack(tf.transpose(gold_sequence, [1, 0]))
def _split_batch_beams(self, t, s): """Splits the tensor from a batch by beams into a batch of beams. More exactly, t is a tensor of dimension [batch_size*beam_width, s]. We reshape this into [batch_size, beam_width, s] Args: t: Tensor of dimension [batch_size*beam_width, s]. s: (Possibly known) depth shape. Returns: A reshaped version of t with dimension [batch_size, beam_width, s]. Raises: ValueError: If, after reshaping, the new tensor is not shaped `[batch_size, beam_width, s]` (assuming batch_size and beam_width are known statically). """ t_shape = tf.shape(t) reshaped = tf.reshape(t, tf.concat(([self._batch_size, self._beam_width], t_shape[1:]), axis=0)) reshaped.set_shape(tf.TensorShape([None, self._beam_width]).concatenate(t.shape[1:])) expected_reshaped_shape = tf.TensorShape([None, self._beam_width]).concatenate(s) if not reshaped.shape.is_compatible_with(expected_reshaped_shape): raise ValueError("Unexpected behavior when reshaping between beam width " "and batch size. The reshaped tensor has shape: %s. " "We expected it to have shape " "(batch_size, beam_width, depth) == %s. Perhaps you " "forgot to create a zero_state with " "batch_size=encoder_batch_size * beam_width?" % (reshaped.shape, expected_reshaped_shape)) return reshaped
def _maybe_split_batch_beams(self, t, s): """Maybe splits the tensor from a batch by beams into a batch of beams. We do this so that we can use nest and not run into problems with shapes. Args: t: Tensor of dimension [batch_size*beam_width, s] s: Tensor, Python int, or TensorShape. Returns: Either a reshaped version of t with dimension [batch_size, beam_width, s] if t's first dimension is of size batch_size*beam_width or t if not. Raises: TypeError: If t is an instance of TensorArray. ValueError: If the rank of t is not statically known. """ return self._split_batch_beams(t, s) if t.shape.ndims >= 1 else t
def _maybe_tensor_gather_helper(gather_indices, gather_from, batch_size, range_size, gather_shape): """Maybe applies _tensor_gather_helper. This applies _tensor_gather_helper when the gather_from dims is at least as big as the length of gather_shape. This is used in conjunction with nest so that we don't apply _tensor_gather_helper to inapplicable values like scalars. Args: gather_indices: The tensor indices that we use to gather. gather_from: The tensor that we are gathering from. batch_size: The batch size. range_size: The number of values in each range. Likely equal to beam_width. gather_shape: What we should reshape gather_from to in order to preserve the correct values. An example is when gather_from is the attention from an AttentionWrapperState with shape [batch_size, beam_width, attention_size]. There, we want to preserve the attention_size elements, so gather_shape is [batch_size * beam_width, -1]. Then, upon reshape, we still have the attention_size as desired. Returns: output: Gathered tensor of shape tf.shape(gather_from)[:1+len(gather_shape)] or the original tensor if its dimensions are too small. """ if gather_from.shape.ndims >= len(gather_shape): return _tensor_gather_helper( gather_indices=gather_indices, gather_from=gather_from, batch_size=batch_size, range_size=range_size, gather_shape=gather_shape) else: return gather_from
def _tensor_gather_helper(gather_indices, gather_from, batch_size, range_size, gather_shape): """Helper for gathering the right indices from the tensor. This works by reshaping gather_from to gather_shape (e.g. [-1]) and then gathering from that according to the gather_indices, which are offset by the right amounts in order to preserve the batch order. Args: gather_indices: The tensor indices that we use to gather. gather_from: The tensor that we are gathering from. batch_size: The input batch size. range_size: The number of values in each range. Likely equal to beam_width. gather_shape: What we should reshape gather_from to in order to preserve the correct values. An example is when gather_from is the attention from an AttentionWrapperState with shape [batch_size, beam_width, attention_size]. There, we want to preserve the attention_size elements, so gather_shape is [batch_size * beam_width, -1]. Then, upon reshape, we still have the attention_size as desired. Returns: output: Gathered tensor of shape tf.shape(gather_from)[:1+len(gather_shape)] """ range_ = tf.expand_dims(tf.range(batch_size) * range_size, 1) gather_indices = tf.reshape(gather_indices + range_, [-1]) output = tf.gather(tf.reshape(gather_from, gather_shape), gather_indices) final_shape = tf.shape(gather_from)[:1 + len(gather_shape)] final_static_shape = (tf.TensorShape([None]).concatenate(gather_from.shape[1:1 + len(gather_shape)])) output = tf.reshape(output, final_shape) output.set_shape(final_static_shape) return output
def ptb_raw_data(data_path=None): """Load PTB raw data from data directory "data_path". Reads PTB text files, converts strings to integer ids, and performs mini-batching of the inputs. The PTB dataset comes from Tomas Mikolov's webpage: http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz Args: data_path: string path to the directory where simple-examples.tgz has been extracted. Returns: tuple (train_data, valid_data, test_data, vocabulary) where each of the data objects can be passed to PTBIterator. """ train_path = os.path.join(data_path, 'train') valid_path = os.path.join(data_path, 'valid') test_path = os.path.join(data_path, 'test') word_to_id = _build_vocab(train_path) print('vocabulary size:', len(word_to_id)) train_data = _file_to_word_ids(train_path, word_to_id) valid_data = _file_to_word_ids(valid_path, word_to_id) test_data = _file_to_word_ids(test_path, word_to_id) vocabulary = len(word_to_id) print('data loaded') return train_data, valid_data, test_data, vocabulary
def ptb_producer(raw_data, batch_size, num_steps, name=None): """Iterate on the raw PTB data. This chunks up raw_data into batches of examples and returns Tensors that are drawn from these batches. Args: raw_data: one of the raw data outputs from ptb_raw_data. batch_size: int, the batch size. num_steps: int, the number of unrolls. name: the name of this operation (optional). Returns: A pair of Tensors, each shaped [batch_size, num_steps]. The second element of the tuple is the same data time-shifted to the right by one. Raises: tf.errors.InvalidArgumentError: if batch_size or num_steps are too high. """ with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]): raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32) data_len = tf.size(raw_data) batch_len = data_len // batch_size data = tf.reshape(raw_data[0 : batch_size * batch_len], [batch_size, batch_len]) epoch_size = (batch_len - 1) // num_steps assertion = tf.assert_positive( epoch_size, message="epoch_size == 0, decrease batch_size or num_steps") with tf.control_dependencies([assertion]): epoch_size = tf.identity(epoch_size, name="epoch_size") i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue() x = tf.slice(data, [0, i * num_steps], [batch_size, num_steps]) y = tf.slice(data, [0, i * num_steps + 1], [batch_size, num_steps]) return x, y
def random_padding(image: tf.Tensor, max_pad_w: int=5, max_pad_h: int=10) -> tf.Tensor: w_pad = list(np.random.randint(0, max_pad_w, size=[2])) h_pad = list(np.random.randint(0, max_pad_h, size=[2])) paddings = [h_pad, w_pad, [0, 0]] return tf.pad(image, paddings, mode='REFLECT', name='random_padding')
def preprocess_image_for_prediction(fixed_height: int=32, min_width: int=8): """ Input function to use when exporting the model for making predictions (see estimator.export_savedmodel) :param fixed_height: height of the input image after resizing :param min_width: minimum width of image after resizing :return: """ def serving_input_fn(): # define placeholder for input image image = tf.placeholder(dtype=tf.float32, shape=[None, None, 1]) shape = tf.shape(image) # Assert shape is h x w x c with c = 1 ratio = tf.divide(shape[1], shape[0]) increment = CONST.DIMENSION_REDUCTION_W_POOLING new_width = tf.cast(tf.round((ratio * fixed_height) / increment) * increment, tf.int32) resized_image = tf.cond(new_width < tf.constant(min_width, dtype=tf.int32), true_fn=lambda: tf.image.resize_images(image, size=(fixed_height, min_width)), false_fn=lambda: tf.image.resize_images(image, size=(fixed_height, new_width)) ) # Features to serve features = {'images': resized_image[None], # cast to 1 x h x w x c 'images_widths': new_width[None] # cast to tensor } # Inputs received receiver_inputs = {'images': image} return tf.estimator.export.ServingInputReceiver(features, receiver_inputs) return serving_input_fn
def create_seed(filename, sample_rate, quantization_channels, window_size): audio, _ = librosa.load(filename, sr=sample_rate, mono=True) quantized = mu_law_encode(audio, quantization_channels) cut_index = tf.cond(tf.size(quantized) < tf.constant(window_size), lambda: tf.size(quantized), lambda: tf.constant(window_size)) return quantized[:cut_index]
def loss(logits, labels): batch_size = tf.size(labels) labels = tf.expand_dims(labels, 1) indices = tf.expand_dims(tf.range(0, batch_size, 1), 1) concated = tf.concat(axis=1, values=[indices, labels]) onehot_labels = tf.sparse_to_dense( concated, tf.stack([batch_size, 1000]), 1.0, 0.0) cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=onehot_labels, name='xentropy') loss = tf.reduce_mean(cross_entropy, name='xentropy_mean') return loss
def loss(logits, labels): batch_size = tf.size(labels) labels = tf.expand_dims(labels, 1) indices = tf.expand_dims(tf.range(0, batch_size, 1), 1) concated = tf.concat(1, [indices, labels]) onehot_labels = tf.sparse_to_dense( concated, tf.pack([batch_size, 1000]), 1.0, 0.0) cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, onehot_labels, name='xentropy') loss = tf.reduce_mean(cross_entropy, name='xentropy_mean') return loss