Caffe2 - Python API
A deep learning, cross platform ML framework
batch_sigmoid_cross_entropy_loss.py
1 # Copyright (c) 2016-present, Facebook, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 ##############################################################################
15 
16 ## @package batch_sigmoid_cross_entropy_loss
17 # Module caffe2.python.layers.batch_sigmoid_cross_entropy_loss
18 from __future__ import absolute_import
19 from __future__ import division
20 from __future__ import print_function
21 from __future__ import unicode_literals
22 
23 from caffe2.python import schema
24 from caffe2.python.layers.layers import ModelLayer
25 from caffe2.python.layers.tags import Tags
26 import numpy as np
27 
28 
30  def __init__(
31  self,
32  model,
33  input_record,
34  name='batch_sigmoid_cross_entropy_loss',
35  **kwargs
36  ):
37  super(BatchSigmoidCrossEntropyLoss, self).__init__(
38  model, name, input_record, **kwargs)
39 
40  assert schema.is_schema_subset(
42  ('label', schema.Scalar(np.float32)),
43  ('prediction', schema.Scalar(np.float32)),
44  ),
45  input_record
46  )
47  assert input_record.prediction.field_type().shape == \
48  input_record.label.field_type().shape, \
49  "prediction and label must have the same shape"
50 
51  self.tags.update([Tags.EXCLUDE_FROM_PREDICTION])
52 
54  (np.float32, tuple()), self.get_next_blob_reference('loss')
55  )
56 
57  def add_ops(self, net):
58  sigmoid_cross_entropy = net.SigmoidCrossEntropyWithLogits(
59  [self.input_record.prediction(), self.input_record.label()],
60  net.NextScopedBlob('sigmoid_cross_entropy')
61  )
62 
63  net.AveragedLoss(
64  sigmoid_cross_entropy, self.output_schema.field_blobs())
def get_next_blob_reference(self, name)
Definition: layers.py:352