Caffe2 - Python API
A deep learning, cross platform ML framework
add_bias.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 add_bias
17 # Module caffe2.python.layers.add_bias
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 import math
26 
27 
29 
30  def __init__(self, model, input_record, bias_init=None,
31  bias_optim=None, name='add_bias'):
32  super(AddBias, self).__init__(model, name, input_record)
33  assert isinstance(input_record, schema.Scalar), "Incorrect input type"
34  assert len(input_record.field_type().shape) > 0, (
35  "AddBias expects limited dimensions of the input tensor")
36 
37  input_dims = input_record.field_type().shape[0]
38  assert input_dims > 0, (
39  "AddBias expects input dimensions > 0, got {}".format(input_dims))
40 
41  scale = math.sqrt(1.0 / input_dims)
42  bias_init = bias_init if bias_init else (
43  'UniformFill', {'min': -scale, 'max': scale})
44 
45  self.b = self.create_param(
46  param_name='b',
47  shape=[input_dims, ],
48  initializer=bias_init,
49  optimizer=bias_optim,
50  )
51 
53  (input_record.field_type().base, (input_dims, )),
54  self.get_next_blob_reference('output')
55  )
56 
57  def add_ops(self, net):
58  net.Add(self.input_record.field_blobs() + [self.b],
59  self.output_schema.field_blobs(), broadcast=1)
def get_next_blob_reference(self, name)
Definition: layers.py:352
def create_param(self, param_name, shape, initializer, optimizer, ps_param=None, regularizer=None)
Definition: layers.py:337