我们从Python开源项目中,提取了以下29个代码示例,用于说明如何使用tensorflow.python.ops.nn_ops.relu()。
def __init__(self, num_units, forget_bias=1.0, reuse_norm=False, input_size=None, activation=nn_ops.relu, layer_norm=True, norm_gain=1.0, norm_shift=0.0, loop_steps=1, decay_rate=0.9, learning_rate=0.5, dropout_keep_prob=1.0, dropout_prob_seed=None): if input_size is not None: logging.warn("%s: The input_size parameter is deprecated.", self) self._num_units = num_units self._activation = activation self._forget_bias = forget_bias self._reuse_norm = reuse_norm self._keep_prob = dropout_keep_prob self._seed = dropout_prob_seed self._layer_norm = layer_norm self._S = loop_steps self._eta = learning_rate self._lambda = decay_rate self._g = norm_gain self._b = norm_shift
def vgg_arg_scope(weight_decay=0.0005): """Defines the VGG arg scope. Args: weight_decay: The l2 regularization coefficient. Returns: An arg_scope. """ with arg_scope( [layers.conv2d, layers_lib.fully_connected], activation_fn=nn_ops.relu, weights_regularizer=regularizers.l2_regularizer(weight_decay), biases_initializer=init_ops.zeros_initializer()): with arg_scope([layers.conv2d], padding='SAME') as arg_sc: return arg_sc
def hinge_loss(logits, target, scope=None): """Method that returns the loss tensor for hinge loss. Args: logits: The logits, a float tensor. target: The ground truth output tensor. Its shape should match the shape of logits. The values of the tensor are expected to be 0.0 or 1.0. scope: The scope for the operations performed in computing the loss. Returns: A `Tensor` of same shape as logits and target representing the loss values across the batch. Raises: ValueError: If the shapes of `logits` and `target` don't match. """ with ops.name_scope(scope, "hinge_loss", [logits, target]) as scope: logits.get_shape().assert_is_compatible_with(target.get_shape()) # We first need to convert binary labels to -1/1 labels (as floats). target = math_ops.to_float(target) all_ones = array_ops.ones_like(target) labels = math_ops.sub(2 * target, all_ones) losses = nn_ops.relu(math_ops.sub(all_ones, math_ops.mul(labels, logits))) return losses
def hinge_loss(logits, labels=None, scope=None, target=None): """Method that returns the loss tensor for hinge loss. Args: logits: The logits, a float tensor. labels: The ground truth output tensor. Its shape should match the shape of logits. The values of the tensor are expected to be 0.0 or 1.0. scope: The scope for the operations performed in computing the loss. target: Deprecated alias for `labels`. Returns: A `Tensor` of same shape as logits and target representing the loss values across the batch. Raises: ValueError: If the shapes of `logits` and `labels` don't match. """ labels = _labels(labels, target) with ops.name_scope(scope, "hinge_loss", [logits, labels]) as scope: logits.get_shape().assert_is_compatible_with(labels.get_shape()) # We first need to convert binary labels to -1/1 labels (as floats). labels = math_ops.to_float(labels) all_ones = array_ops.ones_like(labels) labels = math_ops.sub(2 * labels, all_ones) return nn_ops.relu(math_ops.sub(all_ones, math_ops.mul(labels, logits)))
def hinge_loss(logits, labels=None, scope=None): """Method that returns the loss tensor for hinge loss. Args: logits: The logits, a float tensor. labels: The ground truth output tensor. Its shape should match the shape of logits. The values of the tensor are expected to be 0.0 or 1.0. scope: The scope for the operations performed in computing the loss. Returns: A `Tensor` of same shape as `logits` and `labels` representing the loss values across the batch. Raises: ValueError: If the shapes of `logits` and `labels` don't match. """ with ops.name_scope(scope, "hinge_loss", [logits, labels]) as scope: logits.get_shape().assert_is_compatible_with(labels.get_shape()) # We first need to convert binary labels to -1/1 labels (as floats). labels = math_ops.to_float(labels) all_ones = array_ops.ones_like(labels) labels = math_ops.subtract(2 * labels, all_ones) return nn_ops.relu( math_ops.subtract(all_ones, math_ops.multiply(labels, logits)))
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)
def testGrid2BasicLSTMCellWithRelu(self): with self.test_session() as sess: with variable_scope.variable_scope( 'root', initializer=init_ops.constant_initializer(0.2)): x = array_ops.zeros([1, 3]) m = array_ops.zeros([1, 4]) cell = grid_rnn_cell.Grid2BasicLSTMCell( 2, tied=False, non_recurrent_fn=nn_ops.relu) self.assertEqual(cell.state_size, 4) g, s = cell(x, m) self.assertEqual(g.get_shape(), (1, 2)) self.assertEqual(s.get_shape(), (1, 4)) sess.run([variables.global_variables_initializer()]) res = sess.run( [g, s], {x: np.array([[1., 1., 1.]]), m: np.array([[0.1, 0.2, 0.3, 0.4]])}) self.assertEqual(res[0].shape, (1, 2)) self.assertEqual(res[1].shape, (1, 4)) self.assertAllClose(res[0], [[0.31667367, 0.31667367]]) self.assertAllClose(res[1], [[0.29530135, 0.37520045, 0.17044567, 0.21292259]])
def testGrid2LSTMCellWithRelu(self): with self.test_session() as sess: with variable_scope.variable_scope( 'root', initializer=init_ops.constant_initializer(0.5)): x = array_ops.zeros([1, 3]) m = array_ops.zeros([1, 4]) cell = grid_rnn_cell.Grid2LSTMCell( 2, use_peepholes=True, non_recurrent_fn=nn_ops.relu) self.assertEqual(cell.state_size, 4) g, s = cell(x, m) self.assertEqual(g.get_shape(), (1, 2)) self.assertEqual(s.get_shape(), (1, 4)) sess.run([variables.global_variables_initializer()]) res = sess.run( [g, s], {x: np.array([[1., 1., 1.]]), m: np.array([[0.1, 0.2, 0.3, 0.4]])}) self.assertEqual(res[0].shape, (1, 2)) self.assertEqual(res[1].shape, (1, 4)) self.assertAllClose(res[0], [[2.1831727, 2.1831727]]) self.assertAllClose(res[1], [[0.92270052, 1.02325559, 0.66159075, 0.70475441]])
def testGrid2BasicRNNCellWithRelu(self): with self.test_session() as sess: with variable_scope.variable_scope( 'root', initializer=init_ops.constant_initializer(0.5)): x = array_ops.zeros([1, 2]) m = array_ops.zeros([1, 2]) cell = grid_rnn_cell.Grid2BasicRNNCell(2, non_recurrent_fn=nn_ops.relu) self.assertEqual(cell.state_size, 2) g, s = cell(x, m) self.assertEqual(g.get_shape(), (1, 2)) self.assertEqual(s.get_shape(), (1, 2)) sess.run([variables.global_variables_initializer()]) res = sess.run([g, s], {x: np.array([[1., 1.]]), m: np.array([[0.1, 0.1]])}) self.assertEqual(res[0].shape, (1, 2)) self.assertEqual(res[1].shape, (1, 2)) self.assertAllClose(res[0], [[1.80049896, 1.80049896]]) self.assertAllClose(res[1], [[0.80049896, 0.80049896]])
def resnet_arg_scope(is_training=True, weight_decay=cfg.TRAIN.WEIGHT_DECAY, batch_norm_decay=0.997, batch_norm_epsilon=1e-5, batch_norm_scale=True): batch_norm_params = { # NOTE 'is_training' here does not work because inside resnet it gets reset: # https://github.com/tensorflow/models/blob/master/slim/nets/resnet_v1.py#L187 'is_training': False, 'decay': batch_norm_decay, 'epsilon': batch_norm_epsilon, 'scale': batch_norm_scale, 'trainable': cfg.RESNET.BN_TRAIN, 'updates_collections': ops.GraphKeys.UPDATE_OPS } with arg_scope( [slim.conv2d], weights_regularizer=regularizers.l2_regularizer(weight_decay), weights_initializer=initializers.variance_scaling_initializer(), trainable=is_training, activation_fn=nn_ops.relu, normalizer_fn=layers.batch_norm, normalizer_params=batch_norm_params): with arg_scope([layers.batch_norm], **batch_norm_params) as arg_sc: return arg_sc
def bottleneck_trans_same(inputs, depth, depth_bottleneck, stride, rate=1, outputs_collections=None, scope=None): """Bottleneck residual unit variant with BN after convolutions. This is the original residual unit proposed in [1]. See Fig. 1(a) of [2] for its definition. Note that we use here the bottleneck variant which has an extra bottleneck layer. When putting together two consecutive ResNet blocks that use this unit, one should use stride = 2 in the last unit of the first block. Args: inputs: A tensor of size [batch, height, width, channels]. depth: The depth of the ResNet unit output. depth_bottleneck: The depth of the bottleneck layers. stride: The ResNet unit's stride. Determines the amount of downsampling of the units output compared to its input. rate: An integer, rate for atrous convolution. outputs_collections: Collection to add the ResNet unit output. scope: Optional variable_scope. Returns: The ResNet unit's output. """ with tf.variable_scope(scope, 'bottleneck_trans', [inputs]) as sc: shortcut = slim.conv2d_transpose(inputs, depth, 3, stride=stride, activation_fn=None, scope='shortcut', padding='SAME') residual = slim.conv2d_transpose(inputs, depth_bottleneck, [1, 1], stride=1, scope='conv1_trans') residual = slim.conv2d_transpose(residual, depth_bottleneck, 3, stride=stride, scope='conv2', padding='SAME') residual = slim.conv2d_transpose(residual, depth, [1, 1], stride=1, activation_fn=None, scope='conv3_trans') output = tf.nn.relu(shortcut + residual) return slim.utils.collect_named_outputs(outputs_collections, sc.original_name_scope, output)
def bottleneck_trans_valid(inputs, depth, depth_bottleneck, stride, rate=1, outputs_collections=None, scope=None): """Bottleneck residual unit variant with BN after convolutions. This is the original residual unit proposed in [1]. See Fig. 1(a) of [2] for its definition. Note that we use here the bottleneck variant which has an extra bottleneck layer. When putting together two consecutive ResNet blocks that use this unit, one should use stride = 2 in the last unit of the first block. Args: inputs: A tensor of size [batch, height, width, channels]. depth: The depth of the ResNet unit output. depth_bottleneck: The depth of the bottleneck layers. stride: The ResNet unit's stride. Determines the amount of downsampling of the units output compared to its input. rate: An integer, rate for atrous convolution. outputs_collections: Collection to add the ResNet unit output. scope: Optional variable_scope. Returns: The ResNet unit's output. """ with tf.variable_scope(scope, 'bottleneck_trans', [inputs]) as sc: shortcut = slim.conv2d_transpose(inputs, depth, 3, stride=stride, activation_fn=None, scope='shortcut', padding='VALID') residual = slim.conv2d_transpose(inputs, depth_bottleneck, [1, 1], stride=1, scope='conv1_trans') residual = slim.conv2d_transpose(residual, depth_bottleneck, 3, stride=stride, scope='conv2', padding='VALID') residual = slim.conv2d_transpose(residual, depth, [1, 1], stride=1, activation_fn=None, scope='conv3_trans') output = tf.nn.relu(shortcut + residual) return slim.utils.collect_named_outputs(outputs_collections, sc.original_name_scope, output)
def alexnet_v2_arg_scope(weight_decay=0.0005): with arg_scope( [layers.conv2d, layers_lib.fully_connected], activation_fn=nn_ops.relu, biases_initializer=init_ops.constant_initializer(0.1), weights_regularizer=regularizers.l2_regularizer(weight_decay)): with arg_scope([layers.conv2d], padding='SAME'): with arg_scope([layers_lib.max_pool2d], padding='VALID') as arg_sc: return arg_sc
def modrelu(z, b, comp): if comp: z_norm = math_ops.sqrt(math_ops.square(math_ops.real(z)) + math_ops.square(math_ops.imag(z))) + 0.00001 step1 = nn_ops.bias_add(z_norm, b) step2 = math_ops.complex(nn_ops.relu(step1), array_ops.zeros_like(z_norm)) step3 = z/math_ops.complex(z_norm, array_ops.zeros_like(z_norm)) else: z_norm = math_ops.abs(z) + 0.00001 step1 = nn_ops.bias_add(z_norm, b) step2 = nn_ops.relu(step1) step3 = math_ops.sign(z) return math_ops.multiply(step3, step2)
def testCreateWithActivation(self): height, width = 3, 3 with self.test_session(): images = random_ops.random_uniform((5, height, width, 3), seed=1) output = _layers.bias_add(images, activation_fn=nn_ops.relu) self.assertEqual(output.op.name, 'BiasAdd/Relu') self.assertListEqual(output.get_shape().as_list(), [5, height, width, 3])
def testCreateFC(self): height, width = 3, 3 for layer_fn in (_layers.fully_connected, layers_lib.relu): with ops.Graph().as_default() as g, self.test_session(g): inputs = np.random.uniform(size=(5, height * width * 3)) output = layer_fn(inputs, 32) self.assertEqual(output.op.name, 'fully_connected/Relu') self.assertListEqual(output.get_shape().as_list(), [5, 32]) weights = variables.get_variables_by_name('weights')[0] self.assertListEqual(weights.get_shape().as_list(), [3 * 3 * 3, 32]) biases = variables.get_variables_by_name('biases')[0] self.assertListEqual(biases.get_shape().as_list(), [32])
def testStackRelu(self): height, width = 3, 3 with self.test_session(): images = random_ops.random_uniform( (5, height * width * 3), seed=1, name='images') output = _layers.stack(images, layers_lib.relu, [10, 20, 30]) self.assertEqual(output.op.name, 'Stack/fully_connected_3/Relu') self.assertListEqual(output.get_shape().as_list(), [5, 30])
def test_summarize_activation_relu(self): with self.test_session(): var = variables.Variable(1) op = nn_ops.relu(var, name='SummaryTest') summary_op = summaries_lib.summarize_activation(op) self.assertEquals(summary_op.op.type, 'HistogramSummary') names = [op.op.name for op in ops.get_collection(ops.GraphKeys.SUMMARIES)] self.assertEquals(len(names), 2) self.assertIn(u'SummaryTest/zeros', names) self.assertIn(u'SummaryTest/activation', names)
def testGridRNNEdgeCasesNoOutput(self): with self.test_session() as sess: with variable_scope.variable_scope( 'root', initializer=init_ops.constant_initializer(0.5)): x = array_ops.zeros([1, 2]) m = array_ops.zeros([1, 4]) # This cell produces no output cell = grid_rnn_cell.GridRNNCell( num_units=2, num_dims=2, input_dims=0, output_dims=None, non_recurrent_dims=0, non_recurrent_fn=nn_ops.relu) g, s = cell(x, m) self.assertEqual(g.get_shape(), (0, 0)) self.assertEqual(s.get_shape(), (1, 4)) sess.run([variables.global_variables_initializer()]) res = sess.run( [g, s], {x: np.array([[1., 1.]]), m: np.array([[0.1, 0.1, 0.1, 0.1]])}) self.assertEqual(res[0].shape, (0, 0)) self.assertEqual(res[1].shape, (1, 4))
def testGrid2LSTMCellReLUWithRNN(self): batch_size = 3 input_size = 5 max_length = 6 # unrolled up to this length num_units = 2 with variable_scope.variable_scope( 'root', initializer=init_ops.constant_initializer(0.5)): cell = grid_rnn_cell.Grid2LSTMCell( num_units=num_units, non_recurrent_fn=nn_ops.relu) inputs = max_length * [ array_ops.placeholder( dtypes.float32, shape=(batch_size, input_size)) ] outputs, state = core_rnn.static_rnn(cell, inputs, dtype=dtypes.float32) self.assertEqual(len(outputs), len(inputs)) self.assertEqual(state.get_shape(), (batch_size, 4)) for out, inp in zip(outputs, inputs): self.assertEqual(out.get_shape()[0], inp.get_shape()[0]) self.assertEqual(out.get_shape()[1], num_units) self.assertEqual(out.dtype, inp.dtype) with self.test_session() as sess: sess.run(variables.global_variables_initializer()) input_value = np.ones((batch_size, input_size)) values = sess.run(outputs + [state], feed_dict={inputs[0]: input_value}) for v in values: self.assertTrue(np.all(np.isfinite(v)))
def testGrid3LSTMCellReLUWithRNN(self): batch_size = 3 input_size = 5 max_length = 6 # unrolled up to this length num_units = 2 with variable_scope.variable_scope( 'root', initializer=init_ops.constant_initializer(0.5)): cell = grid_rnn_cell.Grid3LSTMCell( num_units=num_units, non_recurrent_fn=nn_ops.relu) inputs = max_length * [ array_ops.placeholder( dtypes.float32, shape=(batch_size, input_size)) ] outputs, state = core_rnn.static_rnn(cell, inputs, dtype=dtypes.float32) self.assertEqual(len(outputs), len(inputs)) self.assertEqual(state.get_shape(), (batch_size, 8)) for out, inp in zip(outputs, inputs): self.assertEqual(out.get_shape()[0], inp.get_shape()[0]) self.assertEqual(out.get_shape()[1], num_units) self.assertEqual(out.dtype, inp.dtype) with self.test_session() as sess: sess.run(variables.global_variables_initializer()) input_value = np.ones((batch_size, input_size)) values = sess.run(outputs + [state], feed_dict={inputs[0]: input_value}) for v in values: self.assertTrue(np.all(np.isfinite(v)))
def inference(images, num_classes, is_training=True, scope='alexnet_v2'): """Build Inception v3 model architecture. See here for reference: http://arxiv.org/abs/1512.00567 Args: images: Images returned from inputs() or distorted_inputs(). num_classes: number of classes for_training: If set to `True`, build the inference model for training. Kernels that operate differently for inference during training e.g. dropout, are appropriately configured. restore_logits: whether or not the logits layers should be restored. Useful for fine-tuning a model with different num_classes. scope: optional prefix string identifying the ImageNet tower. Returns: Logits. 2-D float Tensor. Auxiliary Logits. 2-D float Tensor of side-head. Used for training only. """ # Parameters for BatchNorm. batch_norm_params = { # Decay for the moving averages. 'decay': BATCHNORM_MOVING_AVERAGE_DECAY, # epsilon to prevent 0s in variance. 'epsilon': 0.001, # calculate moving average or using exist one 'is_training': is_training } # Set weight_decay for weights in Conv and FC layers. with arg_scope([layers.conv2d, layers.fully_connected], activation_fn=nn_ops.relu, biases_initializer=init_ops.constant_initializer(0.1), weights_regularizer=regularizers.l2_regularizer(FLAGS.weight_decay)): with arg_scope([layers.conv2d], normalizer_fn=layers.batch_norm, normalizer_params=batch_norm_params): logits, endpoints = alexnet_v2( images, num_classes=num_classes, dropout_keep_prob=0.8, is_training=is_training, scope=scope ) # Add summaries for viewing model statistics on TensorBoard. _activation_summaries(endpoints) return logits
def unregularized_loss(self, examples): """Add operations to compute the loss (without the regularization loss). Args: examples: Examples to compute unregularized loss on. Returns: An Operation that computes mean (unregularized) loss for given set of examples. Raises: ValueError: if examples are not well defined. """ self._assertSpecified(['example_labels', 'example_weights', 'sparse_features', 'dense_features'], examples) self._assertList(['sparse_features', 'dense_features'], examples) with name_scope('sdca/unregularized_loss'): predictions = math_ops.cast( self._linear_predictions(examples), dtypes.float64) labels = math_ops.cast( convert_to_tensor(examples['example_labels']), dtypes.float64) weights = math_ops.cast( convert_to_tensor(examples['example_weights']), dtypes.float64) if self._options['loss_type'] == 'logistic_loss': return math_ops.reduce_sum(math_ops.mul( sigmoid_cross_entropy_with_logits(predictions, labels), weights)) / math_ops.reduce_sum(weights) if self._options['loss_type'] in ['hinge_loss', 'smooth_hinge_loss']: # hinge_loss = max{0, 1 - y_i w*x} where y_i \in {-1, 1}. So, we need to # first convert 0/1 labels into -1/1 labels. all_ones = array_ops.ones_like(predictions) adjusted_labels = math_ops.sub(2 * labels, all_ones) # Tensor that contains (unweighted) error (hinge loss) per # example. error = nn_ops.relu(math_ops.sub(all_ones, math_ops.mul(adjusted_labels, predictions))) weighted_error = math_ops.mul(error, weights) return math_ops.reduce_sum(weighted_error) / math_ops.reduce_sum( weights) # squared loss err = math_ops.sub(labels, predictions) weighted_squared_err = math_ops.mul(math_ops.square(err), weights) # SDCA squared loss function is sum(err^2) / (2*sum(weights)) return (math_ops.reduce_sum(weighted_squared_err) / (2.0 * math_ops.reduce_sum(weights)))
def resnet_arg_scope(is_training=True, weight_decay=0.0001, batch_norm_decay=0.997, batch_norm_epsilon=1e-5, batch_norm_scale=True): """Defines the default ResNet arg scope. TODO(gpapan): The batch-normalization related default values above are appropriate for use in conjunction with the reference ResNet models released at https://github.com/KaimingHe/deep-residual-networks. When training ResNets from scratch, they might need to be tuned. Args: is_training: Whether or not we are training the parameters in the batch normalization layers of the model. weight_decay: The weight decay to use for regularizing the model. batch_norm_decay: The moving average decay when estimating layer activation statistics in batch normalization. batch_norm_epsilon: Small constant to prevent division by zero when normalizing activations by their variance in batch normalization. batch_norm_scale: If True, uses an explicit `gamma` multiplier to scale the activations in the batch normalization layer. Returns: An `arg_scope` to use for the resnet models. """ batch_norm_params = { 'is_training': is_training, 'decay': batch_norm_decay, 'epsilon': batch_norm_epsilon, 'scale': batch_norm_scale, 'updates_collections': ops.GraphKeys.UPDATE_OPS, } with arg_scope( [layers_lib.conv2d], weights_regularizer=regularizers.l2_regularizer(weight_decay), weights_initializer=initializers.variance_scaling_initializer(), activation_fn=nn_ops.relu, normalizer_fn=layers.batch_norm, normalizer_params=batch_norm_params): with arg_scope([layers.batch_norm], **batch_norm_params): # The following implies padding='SAME' for pool1, which makes feature # alignment easier for dense prediction tasks. This is also used in # https://github.com/facebook/fb.resnet.torch. However the accompanying # code of 'Deep Residual Learning for Image Recognition' uses # padding='VALID' for pool1. You can switch to that choice by setting # tf.contrib.framework.arg_scope([tf.contrib.layers.max_pool2d], padding='VALID'). with arg_scope([layers.max_pool2d], padding='SAME') as arg_sc: return arg_sc
def unregularized_loss(self, examples): """Add operations to compute the loss (without the regularization loss). Args: examples: Examples to compute unregularized loss on. Returns: An Operation that computes mean (unregularized) loss for given set of examples. Raises: ValueError: if examples are not well defined. """ self._assertSpecified([ 'example_labels', 'example_weights', 'sparse_features', 'dense_features' ], examples) self._assertList(['sparse_features', 'dense_features'], examples) with name_scope('sdca/unregularized_loss'): predictions = math_ops.cast( self._linear_predictions(examples), dtypes.float64) labels = math_ops.cast( internal_convert_to_tensor(examples['example_labels']), dtypes.float64) weights = math_ops.cast( internal_convert_to_tensor(examples['example_weights']), dtypes.float64) if self._options['loss_type'] == 'logistic_loss': return math_ops.reduce_sum(math_ops.multiply( sigmoid_cross_entropy_with_logits(labels=labels, logits=predictions), weights)) / math_ops.reduce_sum(weights) if self._options['loss_type'] in ['hinge_loss', 'smooth_hinge_loss']: # hinge_loss = max{0, 1 - y_i w*x} where y_i \in {-1, 1}. So, we need to # first convert 0/1 labels into -1/1 labels. all_ones = array_ops.ones_like(predictions) adjusted_labels = math_ops.subtract(2 * labels, all_ones) # Tensor that contains (unweighted) error (hinge loss) per # example. error = nn_ops.relu( math_ops.subtract(all_ones, math_ops.multiply(adjusted_labels, predictions))) weighted_error = math_ops.multiply(error, weights) return math_ops.reduce_sum(weighted_error) / math_ops.reduce_sum( weights) # squared loss err = math_ops.subtract(labels, predictions) weighted_squared_err = math_ops.multiply(math_ops.square(err), weights) # SDCA squared loss function is sum(err^2) / (2*sum(weights)) return (math_ops.reduce_sum(weighted_squared_err) / (2.0 * math_ops.reduce_sum(weights)))