Python tensorflow.python.ops.nn_ops.softmax() Examples
The following are 30
code examples of tensorflow.python.ops.nn_ops.softmax().
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.python.ops.nn_ops
, or try the search function
.
Example #1
Source File: devel.py From avsr-tf1 with GNU General Public License v3.0 | 6 votes |
def mc_loss(labels, logits): r""" A multi-class cross-entropy loss :param labels: [batch_size, ] - Tensor of the correct class ids :param logits: [batch_size, num_classes] - Unscaled logits :return: [batch_size, ] - Tensor of average costs for each batch element """ num_classes = array_ops.shape(logits)[1] onehot_labels = array_ops.one_hot(labels, num_classes, dtype=logits.dtype) p = nn_ops.softmax(logits) p = clip_ops.clip_by_value(p, 1e-7, 1.0 - 1e-7) ce_loss = - onehot_labels * math_ops.log(p) - (1 - onehot_labels) * math_ops.log(1.0-p) cost = math_ops.reduce_sum(ce_loss, axis=1) return cost
Example #2
Source File: rnn_cell.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def _attention(self, query, attn_states): conv2d = nn_ops.conv2d reduce_sum = math_ops.reduce_sum softmax = nn_ops.softmax tanh = math_ops.tanh with vs.variable_scope("attention"): k = vs.get_variable( "attn_w", [1, 1, self._attn_size, self._attn_vec_size]) v = vs.get_variable("attn_v", [self._attn_vec_size]) hidden = array_ops.reshape(attn_states, [-1, self._attn_length, 1, self._attn_size]) hidden_features = conv2d(hidden, k, [1, 1, 1, 1], "SAME") y = _linear(query, self._attn_vec_size, True) y = array_ops.reshape(y, [-1, 1, 1, self._attn_vec_size]) s = reduce_sum(v * tanh(hidden_features + y), [2, 3]) a = softmax(s) d = reduce_sum( array_ops.reshape(a, [-1, self._attn_length, 1, 1]) * hidden, [1, 2]) new_attns = array_ops.reshape(d, [-1, self._attn_size]) new_attn_states = array_ops.slice(attn_states, [0, 1, 0], [-1, -1, -1]) return new_attns, new_attn_states
Example #3
Source File: categorical.py From lambda-packs with MIT License | 6 votes |
def _kl_categorical_categorical(a, b, name=None): """Calculate the batched KL divergence KL(a || b) with a and b Categorical. Args: a: instance of a Categorical distribution object. b: instance of a Categorical distribution object. name: (optional) Name to use for created operations. default is "kl_categorical_categorical". Returns: Batchwise KL(a || b) """ with ops.name_scope(name, "kl_categorical_categorical", values=[a.logits, b.logits]): # sum(probs log(probs / (1 - probs))) delta_log_probs1 = (nn_ops.log_softmax(a.logits) - nn_ops.log_softmax(b.logits)) return math_ops.reduce_sum(nn_ops.softmax(a.logits) * delta_log_probs1, axis=-1)
Example #4
Source File: onehot_categorical.py From lambda-packs with MIT License | 6 votes |
def _kl_categorical_categorical(a, b, name=None): """Calculate the batched KL divergence KL(a || b) with a, b OneHotCategorical. Args: a: instance of a OneHotCategorical distribution object. b: instance of a OneHotCategorical distribution object. name: (optional) Name to use for created operations. default is "kl_categorical_categorical". Returns: Batchwise KL(a || b) """ with ops.name_scope(name, "kl_categorical_categorical", values=[ a.logits, b.logits]): # sum(p ln(p / q)) return math_ops.reduce_sum( nn_ops.softmax(a.logits) * (nn_ops.log_softmax(a.logits) - nn_ops.log_softmax(b.logits)), axis=-1)
Example #5
Source File: categorical.py From deep_image_model with Apache License 2.0 | 6 votes |
def _kl_categorical_categorical(a, b, name=None): """Calculate the batched KL divergence KL(a || b) with a and b Categorical. Args: a: instance of a Categorical distribution object. b: instance of a Categorical distribution object. name: (optional) Name to use for created operations. default is "kl_categorical_categorical". Returns: Batchwise KL(a || b) """ with ops.name_scope( name, "kl_categorical_categorical", [a.logits, b.logits]): # sum(p*ln(p/q)) return math_ops.reduce_sum( nn_ops.softmax(a.logits)*(nn_ops.log_softmax(a.logits) - nn_ops.log_softmax(b.logits)), reduction_indices=[-1])
Example #6
Source File: layers_test.py From tf-slim with Apache License 2.0 | 6 votes |
def testSoftmax3DUnknownSize(self): logits = np.ones((2, 3, 2)) logits[0, 0, 0] = 0 logits[1, 1, 1] = 0 logit_placeholder = array_ops.placeholder( dtypes.float32, shape=(None, None, 2)) feed_dict = {logit_placeholder: logits} exp_prediction = 0.5 * np.ones((2, 3, 2)) exp_prediction[0, 0, 0] = self.low exp_prediction[0, 0, 1] = self.high exp_prediction[1, 1, 0] = self.high exp_prediction[1, 1, 1] = self.low prediction = _layers.softmax(logit_placeholder) with self.cached_session() as sess: prediction = sess.run(prediction, feed_dict=feed_dict) self.assertAllClose(exp_prediction, prediction)
Example #7
Source File: tf_utils.py From neural-symbolic-machines with Apache License 2.0 | 6 votes |
def _attention(self, query, attn_inputs): with vs.variable_scope("attention"): attn_query = tf.layers.dense( inputs=query, units=self._attn_vec_size, use_bias=True) attn_keys = tf.layers.dense( inputs=attn_inputs, units=self._attn_vec_size, use_bias=True) attn_contents = tf.layers.dense( inputs=attn_inputs, units=self._attn_size, use_bias=True) v_attn = vs.get_variable("attn_v", [self._attn_vec_size]) scores = attn_sum_bahdanau(v_attn, attn_keys, attn_query) if self.attn_masks is not None: score_masks = self.attn_masks scores = scores * score_masks + (1.0 - score_masks) * tf.float32.min attn_weights = nn_ops.softmax(scores) new_attns = math_ops.reduce_sum( tf.expand_dims(attn_weights, -1) * attn_contents, [1]) return new_attns
Example #8
Source File: devel.py From avsr-tf1 with GNU General Public License v3.0 | 6 votes |
def focal_loss(labels, logits, gamma=2.0): r""" Multi-class focal loss implementation: https://arxiv.org/abs/1708.02002 :param labels: [batch_size, ] - Tensor of the correct class ids :param logits: [batch_size, num_classes] - Unscaled logits :param gamma: focal loss weight :return: [batch_size, ] - Tensor of average costs for each batch element """ num_classes = array_ops.shape(logits)[1] onehot_labels = array_ops.one_hot(labels, num_classes, dtype=logits.dtype) p = nn_ops.softmax(logits) p = clip_ops.clip_by_value(p, 1e-7, 1.0 - 1e-7) f_loss = - onehot_labels * math_ops.pow(1.0 - p, gamma) * math_ops.log(p) \ - (1 - onehot_labels) * math_ops.pow(p, gamma) * math_ops.log(1.0 - p) cost = math_ops.reduce_sum(f_loss, axis=1) return cost
Example #9
Source File: rnn_cell.py From deep_image_model with Apache License 2.0 | 6 votes |
def _attention(self, query, attn_states): conv2d = nn_ops.conv2d reduce_sum = math_ops.reduce_sum softmax = nn_ops.softmax tanh = math_ops.tanh with vs.variable_scope("Attention"): k = vs.get_variable("AttnW", [1, 1, self._attn_size, self._attn_vec_size]) v = vs.get_variable("AttnV", [self._attn_vec_size]) hidden = array_ops.reshape(attn_states, [-1, self._attn_length, 1, self._attn_size]) hidden_features = conv2d(hidden, k, [1, 1, 1, 1], "SAME") y = _linear(query, self._attn_vec_size, True) y = array_ops.reshape(y, [-1, 1, 1, self._attn_vec_size]) s = reduce_sum(v * tanh(hidden_features + y), [2, 3]) a = softmax(s) d = reduce_sum( array_ops.reshape(a, [-1, self._attn_length, 1, 1]) * hidden, [1, 2]) new_attns = array_ops.reshape(d, [-1, self._attn_size]) new_attn_states = array_ops.slice(attn_states, [0, 1, 0], [-1, -1, -1]) return new_attns, new_attn_states
Example #10
Source File: categorical.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def _kl_categorical_categorical(a, b, name=None): """Calculate the batched KL divergence KL(a || b) with a and b Categorical. Args: a: instance of a Categorical distribution object. b: instance of a Categorical distribution object. name: (optional) Name to use for created operations. default is "kl_categorical_categorical". Returns: Batchwise KL(a || b) """ with ops.name_scope( name, "kl_categorical_categorical", [a.logits, b.logits]): # sum(p*ln(p/q)) return math_ops.reduce_sum( nn_ops.softmax(a.logits)*(nn_ops.log_softmax(a.logits) - nn_ops.log_softmax(b.logits)), reduction_indices=[-1])
Example #11
Source File: nn_test.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def test_unary_ops(self): ops = [ ('relu', nn_ops.relu, nn.relu), ('relu6', nn_ops.relu6, nn.relu6), ('crelu', nn_ops.crelu, nn.crelu), ('elu', nn_ops.elu, nn.elu), ('softplus', nn_ops.softplus, nn.softplus), ('l2_loss', nn_ops.l2_loss, nn.l2_loss), ('softmax', nn_ops.softmax, nn.softmax), ('log_softmax', nn_ops.log_softmax, nn.log_softmax), ] for op_name, tf_op, lt_op in ops: golden_tensor = tf_op(self.original_lt.tensor) golden_lt = core.LabeledTensor(golden_tensor, self.axes) actual_lt = lt_op(self.original_lt) self.assertIn(op_name, actual_lt.name) self.assertLabeledTensorsEqual(golden_lt, actual_lt)
Example #12
Source File: rnn_cell.py From lambda-packs with MIT License | 6 votes |
def _attention(self, query, attn_states): conv2d = nn_ops.conv2d reduce_sum = math_ops.reduce_sum softmax = nn_ops.softmax tanh = math_ops.tanh with vs.variable_scope("attention"): k = vs.get_variable( "attn_w", [1, 1, self._attn_size, self._attn_vec_size]) v = vs.get_variable("attn_v", [self._attn_vec_size]) hidden = array_ops.reshape(attn_states, [-1, self._attn_length, 1, self._attn_size]) hidden_features = conv2d(hidden, k, [1, 1, 1, 1], "SAME") y = _linear(query, self._attn_vec_size, True) y = array_ops.reshape(y, [-1, 1, 1, self._attn_vec_size]) s = reduce_sum(v * tanh(hidden_features + y), [2, 3]) a = softmax(s) d = reduce_sum( array_ops.reshape(a, [-1, self._attn_length, 1, 1]) * hidden, [1, 2]) new_attns = array_ops.reshape(d, [-1, self._attn_size]) new_attn_states = array_ops.slice(attn_states, [0, 1, 0], [-1, -1, -1]) return new_attns, new_attn_states
Example #13
Source File: rnn_cell.py From Multiview2Novelview with MIT License | 6 votes |
def _attention(self, query, attn_states): conv2d = nn_ops.conv2d reduce_sum = math_ops.reduce_sum softmax = nn_ops.softmax tanh = math_ops.tanh with vs.variable_scope("attention"): k = vs.get_variable( "attn_w", [1, 1, self._attn_size, self._attn_vec_size]) v = vs.get_variable("attn_v", [self._attn_vec_size]) hidden = array_ops.reshape(attn_states, [-1, self._attn_length, 1, self._attn_size]) hidden_features = conv2d(hidden, k, [1, 1, 1, 1], "SAME") if self._linear3 is None: self._linear3 = _Linear(query, self._attn_vec_size, True) y = self._linear3(query) y = array_ops.reshape(y, [-1, 1, 1, self._attn_vec_size]) s = reduce_sum(v * tanh(hidden_features + y), [2, 3]) a = softmax(s) d = reduce_sum( array_ops.reshape(a, [-1, self._attn_length, 1, 1]) * hidden, [1, 2]) new_attns = array_ops.reshape(d, [-1, self._attn_size]) new_attn_states = array_ops.slice(attn_states, [0, 1, 0], [-1, -1, -1]) return new_attns, new_attn_states
Example #14
Source File: seq2seq.py From DeepAffinity with GNU General Public License v3.0 | 5 votes |
def sequence_loss(logits, targets, weights, average_across_timesteps=True, average_across_batch=True, softmax_loss_function=None, name=None): """Weighted cross-entropy loss for a sequence of logits, batch-collapsed. Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (labels, logits) -> loss-batch to be used instead of the standard softmax (the default if this is None). **Note that to avoid confusion, it is required for the function to accept named arguments.** name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ with ops.name_scope(name, "sequence_loss", logits + targets + weights): cost = math_ops.reduce_sum( sequence_loss_by_example( logits, targets, weights, average_across_timesteps=average_across_timesteps, softmax_loss_function=softmax_loss_function)) if average_across_batch: batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, cost.dtype) else: return cost
Example #15
Source File: seq2seq.py From DeepAffinity with GNU General Public License v3.0 | 5 votes |
def sequence_loss(logits, targets, weights, average_across_timesteps=True, average_across_batch=True, softmax_loss_function=None, name=None): """Weighted cross-entropy loss for a sequence of logits, batch-collapsed. Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (labels, logits) -> loss-batch to be used instead of the standard softmax (the default if this is None). **Note that to avoid confusion, it is required for the function to accept named arguments.** name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ with ops.name_scope(name, "sequence_loss", logits + targets + weights): cost = math_ops.reduce_sum( sequence_loss_by_example( logits, targets, weights, average_across_timesteps=average_across_timesteps, softmax_loss_function=softmax_loss_function)) if average_across_batch: batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, cost.dtype) else: return cost
Example #16
Source File: seq2seq.py From DeepAffinity with GNU General Public License v3.0 | 5 votes |
def sequence_loss(logits, targets, weights, average_across_timesteps=True, average_across_batch=True, softmax_loss_function=None, name=None): """Weighted cross-entropy loss for a sequence of logits, batch-collapsed. Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (labels, logits) -> loss-batch to be used instead of the standard softmax (the default if this is None). **Note that to avoid confusion, it is required for the function to accept named arguments.** name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ with ops.name_scope(name, "sequence_loss", logits + targets + weights): cost = math_ops.reduce_sum( sequence_loss_by_example( logits, targets, weights, average_across_timesteps=average_across_timesteps, softmax_loss_function=softmax_loss_function)) if average_across_batch: batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, cost.dtype) else: return cost
Example #17
Source File: seq2seq.py From DeepAffinity with GNU General Public License v3.0 | 5 votes |
def sequence_loss(logits, targets, weights, average_across_timesteps=True, average_across_batch=True, softmax_loss_function=None, name=None): """Weighted cross-entropy loss for a sequence of logits, batch-collapsed. Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (labels, logits) -> loss-batch to be used instead of the standard softmax (the default if this is None). **Note that to avoid confusion, it is required for the function to accept named arguments.** name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ with ops.name_scope(name, "sequence_loss", logits + targets + weights): cost = math_ops.reduce_sum( sequence_loss_by_example( logits, targets, weights, average_across_timesteps=average_across_timesteps, softmax_loss_function=softmax_loss_function)) if average_across_batch: batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, cost.dtype) else: return cost
Example #18
Source File: seq2seq.py From DeepAffinity with GNU General Public License v3.0 | 5 votes |
def sequence_loss(logits, targets, weights, average_across_timesteps=True, average_across_batch=True, softmax_loss_function=None, name=None): """Weighted cross-entropy loss for a sequence of logits, batch-collapsed. Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (labels, logits) -> loss-batch to be used instead of the standard softmax (the default if this is None). **Note that to avoid confusion, it is required for the function to accept named arguments.** name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ with ops.name_scope(name, "sequence_loss", logits + targets + weights): cost = math_ops.reduce_sum( sequence_loss_by_example( logits, targets, weights, average_across_timesteps=average_across_timesteps, softmax_loss_function=softmax_loss_function)) if average_across_batch: batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, cost.dtype) else: return cost
Example #19
Source File: hard_decisions_to_data_then_nn.py From deep_image_model with Apache License 2.0 | 5 votes |
def inference_graph(self, data, data_spec=None): """Returns the op that performs inference on a batch of data.""" return nn_ops.softmax( self._base_inference( data, data_spec=data_spec, soft=True)) # pylint: disable=unused-argument
Example #20
Source File: seq2seq.py From DeepAffinity with GNU General Public License v3.0 | 5 votes |
def sequence_loss(logits, targets, weights, average_across_timesteps=True, average_across_batch=True, softmax_loss_function=None, name=None): """Weighted cross-entropy loss for a sequence of logits, batch-collapsed. Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (labels, logits) -> loss-batch to be used instead of the standard softmax (the default if this is None). **Note that to avoid confusion, it is required for the function to accept named arguments.** name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ with ops.name_scope(name, "sequence_loss", logits + targets + weights): cost = math_ops.reduce_sum( sequence_loss_by_example( logits, targets, weights, average_across_timesteps=average_across_timesteps, softmax_loss_function=softmax_loss_function)) if average_across_batch: batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, cost.dtype) else: return cost
Example #21
Source File: hard_decisions_to_data_then_nn.py From deep_image_model with Apache License 2.0 | 5 votes |
def _base_inference(self, data, data_spec=None, soft=False): if soft: inference_result = self.layers[0].soft_inference_graph(data) else: inference_result = self._do_layer_inference(self.layers[0], data) for layer in self.layers[1:]: inference_result = self._do_layer_inference(layer, inference_result) output_size = 1 if self.is_regression else self.params.num_classes output = layers.fully_connected( inference_result, output_size, activation_fn=nn_ops.softmax) return output
Example #22
Source File: seq2seq.py From deep-text-corrector with Apache License 2.0 | 5 votes |
def sequence_loss(logits, targets, weights, average_across_timesteps=True, average_across_batch=True, softmax_loss_function=None, name=None): """Weighted cross-entropy loss for a sequence of logits, batch-collapsed. Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (inputs-batch, labels-batch) -> loss-batch to be used instead of the standard softmax (the default if this is None). name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ with ops.name_scope(name, "sequence_loss", logits + targets + weights): cost = math_ops.reduce_sum(sequence_loss_by_example( logits, targets, weights, average_across_timesteps=average_across_timesteps, softmax_loss_function=softmax_loss_function)) if average_across_batch: batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, cost.dtype) else: return cost
Example #23
Source File: seq2seq.py From Attention-OCR with MIT License | 5 votes |
def sequence_loss(logits, targets, weights, average_across_timesteps=True, average_across_batch=True, softmax_loss_function=None, name=None): """Weighted cross-entropy loss for a sequence of logits, batch-collapsed. Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (inputs-batch, labels-batch) -> loss-batch to be used instead of the standard softmax (the default if this is None). name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ with ops.name_scope(name, "sequence_loss", logits + targets + weights): cost = math_ops.reduce_sum(sequence_loss_by_example( logits, targets, weights, average_across_timesteps=average_across_timesteps, softmax_loss_function=softmax_loss_function)) if average_across_batch: batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, dtypes.float32) else: return cost
Example #24
Source File: hybrid_model.py From deep_image_model with Apache License 2.0 | 5 votes |
def _base_inference(self, data, data_spec=None): """Returns an op that performs inference without a softmax.""" inference_result = self._do_layer_inference(self.layers[0], data) for layer in self.layers[1:]: inference_result = self._do_layer_inference(layer, inference_result) output_size = 1 if self.is_regression else self.params.num_classes output = layers.fully_connected( inference_result, output_size, activation_fn=array_ops.identity) return output
Example #25
Source File: seq2seq.py From deep_image_model with Apache License 2.0 | 5 votes |
def sequence_loss(logits, targets, weights, average_across_timesteps=True, average_across_batch=True, softmax_loss_function=None, name=None): """Weighted cross-entropy loss for a sequence of logits, batch-collapsed. Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (inputs-batch, labels-batch) -> loss-batch to be used instead of the standard softmax (the default if this is None). name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ with ops.name_scope(name, "sequence_loss", logits + targets + weights): cost = math_ops.reduce_sum(sequence_loss_by_example( logits, targets, weights, average_across_timesteps=average_across_timesteps, softmax_loss_function=softmax_loss_function)) if average_across_batch: batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, cost.dtype) else: return cost
Example #26
Source File: layers_test.py From tf-slim with Apache License 2.0 | 5 votes |
def test_empty_x_results_in_empty_output(self): # Empty x is common if someone masks their input with tf.boolean_mask in # order to drop missing entries, and in a particular batch all entries are # missing. with self.cached_session(): x = np.array([]).reshape(0, 3) self.assertEqual(0, array_ops.size(x).eval()) y = _layers.legacy_fully_connected(x, 2, activation_fn=nn_ops.softmax) variables_lib.global_variables_initializer().run() expected_y = np.array([]).reshape(0, 2) np.testing.assert_array_equal(expected_y, y.eval())
Example #27
Source File: layers_test.py From tf-slim with Apache License 2.0 | 5 votes |
def testSoftmax3D(self): logits = np.ones((2, 3, 2)) logits[0, 0, 0] = 0 logits[1, 1, 1] = 0 logits = constant_op.constant(logits) exp_prediction = 0.5 * np.ones((2, 3, 2)) exp_prediction[0, 0, 0] = self.low exp_prediction[0, 0, 1] = self.high exp_prediction[1, 1, 0] = self.high exp_prediction[1, 1, 1] = self.low prediction = _layers.softmax(logits) with self.cached_session() as sess: prediction = sess.run(prediction) self.assertAllClose(exp_prediction, prediction)
Example #28
Source File: layers_test.py From tf-slim with Apache License 2.0 | 5 votes |
def testSoftmax2D(self): logits = constant_op.constant([[0.0, 1], [1, 1], [1, 0]]) prediction = _layers.softmax(logits) exp_prediction = np.array([[self.low, self.high], [0.5, 0.5], [self.high, self.low]]) with self.cached_session() as sess: prediction = sess.run(prediction) self.assertAllClose(exp_prediction, prediction)
Example #29
Source File: custom_ssim.py From MimickNet with Apache License 2.0 | 5 votes |
def _fspecial_gauss(size, sigma): size = ops.convert_to_tensor(size, dtypes.int32) sigma = ops.convert_to_tensor(sigma) coords = math_ops.cast(math_ops.range(size), sigma.dtype) coords -= math_ops.cast(size - 1, sigma.dtype) / 2.0 g = math_ops.square(coords) g *= -0.5 / math_ops.square(sigma) g = array_ops.reshape(g, shape=[1, -1]) + array_ops.reshape(g, shape=[-1, 1]) g = array_ops.reshape(g, shape=[1, -1]) # For tf.nn.softmax(). g = nn_ops.softmax(g) return array_ops.reshape(g, shape=[size, size, 1, 1])
Example #30
Source File: layers_test.py From tf-slim with Apache License 2.0 | 5 votes |
def testSoftmaxUndefinedNthDimension(self): logits = array_ops.placeholder(dtypes.float32) with self.assertRaises(ValueError): _layers.softmax(logits)