Caffe2 - Python API
A deep learning, cross platform ML framework
elementwise_linear.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 elementwise_linear
17 # Module caffe2.python.helpers.elementwise_linear
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 core
24 from caffe2.python.modeling.parameter_info import ParameterTags
25 
26 
27 def _elementwise_linear(
28  model, op_call, blob_in, blob_out, dim,
29  weight_init=None, bias_init=None, **kwargs
30 ):
31  """Elementwise_Linear"""
32  weight_init = weight_init or ('ConstantFill', {'value': 1.0})
33  bias_init = bias_init or ('ConstantFill', {'value': 0.0})
34  blob_out = blob_out or model.net.NextName()
35  if model.init_params:
36  weight = model.param_init_net.__getattr__(weight_init[0])(
37  [],
38  blob_out + '_w',
39  shape=[dim],
40  **weight_init[1]
41  )
42  bias = model.param_init_net.__getattr__(bias_init[0])(
43  [],
44  blob_out + '_b',
45  shape=[dim],
46  **bias_init[1]
47  )
48  else:
49  weight = core.ScopedBlobReference(
50  blob_out + '_w', model.param_init_net)
51  bias = core.ScopedBlobReference(
52  blob_out + '_b', model.param_init_net)
53 
54  model.AddParameter(weight, ParameterTags.WEIGHT)
55  model.AddParameter(bias, ParameterTags.BIAS)
56  return op_call([blob_in, weight, bias], blob_out, **kwargs)
57 
58 
59 def elementwise_linear(model, *args, **kwargs):
60  return _elementwise_linear(
61  model, model.net.ElementwiseLinear, *args, **kwargs)
Module caffe2.python.helpers.elementwise_linear.