1 from __future__
import absolute_import
2 from __future__
import division
3 from __future__
import print_function
4 from __future__
import unicode_literals
13 A general version of the arc-cosine kernel feature map (s = 1 restores 14 the original arc-cosine kernel feature map). 16 Applies H(x) * x^s, where H is the Heaviside step function and x is the 17 input after applying FC (such that x = w * x_orig + b). 19 For more information, see the original paper: 20 http://cseweb.ucsd.edu/~saul/papers/nips09_kernel.pdf 23 output_dims -- dimensions of the output vector 24 s -- degree to raise transformed features 25 scale -- amount to scale the standard deviation 26 weight_init -- initialization distribution for weight parameter 27 bias_init -- initialization distribution for bias pararmeter 28 weight_optim -- optimizer for weight params; None for random features 29 bias_optim -- optimizer for bias param; None for random features 30 set_weight_as_global_constant -- if True, initialized random parameters 31 will be constant across all distributed 32 instances of the layer 33 initialize_output_schema -- if True, initialize output schema as Scalar 34 from Arc Cosine; else output schema is None 47 set_weight_as_global_constant=
False,
48 initialize_output_schema=
True,
49 name=
'arc_cosine_feature_map',
52 super(ArcCosineFeatureMap, self).__init__(model, name, input_record,
54 assert isinstance(input_record,
schema.Scalar),
"Incorrect input type" 59 self.
input_dims = input_record.field_type().shape[0]
60 assert self.
input_dims >= 1,
"Expected input dimensions >= 1, got %s" \
63 if initialize_output_schema:
65 (np.float32, (output_dims, )),
66 model.net.NextScopedBlob(name +
'_output')
70 assert self.
output_dims >= 1,
"Expected output dimensions >= 1, got %s" \
73 assert (self.
s >= 0),
"Expected s >= 0, got %s" % self.
s 74 assert isinstance(self.
s, int),
"Expected s to be type int, got type %s" \
77 assert (scale > 0.0),
"Expected scale > 0, got %s" % scale
82 if set_weight_as_global_constant:
83 w_init = np.random.normal(scale=self.
stddev,
85 b_init = np.random.uniform(low=-0.5 * self.
stddev,
88 self.
random_w = self.model.add_global_constant(
89 name=self.
name +
"_fixed_rand_W",
92 self.
random_b = self.model.add_global_constant(
93 name=self.
name +
"_fixed_rand_b",
102 w_optim=weight_optim,
106 def _initialize_params(self, w_name, b_name, w_init=None, b_init=None,
107 w_optim=
None, b_optim=
None):
109 Initializes the Layer Parameters for weight and bias terms for features 112 w_blob -- blob to contain w values 113 b_blob -- blob to contain b values 114 w_init -- initialization distribution for weight parameter 115 b_init -- initialization distribution for bias parameter 116 w_optim -- optimizer to use for w; if None, then will use no optimizer 117 b_optim -- optimizer to user for b; if None, then will use no optimizer 120 w_init = w_init
if w_init
else (
121 'GaussianFill', {
'mean': 0.0,
'std': self.
stddev}
123 w_optim = w_optim
if w_optim
else self.model.NoOptim
125 b_init = b_init
if b_init
else (
126 'UniformFill', {
'min': -0.5 * self.
stddev,
'max': 0.5 * self.
stddev}
128 b_optim = b_optim
if b_optim
else self.model.NoOptim
140 return [w_param, b_param]
142 def _heaviside_with_power(self, net, input_features, output_blob, s):
144 Applies Heaviside step function and Relu / exponentiation to features 145 depending on the value of s. 148 net -- net with operators 149 input_features -- features to processes 150 output_blob -- output blob reference 151 s -- degree to raise the transformed features 154 softsign_features = net.Softsign([input_features],
155 net.NextScopedBlob(
'softsign'))
156 return net.Relu(softsign_features, output_blob)
158 return net.Relu([input_features],
161 relu_features = net.Relu([input_features],
162 net.NextScopedBlob(
'relu_rand'))
163 pow_features = net.Pow([input_features],
164 net.NextScopedBlob(
'pow_rand'),
165 exponent=float(s - 1))
166 return net.Mul([relu_features, pow_features],
169 def add_ops(self, net):
170 input_blob = self.input_record.field_blobs()
174 net.NextScopedBlob(
'random_features'))
178 self.output_schema.field_blobs(),
set_weight_as_global_constant
def _heaviside_with_power(self, net, input_features, output_blob, s)
def create_param(self, param_name, shape, initializer, optimizer, ps_param=None, regularizer=None)
def _initialize_params(self, w_name, b_name, w_init=None, b_init=None, w_optim=None, b_optim=None)