1 from __future__
import absolute_import
2 from __future__
import division
3 from __future__
import print_function
4 from __future__
import unicode_literals
13 Implementation of the semi-random kernel feature map. 15 Applies H(x_rand) * x_rand^s * x_learned, where 16 H is the Heaviside step function, 17 x_rand is the input after applying FC with randomized parameters, 18 and x_learned is the input after applying FC with learnable parameters. 20 If using multilayer model with semi-random layers, then input and output records 21 should have a 'full' and 'random' Scalar. The random Scalar will be passed as 22 input to process the random features. 24 For more information, see the original paper: 25 https://arxiv.org/pdf/1702.08882.pdf 28 output_dims -- dimensions of the output vector 29 s -- if s == 0, will obtain linear semi-random features; 30 else if s == 1, will obtain squared semi-random features; 31 else s >= 2, will obtain higher order semi-random features 32 scale_random -- amount to scale the standard deviation 33 (for random parameter initialization when weight_init or 34 bias_init hasn't been specified) 35 scale_learned -- amount to scale the standard deviation 36 (for learned parameter initialization when weight_init or 37 bias_init hasn't been specified) 39 weight_init_random -- initialization distribution for random weight parameter 40 (if None, will use Gaussian distribution) 41 bias_init_random -- initialization distribution for random bias pararmeter 42 (if None, will use Uniform distribution) 43 weight_init_learned -- initialization distribution for learned weight parameter 44 (if None, will use Gaussian distribution) 45 bias_init_learned -- initialization distribution for learned bias pararmeter 46 (if None, will use Uniform distribution) 47 weight_optim -- optimizer for weight params for learned features 48 bias_optim -- optimizer for bias param for learned features 50 set_weight_as_global_constant -- if True, initialized random parameters 51 will be constant across all distributed 52 instances of the layer 62 weight_init_random=
None,
63 bias_init_random=
None,
64 weight_init_learned=
None,
65 bias_init_learned=
None,
68 set_weight_as_global_constant=
False,
69 name=
'semi_random_features',
73 schema.is_schema_subset(
87 super(SemiRandomFeatures, self).__init__(
93 weight_init=weight_init_random,
94 bias_init=bias_init_random,
97 set_weight_as_global_constant=set_weight_as_global_constant,
98 initialize_output_schema=
False,
104 (np.float32, output_dims),
105 model.net.NextScopedBlob(name +
'_full_output')
108 (np.float32, output_dims),
109 model.net.NextScopedBlob(name +
'_random_output')
114 assert (scale_learned > 0.0), \
115 "Expected scale (learned) > 0, got %s" % scale_learned
122 w_init=weight_init_learned,
123 b_init=bias_init_learned,
124 w_optim=weight_optim,
128 def add_ops(self, net):
130 learned_features = net.FC(self.input_record_full.field_blobs() +
131 [self.learned_w, self.learned_b],
132 net.NextScopedBlob(
'learned_features'))
134 random_features = net.FC(self.input_record_random.field_blobs() +
136 net.NextScopedBlob(
'random_features'))
140 self.output_schema.random.field_blobs(),
143 net.Mul([processed_random_features, learned_features],
144 self.output_schema.full.field_blobs())
def _heaviside_with_power(self, net, input_features, output_blob, s)
def _initialize_params(self, w_name, b_name, w_init=None, b_init=None, w_optim=None, b_optim=None)