Caffe2 - Python API
A deep learning, cross platform ML framework
parameter_info.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 from __future__ import absolute_import
17 from __future__ import division
18 from __future__ import print_function
19 from __future__ import unicode_literals
20 
21 from caffe2.python import core
22 
23 import numpy as np
24 
25 
26 class ParameterTags(object):
27  BIAS = 'BIAS'
28  WEIGHT = 'WEIGHT'
29  COMPUTED_PARAM = 'COMPUTED_PARAM'
30 
31 
32 class ParameterType(object):
33  DENSE = 'dense'
34  SPARSE = 'sparse'
35 
36 
37 class ParameterInfo(object):
38 
39  def __init__(
40  self, param_id, param, key=None, shape=None, length=None,
41  grad=None, blob_copy=None):
42  assert isinstance(param, core.BlobReference)
43  self.param_id = param_id
44  self.name = str(param)
45  self.blob = param
46  self.key = key
47  self.shape = shape
48  self.size = None if shape is None else np.prod(shape)
49  self.length = max(1, length if length is not None else 1)
50  self.grad = grad
51  self._cloned_init_net = None
52  # Optionally store equivalent copies of the blob
53  # in different precisions (i.e. half and float copies)
54  # stored as a dict of TensorProto.DataType -> BlobReference
55  self.blob_copy = blob_copy
56  # each param_info can have its own optimizer. It can be set within
57  # OptimizerContext (caffe2/python/optimizer.py)
58  self._optimizer = None
59 
60  def grad_type(self):
61  # self.grad could be None for model parallelism with parameter server
62  if self.grad is None:
63  return
64  return (
65  ParameterType.SPARSE if isinstance(self.grad, core.GradientSlice)
66  else ParameterType.DENSE)
67 
68  @property
69  def parameter(self):
70  return self.blob
71 
72  @property
73  def optimizer(self):
74  return self._optimizer
75 
76  @optimizer.setter
77  def optimizer(self, value):
78  assert self._optimizer is None, "optimizer has already been set"
79  self._optimizer = value
80 
81  def __str__(self):
82  return self.name
Module caffe2.python.optimizer.