Caffe2 - Python API
A deep learning, cross platform ML framework
add_bias.py
1 ## @package add_bias
2 # Module caffe2.python.layers.add_bias
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 schema
9 from caffe2.python.layers.layers import ModelLayer
10 import math
11 
12 
14 
15  def __init__(self, model, input_record, bias_init=None,
16  bias_optim=None, name='add_bias'):
17  super(AddBias, self).__init__(model, name, input_record)
18  assert isinstance(input_record, schema.Scalar), "Incorrect input type"
19  assert len(input_record.field_type().shape) > 0, (
20  "AddBias expects limited dimensions of the input tensor")
21 
22  input_dims = input_record.field_type().shape[0]
23  assert input_dims > 0, (
24  "AddBias expects input dimensions > 0, got {}".format(input_dims))
25 
26  scale = math.sqrt(1.0 / input_dims)
27  bias_init = bias_init if bias_init else (
28  'UniformFill', {'min': -scale, 'max': scale})
29 
30  self.b = self.create_param(
31  param_name='b',
32  shape=[input_dims, ],
33  initializer=bias_init,
34  optimizer=bias_optim,
35  )
36 
38  (input_record.field_type().base, (input_dims, )),
39  self.get_next_blob_reference('output')
40  )
41 
42  def add_ops(self, net):
43  net.Add(self.input_record.field_blobs() + [self.b],
44  self.output_schema.field_blobs(), broadcast=1)
def get_next_blob_reference(self, name)
Definition: layers.py:349
def create_param(self, param_name, shape, initializer, optimizer, ps_param=None, regularizer=None)
Definition: layers.py:334