我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.Tensor()。
def inference(self): """main computation graph here: 1. embeddding layer, 2.Bi-LSTM layer, 3.concat, 4.FC layer 5.softmax """ #1.get emebedding of words in the sentence self.embedded_words = tf.nn.embedding_lookup(self.Embedding,self.input_x) #shape:[None,sentence_length,embed_size] #2. Bi-lstm layer # define lstm cess:get lstm cell output lstm_fw_cell=rnn.BasicLSTMCell(self.hidden_size) #forward direction cell lstm_bw_cell=rnn.BasicLSTMCell(self.hidden_size) #backward direction cell if self.dropout_keep_prob is not None: lstm_fw_cell=rnn.DropoutWrapper(lstm_fw_cell,output_keep_prob=self.dropout_keep_prob) lstm_bw_cell=rnn.DropoutWrapper(lstm_bw_cell,output_keep_prob=self.dropout_keep_prob) # bidirectional_dynamic_rnn: input: [batch_size, max_time, input_size] # output: A tuple (outputs, output_states) # where:outputs: A tuple (output_fw, output_bw) containing the forward and the backward rnn output `Tensor`. outputs,_=tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell,lstm_bw_cell,self.embedded_words,dtype=tf.float32) #[batch_size,sequence_length,hidden_size] #creates a dynamic bidirectional recurrent neural network print("outputs:===>",outputs) #outputs:(<tf.Tensor 'bidirectional_rnn/fw/fw/transpose:0' shape=(?, 5, 100) dtype=float32>, <tf.Tensor 'ReverseV2:0' shape=(?, 5, 100) dtype=float32>)) #3. concat output output_rnn=tf.concat(outputs,axis=2) #[batch_size,sequence_length,hidden_size*2] self.output_rnn_last=tf.reduce_mean(output_rnn,axis=1) #[batch_size,hidden_size*2] #output_rnn_last=output_rnn[:,-1,:] ##[batch_size,hidden_size*2] #TODO print("output_rnn_last:", self.output_rnn_last) # <tf.Tensor 'strided_slice:0' shape=(?, 200) dtype=float32> #4. logits(use linear layer) with tf.name_scope("output"): #inputs: A `Tensor` of shape `[batch_size, dim]`. The forward activations of the input network. logits = tf.matmul(self.output_rnn_last, self.W_projection) + self.b_projection # [batch_size,num_classes] return logits
def masked_apply(tensor, op, mask): """Applies `op` to tensor only at locations indicated by `mask` and sets the rest to zero. Similar to doing `tensor = tf.where(mask, op(tensor), tf.zeros_like(tensor))` but it behaves correctly when `op(tensor)` is NaN or inf while tf.where does not. :param tensor: tf.Tensor :param op: tf.Op :param mask: tf.Tensor with dtype == bool :return: tf.Tensor """ chosen = tf.boolean_mask(tensor, mask) applied = op(chosen) idx = tf.to_int32(tf.where(mask)) result = tf.scatter_nd(idx, applied, tf.shape(tensor)) return result
def feed_network(self,data,keep_prob,chunk_size,n_chunks,dynamic): # This code is copied from tflearn sequence_lengths = None if dynamic: sequence_lengths = net.calc_seqlenth(data if isinstance(data, tf.Tensor) else tf.stack(data)) batch_size = tf.shape(data)[0] weight_dropout = tf.nn.dropout(self._layer_weights, keep_prob) rnn_dropout = rnn.core_rnn_cell.DropoutWrapper(self._gru_cell,output_keep_prob=keep_prob) # Calculation Begin input_shape = data.get_shape().as_list() ndim = len(input_shape) axis = [1, 0] + list(range(2,ndim)) data = tf.transpose(data,(axis)) sequence = tf.unstack(data) outputs, states = rnn.static_rnn(rnn_dropout, sequence, dtype=tf.float32, sequence_length = sequence_lengths) if dynamic: outputs = tf.transpose(tf.stack(outputs), [1, 0, 2]) output = net.advanced_indexing_op(outputs, sequence_lengths) else: output = outputs[-1] output = tf.add(tf.matmul(output,weight_dropout), self._layer_biases) return output
def repeat(tensor: tf.Tensor, repeats: int, axis: int) -> tf.Tensor: """ Repeat elements of the input tensor in the specified axis ``repeats``-times. .. note:: Chaining of this op may produce TF warnings although the performance seems to be unaffected. :param tensor: TF tensor to be repeated :param repeats: number of repeats :param axis: axis to repeat :return: tensor with repeated elements """ shape = tensor.get_shape().as_list() dims = np.arange(len(tensor.shape)) prepare_perm = np.hstack(([axis], np.delete(dims, axis))) restore_perm = np.hstack((dims[1:axis+1], [0], dims[axis+1:])) indices = tf.cast(tf.floor(tf.range(0, shape[axis]*repeats)/tf.constant(repeats)), 'int32') shuffled = tf.transpose(tensor, prepare_perm) repeated = tf.gather(shuffled, indices) return tf.transpose(repeated, restore_perm)
def bin_stats(predictions: tf.Tensor, labels: tf.Tensor) -> Tuple[tf.Tensor, tf.Tensor, tf.Tensor]: """ Calculate f1, precision and recall from binary classification expected and predicted values. :param predictions: 2-d tensor (batch, predictions) of predicted 0/1 classes :param labels: 2-d tensor (batch, labels) of expected 0/1 classes :return: a tuple of batched (f1, precision and recall) values """ predictions = tf.cast(predictions, tf.int32) labels = tf.cast(labels, tf.int32) true_positives = tf.reduce_sum((predictions * labels), axis=1) false_positives = tf.reduce_sum(tf.cast(tf.greater(predictions, labels), tf.int32), axis=1) false_negatives = tf.reduce_sum(tf.cast(tf.greater(labels, predictions), tf.int32), axis=1) recall = true_positives / (true_positives + false_negatives) precision = true_positives / (true_positives + false_positives) f1_score = 2 / (1 / precision + 1 / recall) return f1_score, precision, recall
def bin_dice(predictions: tf.Tensor, labels: tf.Tensor) -> tf.Tensor: """ Calculate Sorensen–Dice coefficient from the given binary classification expected and predicted values. The coefficient is defined as :math:`2*|X \cup Y| / (|X| + |Y|)`. :param predictions: 2-d tensor (batch, predictions) of predicted 0/1 classes :param labels: 2-d tensor (batch, labels) of expected 0/1 classes :return: batched Sørensen–Dice coefficients """ predictions = tf.cast(predictions, tf.int32) labels = tf.cast(labels, tf.int32) true_positives = tf.reduce_sum((predictions * labels), axis=1) pred_positives = tf.reduce_sum(predictions, axis=1) label_positives = tf.reduce_sum(labels, axis=1) return 2 * true_positives / (pred_positives + label_positives)
def loss_nce(self,l2_lambda=0.0001): #0.0001-->0.001 """calculate loss using (NCE)cross entropy here""" # Compute the average NCE loss for the batch. # tf.nce_loss automatically draws a new sample of the negative labels each # time we evaluate the loss. if self.is_training: #training #labels=tf.reshape(self.input_y,[-1]) #[batch_size,1]------>[batch_size,] labels=tf.expand_dims(self.input_y,1) #[batch_size,]----->[batch_size,1] loss = tf.reduce_mean( #inputs: A `Tensor` of shape `[batch_size, dim]`. The forward activations of the input network. tf.nn.nce_loss(weights=tf.transpose(self.W_projection),#[hidden_size*2, num_classes]--->[num_classes,hidden_size*2]. nce_weights:A `Tensor` of shape `[num_classes, dim].O.K. biases=self.b_projection, #[label_size]. nce_biases:A `Tensor` of shape `[num_classes]`. labels=labels, #[batch_size,1]. train_labels, # A `Tensor` of type `int64` and shape `[batch_size,num_true]`. The target classes. inputs=self.output_rnn_last,# [batch_size,hidden_size*2] #A `Tensor` of shape `[batch_size, dim]`. The forward activations of the input network. num_sampled=self.num_sampled, #scalar. 100 num_classes=self.num_classes,partition_strategy="div")) #scalar. 1999 l2_losses = tf.add_n([tf.nn.l2_loss(v) for v in tf.trainable_variables() if 'bias' not in v.name]) * l2_lambda loss = loss + l2_losses return loss
def compute_exponential_averages(variables, decay): """Given a list of tensorflow scalar variables create ops corresponding to their exponential averages Parameters ---------- variables: [tf.Tensor] List of scalar tensors. Returns ------- averages: [tf.Tensor] List of scalar tensors corresponding to averages of al the `variables` (in order) apply_op: tf.runnable Op to be run to update the averages with current value of variables. """ averager = tf.train.ExponentialMovingAverage(decay=decay) apply_op = averager.apply(variables) return [averager.average(v) for v in variables], apply_op
def make_moving_average(name, value, init, decay, log=True): """Creates an exp-moving average of `value` and an update op, which is added to UPDATE_OPS collection. :param name: string, name of the created moving average tf.Variable :param value: tf.Tensor, the value to be averaged :param init: float, an initial value for the moving average :param decay: float between 0 and 1, exponential decay of the moving average :param log: bool, add a summary op if True :return: tf.Tensor, the moving average """ var = tf.get_variable(name, shape=value.get_shape(), initializer=tf.constant_initializer(init), trainable=False) update = moving_averages.assign_moving_average(var, value, decay, zero_debias=False) tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, update) if log: tf.summary.scalar(name, var) return var
def feed_network(self,data,keep_prob,chunk_size,n_chunks, dynamic): # This code is copied from tflearn sequence_lengths = None if dynamic: sequence_lengths = net.calc_seqlenth(data if isinstance(data, tf.Tensor) else tf.stack(data)) batch_size = tf.shape(data)[0] weight_dropout = tf.nn.dropout(self._layer_weights, keep_prob) rnn_dropout = rnn.core_rnn_cell.DropoutWrapper(self._lstm_cell,output_keep_prob=keep_prob) # Calculation Begin input_shape = data.get_shape().as_list() ndim = len(input_shape) axis = [1, 0] + list(range(2,ndim)) data = tf.transpose(data,(axis)) sequence = tf.unstack(data) outputs, states = rnn.static_rnn(rnn_dropout, sequence, dtype=tf.float32, sequence_length = sequence_lengths) if dynamic: outputs = tf.transpose(tf.stack(outputs), [1, 0, 2]) output = net.advanced_indexing_op(outputs, sequence_lengths) else: output = outputs[-1] output = tf.add(tf.matmul(output,weight_dropout), self._layer_biases) return output
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 create_training_output(self, shared_resources: SharedResources, training_input_tensors: Mapping[TensorPort, tf.Tensor]) \ -> Mapping[TensorPort, tf.Tensor]: """ This function needs to be implemented in order to define how the module produces tensors only used during training given tensors corresponding to the ones defined by `training_input_ports`, which might include tensors corresponding to ports defined by `output_ports`. This sub-graph should only be created during training. Args: shared_resources: contains resources shared by modules, such as hyper-parameters or vocabularies. training_input_tensors: a mapping from training input tensorports to tensors. Returns: mapping from defined training output ports to their tensors. """ raise NotImplementedError
def __init__(self, incoming, shape, **kwargs): super(ReshapeLayer, self).__init__(incoming, **kwargs) shape = tuple(shape) for s in shape: if isinstance(s, int): if s == 0 or s < - 1: raise ValueError("`shape` integers must be positive or -1") elif isinstance(s, list): if len(s) != 1 or not isinstance(s[0], int) or s[0] < 0: raise ValueError("`shape` input references must be " "single-element lists of int >= 0") elif isinstance(s, (tf.Tensor, tf.Variable)): # T.TensorVariable): raise NotImplementedError # if s.ndim != 0: # raise ValueError( # "A symbolic variable in a shape specification must be " # "a scalar, but had %i dimensions" % s.ndim) else: raise ValueError("`shape` must be a tuple of int and/or [int]") if sum(s == -1 for s in shape) > 1: raise ValueError("`shape` cannot contain multiple -1") self.shape = shape # try computing the output shape once as a sanity check self.get_output_shape_for(self.input_shape)
def apply_gradients(self, grads_and_vars, global_step=None, name=None): with tf.name_scope(name, self._name) as name: update_op = self._opt.apply_gradients( grads_and_vars, global_step=global_step) add_noise_ops = [] with tf.control_dependencies([update_op]): for grad, var in grads_and_vars: if grad is None: continue with tf.name_scope("sgld_noise_" + var.op.name): if isinstance(grad, tf.Tensor): add_noise_ops.append(self._noise_dense(var)) else: add_noise_ops.append(self._noise_sparse(grad, var)) ## running combined op return tf.group(*([update_op] + add_noise_ops), name=name)
def apply_gradients(self, grads_and_vars, global_step=None, name=None): with tf.name_scope(name, self._name) as name: update_op = self._opt.apply_gradients( grads_and_vars, global_step=global_step) add_noise_ops = [] with tf.control_dependencies([update_op]): for grad, var in grads_and_vars: if grad is None: continue with tf.name_scope("psgld_noise_" + var.op.name): if isinstance(grad, tf.Tensor): add_noise_ops.append(self._noise_dense(var)) else: add_noise_ops.append(self._noise_sparse(grad, var)) ## running combined op return tf.group(*([update_op] + add_noise_ops), name=name)
def is_same_dynamic_shape(x, y): """ Whether `x` and `y` has the same dynamic shape. :param x: A Tensor. :param y: A Tensor. :return: A scalar Tensor of `bool`. """ # There is a BUG of Tensorflow for not doing static shape inference # right in nested tf.cond()'s, so we are not comparing x and y's # shape directly but working with their concatenations. return tf.cond( tf.equal(tf.rank(x), tf.rank(y)), lambda: tf.reduce_all(tf.equal( tf.concat([tf.shape(x), tf.shape(y)], 0), tf.concat([tf.shape(y), tf.shape(x)], 0))), lambda: tf.convert_to_tensor(False, tf.bool))
def __init__(self, name, generator, output_types, output_shapes=None, min_queue_examples=0, shuffle_size=None, padded_shapes=None, padded_values=None): """ :param name: name of the dataset. :param generator generator: generator of elements in of the dataset :param output_types: list of output types of the generator :param output_shapes: output shapes of the generator :param int min_queue_examples: minimum number of examples to queue, this value should be proportional to the ram of the computer. By default is 0 :param int shuffle_size: size of the buffer for shuffling, this value should be proportional to the ram of the computer :param List[tf.Tensor] padded_shapes: shape for padding the batch :param tf.Tensor padded_values: values for the padding """ if not callable(generator): raise TypeError("`generator` must be callable.") self.name = name self.generator = generator self.output_types = output_types self.output_shapes = output_shapes self.min_queue_examples = min_queue_examples self.shuffle_size = shuffle_size self.padded_shapes = padded_shapes self.padded_values = padded_values
def __init__(self, name, data_files_pattern, dataset_class=TextLineDataset, min_queue_examples=0, shuffle_size=None, padded_shapes=None, padded_values=None): """ :param name: name of the dataset. :param str data_files_pattern: pattern of the data files :param Dataset dataset_class: class to create the dataset with the files. By default is TextLineDataset :param int min_queue_examples: minimum number of examples to queue, this value should be proportional to the ram of the computer. By default is 0 :param int shuffle_size: size of the buffer for shuffling, this value should be proportional to the ram of the computer :param List[tf.Tensor] padded_shapes: shape for padding the batch :param tf.Tensor padded_values: values for the padding """ self.name = name self.data_files_pattern = data_files_pattern self.dataset_class = dataset_class self.min_queue_examples = min_queue_examples self.shuffle_size = shuffle_size self.padded_shapes = padded_shapes self.padded_values = padded_values self._size = None
def optimize(self, loss, global_step, learning_rate_initial=TC_LEARNING_RATE_INITIAL, learning_rate_decay=TC_LEARNING_RATE_DECAY, learning_rate_decay_steps=TC_LEARNING_RATE_DECAY_STEPS): """ Creates a learning rate and an optimizer for the loss :param tf.Tensor loss: the tensor with the loss of the model :param tf.Tensor global_step: the global step for training :param int learning_rate_initial: the initial learning rate :param int learning_rate_decay: the decay of the learning rate :param int learning_rate_decay_steps: the number of steps to decay the learning rate :return (tf.Tensor, tf.Tensor): a tuple with the optimizer and the learning rate """ learning_rate = tf.train.exponential_decay(learning_rate_initial, global_step, learning_rate_decay_steps, learning_rate_decay, staircase=True, name='learning_rate') # optimizer optimizer = tf.train.RMSPropOptimizer(learning_rate) # optimizer = tf.train.GradientDescentOptimizer(learning_rate) # optimizer = tf.train.AdamOptimizer(learning_rate) optimizer = optimizer.minimize(loss, global_step=global_step) return optimizer, learning_rate
def multilabel_image_to_class(label_image: tf.Tensor, classes_file: str) -> tf.Tensor: classes_color_values, colors_labels = get_classes_color_from_file_multilabel(classes_file) # Convert label_image [H,W,3] to the classes [H,W,C],int32 according to the classes [C,3] with tf.name_scope('LabelAssign'): if len(label_image.get_shape()) == 3: diff = tf.cast(label_image[:, :, None, :], tf.float32) - tf.constant(classes_color_values[None, None, :, :]) # [H,W,C,3] elif len(label_image.get_shape()) == 4: diff = tf.cast(label_image[:, :, :, None, :], tf.float32) - tf.constant( classes_color_values[None, None, None, :, :]) # [B,H,W,C,3] else: raise NotImplementedError('Length is : {}'.format(len(label_image.get_shape()))) pixel_class_diff = tf.reduce_sum(tf.square(diff), axis=-1) # [H,W,C] or [B,H,W,C] class_label = tf.argmin(pixel_class_diff, axis=-1) # [H,W] or [B,H,W] return tf.gather(colors_labels, class_label) > 0
def data_augmentation_fn(input_image: tf.Tensor, label_image: tf.Tensor) -> (tf.Tensor, tf.Tensor): with tf.name_scope('DataAugmentation'): with tf.name_scope('random_flip_lr'): sample = tf.random_uniform([], 0, 1) label_image = tf.cond(sample > 0.5, lambda: tf.image.flip_left_right(label_image), lambda: label_image) input_image = tf.cond(sample > 0.5, lambda: tf.image.flip_left_right(input_image), lambda: input_image) with tf.name_scope('random_flip_ud'): sample = tf.random_uniform([], 0, 1) label_image = tf.cond(sample > 0.5, lambda: tf.image.flip_up_down(label_image), lambda: label_image) input_image = tf.cond(sample > 0.5, lambda: tf.image.flip_up_down(input_image), lambda: input_image) chanels = input_image.get_shape()[-1] input_image = tf.image.random_contrast(input_image, lower=0.8, upper=1.0) if chanels == 3: input_image = tf.image.random_hue(input_image, max_delta=0.1) input_image = tf.image.random_saturation(input_image, lower=0.8, upper=1.2) return input_image, label_image
def extract_patches_fn(image: tf.Tensor, patch_shape: list, offsets) -> tf.Tensor: """ :param image: tf.Tensor :param patch_shape: [h, w] :param offsets: tuple between 0 and 1 :return: patches [batch_patches, h, w, c] """ with tf.name_scope('patch_extraction'): h, w = patch_shape c = image.get_shape()[-1] offset_h = tf.cast(tf.round(offsets[0] * h // 2), dtype=tf.int32) offset_w = tf.cast(tf.round(offsets[1] * w // 2), dtype=tf.int32) offset_img = image[offset_h:, offset_w:, :] offset_img = offset_img[None, :, :, :] patches = tf.extract_image_patches(offset_img, ksizes=[1, h, w, 1], strides=[1, h // 2, w // 2, 1], rates=[1, 1, 1, 1], padding='VALID') patches_shape = tf.shape(patches) return tf.reshape(patches, [tf.reduce_prod(patches_shape[0:3]), h, w, int(c)]) # returns [batch_patches, h, w, c]
def tf_obj_shape(input): """ Convert tf objects to shape tuple. Arguments: input: tf.TensorShape, tf.Tensor, tf.AttrValue or tf.NodeDef the corresponding tensorflow object Returns: tuple: shape of the tensorflow object """ if isinstance(input, tf.TensorShape): return tuple([int(i.value) for i in input]) elif isinstance(input, tf.Tensor): return tf_obj_shape(input.get_shape()) elif isinstance(input, tf.AttrValue): return tuple([int(d.size) for d in input.shape.dim]) elif isinstance(input, tf.NodeDef): return tf_obj_shape(input.attr['shape']) else: raise TypeError("Input to `tf_obj_shape` has the wrong type.")
def get_weights(self, weight_tensor): """ Get Weights. Get a variable weights. Examples:
dnn = DNNTrainer(...) w = dnn.get_weights(denselayer.W) # get a dense layer weights w = dnn.get_weights(convlayer.b) # get a conv layer biases ``` Arguments: weight_tensor: `Tensor`. A Variable. Returns: `np.array`. The provided variable weights. """ return weight_tensor.eval(self.trainer.session)
```
def __init__(self, network, dictionary=None, seq_maxlen=25, clip_gradients=0.0, tensorboard_verbose=0, tensorboard_dir="/tmp/tflearn_logs/", checkpoint_path=None, max_checkpoints=None, session=None): assert isinstance(network, tf.Tensor), "'network' arg is not a Tensor!" self.net = network self.train_ops = tf.get_collection(tf.GraphKeys.TRAIN_OPS) self.trainer = Trainer(self.train_ops, clip_gradients=clip_gradients, tensorboard_dir=tensorboard_dir, tensorboard_verbose=tensorboard_verbose, checkpoint_path=checkpoint_path, max_checkpoints=max_checkpoints, session=session) self.session = self.trainer.session self.inputs = tf.get_collection(tf.GraphKeys.INPUTS) self.targets = tf.get_collection(tf.GraphKeys.TARGETS) self.predictor = Evaluator([self.net], session=self.session) self.dic = dictionary self.rev_dic = reverse_dictionary(dictionary) self.seq_maxlen = seq_maxlen
def get_weights(self, weight_tensor): """ Get weights. Get a variable weights. Examples: sgen = SequenceGenerator(...) w = sgen.get_weights(denselayer.W) -- get a dense layer weights Arguments: weight_tensor: `tf.Tensor`. A Variable. Returns: `np.array`. The provided variable weights. """ return weight_tensor.eval(self.trainer.session)
def softmax(x): """ Softmax. Computes softmax activations. For each batch `i` and class `j` we have softmax[i, j] = exp(logits[i, j]) / sum(exp(logits[i])) Arguments: x: A `Tensor`. Must be one of the following types: `float32`, `float64`. 2-D with shape `[batch_size, num_classes]`. Returns: A `Tensor`. Has the same type as `x`. Same shape as `x`. """ return tf.nn.softmax(x)
def elu(x): """ ELU. Exponential Linear Unit. Arguments: x : A `Tensor` with type `float`, `double`, `int32`, `int64`, `uint8`, `int16`, or `int8` Returns: A `tuple` of `tf.Tensor`. This layer inference, i.e. output Tensors at training and testing time. References: Fast and Accurate Deep Network Learning by Exponential Linear Units, Djork-Arné Clevert, Thomas Unterthiner, Sepp Hochreiter. 2015. Links: [http://arxiv.org/abs/1511.07289](http://arxiv.org/abs/1511.07289) """ return tf.nn.elu(x)
def selu(x): """ SELU. Scaled Exponential Linear Unit. Arguments x : A `Tensor` with type `float`, `double`, `int32`, `int64`, `uint8`, `int16`, or `int8` References: Self-Normalizing Neural Networks, Klambauer et al., 2017. Links: [https://arxiv.org/abs/1706.02515](https://arxiv.org/abs/1706.02515) """ alpha = 1.6732632423543772848170429916717 scale = 1.0507009873554804934193349852946 return scale*tf.where(x>=0.0, x, alpha*tf.nn.elu(x))
def global_avg_pool(incoming, name="GlobalAvgPool"): """ Global Average Pooling. Input: 4-D Tensor [batch, height, width, in_channels]. Output: 2-D Tensor [batch, pooled dim] Arguments: incoming: `Tensor`. Incoming 4-D Tensor. name: A name for this layer (optional). Default: 'GlobalAvgPool'. """ input_shape = utils.get_incoming_shape(incoming) assert len(input_shape) == 4, "Incoming Tensor shape must be 4-D" with tf.name_scope(name): inference = tf.reduce_mean(incoming, [1, 2]) # Track output tensor. tf.add_to_collection(tf.GraphKeys.LAYER_TENSOR + '/' + name, inference) return inference
def custom_layer(incoming, custom_fn, **kwargs): """ Custom Layer. A custom layer that can apply any operations to the incoming Tensor or list of `Tensor`. The custom function can be pass as a parameter along with its parameters. Arguments: incoming : A `Tensor` or list of `Tensor`. Incoming tensor. custom_fn : A custom `function`, to apply some ops on incoming tensor. **kwargs: Some custom parameters that custom function might need. """ name = "CustomLayer" if 'name' in kwargs: name = kwargs['name'] with tf.name_scope(name): inference = custom_fn(incoming, **kwargs) return inference
def reshape(incoming, new_shape, name="Reshape"): """ Reshape. A layer that reshape the incoming layer tensor output to the desired shape. Arguments: incoming: A `Tensor`. The incoming tensor. new_shape: A list of `int`. The desired shape. name: A name for this layer (optional). """ with tf.name_scope(name) as scope: inference = incoming if isinstance(inference, list): inference = tf.concat(0, inference) inference = tf.cast(inference, tf.float32) inference = tf.reshape(inference, shape=new_shape) inference.scope = scope # Track output tensor. tf.add_to_collection(tf.GraphKeys.LAYER_TENSOR + '/' + name, inference) return inference
def flatten(incoming, name="Flatten"): """ Flatten. Flatten the incoming Tensor. Input: (2+)-D `Tensor`. Output: 2-D `Tensor` [batch, flatten_dims]. Arguments: incoming: `Tensor`. The incoming tensor. """ input_shape = utils.get_incoming_shape(incoming) assert len(input_shape) > 1, "Incoming Tensor shape must be at least 2-D" dims = int(np.prod(input_shape[1:])) x = reshape(incoming, [-1, dims], name) # Track output tensor. tf.add_to_collection(tf.GraphKeys.LAYER_TENSOR + '/' + name, x) return x
def multi_target_data(name_list, shape, dtype=tf.float32): """ Multi Target Data. Create and concatenate multiple placeholders. To be used when a regression layer uses targets from different sources. Arguments: name_list: list of `str`. The names of the target placeholders. shape: list of `int`. The shape of the placeholders. dtype: `tf.type`, Placeholder data type (optional). Default: float32. Return: A `Tensor` of the concatenated placeholders. """ placeholders = [] for i in range(len(name_list)): with tf.name_scope(name_list[i]): p = tf.placeholder(shape=shape, dtype=dtype, name='Y') if p not in tf.get_collection(tf.GraphKeys.TARGETS): tf.add_to_collection(tf.GraphKeys.TARGETS, p) placeholders.append(p) return tf.concat(placeholders, axis=0)
def get_layer_by_name(name_or_scope): """ get_layer. Retrieve the output tensor of a layer with the given name or scope. Arguments: name_or_scope: `str`. The name (or scope) given to the layer to retrieve. Returns: A Tensor. """ # Track output tensor. c = tf.get_collection(tf.GraphKeys.LAYER_TENSOR + '/' + name_or_scope) if len(c) == 0: raise Exception("No layer found for this name.") if len(c) > 1: return c return c[0]
def _GradMom(op, v, out_grad, batch_size, mom=2): """Wrapper function for the operation type-specific GradMom functions below. Inputs: :op: A tensorflow operation of type in VALID_TYPES. :v: The read-tensor of the trainable variable consumed by this operation. :out_grad: The tensor containing the gradient w.r.t. to the output of the op (as computed by ``tf.gradients``). :batch_size: Batch size ``m`` (constant integer or scalar int tf.Tensor) :mom: Integer moment desired (defaults to 2).""" with tf.name_scope(op.name+"_grad_mom"): if op.type == "MatMul": return _MatMulGradMom(op, v, out_grad, batch_size, mom) elif op.type == "Conv2D": return _Conv2DGradMom(op, v, out_grad, batch_size, mom) elif op.type == "Add": return _AddGradMom(op, v, out_grad, batch_size, mom) else: raise ValueError("Don't know how to compute gradient moment for " "variable {}, consumed by operation of type {}".format(v.name, op.type))
def _MatMulGradMom(op, W, out_grad, batch_size, mom=2): """Computes gradient moment for a weight matrix through a MatMul operation. Assumes ``Z=tf.matmul(A, W)``, where ``W`` is a d1xd2 weight matrix, ``A`` are the nxd1 activations of the previous layer (n being the batch size). ``out_grad`` is the gradient w.r.t. ``Z``, as computed by ``tf.gradients()``. No transposes in the MatMul operation allowed. Inputs: :op: The MatMul operation :W: The weight matrix (the tensor, not the variable) :out_grad: The tensor of gradient w.r.t. to the output of the op :batch_size: Batch size n (constant integer or scalar int tf.Tensor) :mom: Integer moment desired (defaults to 2)""" assert op.type == "MatMul" t_a, t_b = op.get_attr("transpose_a"), op.get_attr("transpose_b") assert W is op.inputs[1] and not t_a and not t_b A = op.inputs[0] out_grad_pow = tf.pow(out_grad, mom) A_pow = tf.pow(A, mom) return tf.mul(batch_size, tf.matmul(A_pow, out_grad_pow, transpose_a=True))
def zero_state(self, batch_size, dtype): """Return zero-filled state tensor(s). Args: batch_size: int, float, or unit Tensor representing the batch size. dtype: the data type to use for the state. Returns: If `state_size` is an int or TensorShape, then the return value is a `N-D` tensor of shape `[batch_size x state_size]` filled with zeros. If `state_size` is a nested list or tuple, then the return value is a nested list or tuple (of the same structure) of `2-D` tensors with the shapes `[batch_size x s]` for each s in `state_size`. """ # Keep scope for backwards compatibility. with tf.name_scope(type(self).__name__ + "ZeroState", values=[batch_size]): return rnn_cell_impl._zero_state_tensors( # pylint: disable=protected-access self.state_size, batch_size, dtype)
def __init__(self, preserve_dims=1, name="batch_flatten"): """Constructs a BatchFlatten module. Args: preserve_dims: Number of leading dimensions that will not be reshaped. For example, given an input Tensor with shape `[B, H, W, C]`: * `preserve_dims=1` will return a Tensor with shape `[B, H*W*C]`. * `preserve_dims=2` will return a Tensor with shape `[B, H, W*C]`. * `preserve_dims=3` will return the input itself, shape `[B, H, W, C]`. * `preserve_dims=4` will return a Tensor with shape `[B, H, W, C, 1]`. * `preserve_dims>=5` will throw an error on build. The preserved dimensions can be unknown at building time. name: Name of the module. """ super(BatchFlatten, self).__init__( shape=(-1,), preserve_dims=preserve_dims, name=name)
def _build(self): """Connects the TrainableTensor module into the graph. Returns: A Tensor of shape as determined in the constructor. """ if "w" not in self._initializers: stddev = 1 / math.sqrt(np.prod(self._shape)) self._initializers["w"] = tf.truncated_normal_initializer(stddev=stddev) self._w = tf.get_variable("w", shape=self._shape, dtype=self._dtype, initializer=self._initializers["w"], partitioner=self._partitioners.get("w", None), regularizer=self._regularizers.get("w", None)) return self._w
def _build(self, inputs): """Connects the `TileByDim` module into the graph. Args: inputs: `Tensor` to tile. Returns: The tiled tensor. """ shape_inputs = inputs.get_shape().as_list() rank = len(shape_inputs) # Builds default lists for multiples to pass to `tf.tile`. full_multiples = [1] * rank # Updates lists with what the user provided. for dim, multiple in zip(self._dims, self._multiples): full_multiples[dim] = multiple return tf.tile(inputs, multiples=full_multiples)
def _build(self, inputs): """Connects the MergeDims module into the graph. Args: inputs: Tensor or a nested list of Tensors to merge. Its rank must be greater than or equal to `start` + `size`. Returns: The merged Tensor or a nested list of merged Tensors. Raises: ValueError: If any of the `inputs` tensors has insufficient rank. """ if nest.is_sequence(inputs): merged_tensors = [self._merge(tensor) for tensor in nest.flatten(inputs)] return nest.pack_sequence_as(inputs, merged_tensors) # inputs is a single tf.Tensor return self._merge(inputs)
def testModuleInfo_recursion(self): # pylint: disable=not-callable tf.reset_default_graph() dumb = DumbModule(name="dumb_a", no_nest=True) ph_0 = tf.placeholder(dtype=tf.float32, shape=(1, 10,)) val = {"one": ph_0, "self": None} val["self"] = val dumb(val) def check(check_type): sonnet_collection = tf.get_default_graph().get_collection( base_info.SONNET_COLLECTION_NAME) connected_subgraph = sonnet_collection[0].connected_subgraphs[0] self.assertIsInstance(connected_subgraph.inputs["inputs"]["one"], tf.Tensor) self.assertIsInstance( connected_subgraph.inputs["inputs"]["self"], check_type) self.assertIsInstance(connected_subgraph.outputs["one"], tf.Tensor) self.assertIsInstance(connected_subgraph.outputs["self"], check_type) check(dict) _copy_default_graph() check(base_info._UnserializableObject)
def __init__(self, inputs, output_dtype_shape_and_is_asset, spec, name): for tensor in inputs: if not isinstance(tensor, tf.Tensor): raise ValueError('Analyzers can only accept `Tensor`s as inputs') self._inputs = inputs self._outputs = [] self._output_is_asset_map = {} with tf.name_scope(name) as scope: self._name = scope for dtype, shape, is_asset in output_dtype_shape_and_is_asset: output_tensor = tf.placeholder(dtype, shape) if is_asset and output_tensor.dtype != tf.string: raise ValueError(('Tensor {} cannot represent an asset, because it ' 'is not a string.').format(output_tensor.name)) self._outputs.append(output_tensor) self._output_is_asset_map[output_tensor] = is_asset self._spec = spec tf.add_to_collection(ANALYZER_COLLECTION, self)
def combine_analyzer(x, output_dtype, output_shape, combiner_spec, name): """Applies the combiner over the whole dataset. Args: x: An input `Tensor` or `SparseTensor`. output_dtype: The dtype of the output of the analyzer. output_shape: The shape of the output of the analyzer. combiner_spec: A subclass of CombinerSpec. name: Similar to a TF op name. Used to define a unique scope for this analyzer, which can be used for debugging info. Returns: The combined values, which is a `Tensor` with type output_dtype and shape `output_shape`. These must be compatible with the combiner_spec. """ return Analyzer([x], [(output_dtype, output_shape, False)], combiner_spec, name).outputs[0]
def _numeric_combine(x, fn, reduce_instance_dims=True, name=None): """Apply an analyzer with _NumericCombineSpec to given input.""" if not isinstance(x, tf.Tensor): raise TypeError('Expected a Tensor, but got %r' % x) if reduce_instance_dims: # If reducing over all dimensions, result is scalar. shape = () elif x.shape.dims is not None: # If reducing over batch dimensions, with known shape, the result will be # the same shape as the input, but without the batch. shape = x.shape.as_list()[1:] else: # If reducing over batch dimensions, with unknown shape, the result will # also have unknown shape. shape = None return combine_analyzer( x, x.dtype, shape, _NumPyCombinerSpec(fn, reduce_instance_dims), name if name is not None else fn.__name__)
def size(x, reduce_instance_dims=True, name=None): """Computes the total size of instances in a `Tensor` over the whole dataset. Args: x: A `Tensor`. reduce_instance_dims: By default collapses the batch and instance dimensions to arrive at a single scalar output. If False, only collapses the batch dimension and outputs a vector of the same shape as the input. name: (Optional) A name for this operation. Returns: A `Tensor`. Has the same type as `x`. """ with tf.name_scope(name, 'size'): # Note: Calling `sum` defined in this module, not the builtin. return sum(tf.ones_like(x), reduce_instance_dims)
def mean(x, reduce_instance_dims=True, name=None): """Computes the mean of the values of a `Tensor` over the whole dataset. Args: x: A `Tensor`. reduce_instance_dims: By default collapses the batch and instance dimensions to arrive at a single scalar output. If False, only collapses the batch dimension and outputs a vector of the same shape as the input. name: (Optional) A name for this operation. Returns: A `Tensor` containing the mean. If `x` is floating point, the mean will have the same type as `x`. If `x` is integral, the output is cast to float32 for int8 and int16 and float64 for int32 and int64 (similar to the behavior of tf.truediv). """ with tf.name_scope(name, 'mean'): # Note: Calling `sum` defined in this module, not the builtin. return tf.divide( sum(x, reduce_instance_dims), size(x, reduce_instance_dims))