Caffe2 - Python API
A deep learning, cross platform ML framework
batch_softmax_loss.py
1 
3 from __future__ import absolute_import
4 from __future__ import division
5 from __future__ import print_function
6 from __future__ import unicode_literals
7 
8 from caffe2.python import core, schema
9 from caffe2.python.layers.layers import ModelLayer
10 from caffe2.python.layers.tags import Tags
11 import numpy as np
12 
13 
14 class BatchSoftmaxLoss(ModelLayer):
15  def __init__(
16  self,
17  model,
18  input_record,
19  name='batch_softmax_loss',
20  **kwargs
21  ):
22  super(BatchSoftmaxLoss, self).__init__(
23  model, name, input_record, **kwargs)
24 
27  ('label', schema.Scalar()),
28  ('prediction', schema.Scalar()),
29  ),
30  input_record
31  )
32  self.tags.update({Tags.TRAIN_ONLY})
33 
35  (
36  'softmax', schema.Scalar(
37  input_record.prediction.field_type(),
38  model.net.NextScopedBlob(name + '_softmax')
39  )
40  ),
41  (
42  'loss', schema.Scalar(
43  np.float32, model.net.NextScopedBlob(name + '_loss')
44  )
45  ),
46  )
47 
48  def add_ops(self, net):
49  label = self.input_record.label.field_blobs()
50  if self.input_record.label.field_types()[0].base != np.int32:
51  label = [
52  net.Cast(label,
53  net.NextScopedBlob('int32_label'),
54  to=core.DataType.INT32)
55  ]
56 
57  net.SoftmaxWithLoss(
58  self.input_record.prediction.field_blobs() + label,
59  self.output_schema.field_blobs()
60  )
def is_schema_subset(schema, original_schema)
Definition: schema.py:985