def get_init(features, initializer_class, nhid=5): initializer = initializer_class.build({}, graph=features.graph) if hasattr(initializer, "Input"): initializer.Input.resize(2) initializer.Input[0].connect(features.Output) nvis = features.Output.meta.shape[1] layer = Sigmoid(layer_name="a", irange=0, dim=nhid) # layer needs to be initialized by MLP first MLP(layers=[layer], nvis=nvis) initializer.init_layer(layer, nvis=nvis, nhid=nhid) weights = layer.get_weights() biases = layer.get_biases() return weights, biases
def testLSF(self): init = LeastSquaresWeightInitializer.build({}, graph=Graph()) init.Input.resize(2) init.Input[0].setValue(self.X) init.Input[1].setValue(self.y) for k in (2, 3, 5, 7): layer = Sigmoid(layer_name="a", irange=0, dim=k) # layer needs to be initialized by MLP first MLP(layers=[layer], nvis=self.d) np.random.seed(123) init.init_layer(layer, nvis=self.d, nhid=k) weights = layer.get_weights() np.testing.assert_array_equal(weights.shape, (self.d, k)) assert np.abs(weights).sum() > 0
def testPCA(self): init = PCAWeightInitializer.build({}, graph=Graph()) init.Input.resize(2) init.Input[0].setValue(self.X) init.Input[1].setValue(self.y) for k in (2, 4, 6, 8): layer = Sigmoid(layer_name="a", irange=0, dim=k) # layer needs to be initialized by MLP first MLP(layers=[layer], nvis=self.d) np.random.seed(123) init.init_layer(layer, nvis=self.d, nhid=k) weights = layer.get_weights() np.testing.assert_array_equal(weights.shape, (self.d, k)) assert np.abs(weights).sum() > 0 if k <= 2*self.d: for i in range(k): for j in range(i+2, k, 2): dot = np.dot(weights[:, i], weights[:, j]) np.testing.assert_almost_equal(dot, 0, decimal=4)