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