Caffe2 - Python API
A deep learning, cross platform ML framework
fc_without_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 fc_without_bias
17 # Module caffe2.python.layers.fc_without_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 from caffe2.python.layers.sampling_trainable_mixin import SamplingTrainableMixin
26 
27 import math
28 import numpy as np
29 
30 
32  def __init__(
33  self,
34  model,
35  input_record,
36  output_dims,
37  weight_init=None,
38  weight_optim=None,
39  name='fc_without_bias',
40  **kwargs
41  ):
42  super(FCWithoutBias, self).__init__(model, name, input_record, **kwargs)
43  assert isinstance(input_record, schema.Scalar), "Incorrect input type"
44  assert len(input_record.field_types()[0].shape) > 0, (
45  "FCWithoutBias expects limited dimensions of the input tensor"
46  )
47 
48  input_dims = input_record.field_types()[0].shape[0]
49  assert input_dims > 0, (
50  "FCWithoutBias expects input dimensions > 0, got {}".format(input_dims)
51  )
52 
54  (np.float32, (output_dims, )),
55  self.get_next_blob_reference('output')
56  )
57 
58  scale = math.sqrt(1.0 / input_dims)
59  weight_init = weight_init if weight_init else (
60  'UniformFill', {'min': -scale,
61  'max': scale}
62  )
63 
64  self.w = self.create_param(param_name='w',
65  shape=[output_dims, input_dims],
66  initializer=weight_init,
67  optimizer=weight_optim)
68 
69  def _add_ops(self, net, params):
70  net.MatMul(
71  self.input_record.field_blobs() + params,
72  self.output_schema.field_blobs(), trans_b=1, **self.kwargs
73  )
74 
75  @property
76  def param_blobs(self):
77  return [self.w]
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