def test_batchnorm_infer(): layer = BatchNormalization(input_dim = (4, 5, 5)) layer.accumulate = True layer.initialize() x = T.tensor4("features") x_val = [np.ones((6, 4, 5, 5), dtype=theano.config.floatX) for _ in range(2)] x_val[0][0,0,0,0] = 10.0 x_val[1][0,0,0,0] = -200.0 y = layer.apply(x) dataset = IterableDataset(dict(features=x_val)) data_stream = DataStream(dataset) cg = ComputationGraph([y]) infer_population(data_stream, cg, 2) assert layer.use_population == True assert_allclose(layer.u.get_value(), np.array([0.72, 2, 2, 2])) assert_allclose(layer.n.get_value(), np.array([2]))
def test_batchnorm_get_set(): layer = BatchNormalization(input_dim = (4, 5, 5)) layer.accumulate = True layer.initialize() x = T.tensor4("features") x_val = [np.ones((6, 4, 5, 5), dtype=theano.config.floatX) for _ in range(2)] x_val[0][0,0,0,0] = 10.0 x_val[1][0,0,0,0] = -200.0 y = layer.apply(x) dataset = IterableDataset(dict(features=x_val)) data_stream = DataStream(dataset) cg = Model([y]) infer_population(data_stream, cg, 2) values_dict = get_batchnorm_parameter_values(cg) assert len(values_dict.keys()) == 3 assert_allclose(values_dict['/batchnormalization.n'], np.array([2])) values_dict['/batchnormalization.n'] = np.array([5.], dtype="float32") set_batchnorm_parameter_values(cg, values_dict) assert_allclose(layer.n.get_value(), np.array([5.]))
def test_batchnorm_infer(): layer = BatchNormalization(input_dim=(4, 5, 5)) layer.accumulate = True layer.initialize() x = T.tensor4("features") x_val = [ np.ones((6, 4, 5, 5), dtype=theano.config.floatX) for _ in range(2) ] x_val[0][0, 0, 0, 0] = 10.0 x_val[1][0, 0, 0, 0] = -200.0 y = layer.apply(x) dataset = IterableDataset(dict(features=x_val)) data_stream = DataStream(dataset) cg = ComputationGraph([y]) infer_population(data_stream, cg, 2) assert layer.use_population == True assert_allclose(layer.u.get_value(), np.array([0.72, 2, 2, 2])) assert_allclose(layer.n.get_value(), np.array([2]))
def test_batchnorm_get_set(): layer = BatchNormalization(input_dim=(4, 5, 5)) layer.accumulate = True layer.initialize() x = T.tensor4("features") x_val = [ np.ones((6, 4, 5, 5), dtype=theano.config.floatX) for _ in range(2) ] x_val[0][0, 0, 0, 0] = 10.0 x_val[1][0, 0, 0, 0] = -200.0 y = layer.apply(x) dataset = IterableDataset(dict(features=x_val)) data_stream = DataStream(dataset) cg = Model([y]) infer_population(data_stream, cg, 2) values_dict = get_batchnorm_parameter_values(cg) assert len(values_dict.keys()) == 3 assert_allclose(values_dict['/batchnormalization.n'], np.array([2])) values_dict['/batchnormalization.n'] = np.array([5.], dtype="float32") set_batchnorm_parameter_values(cg, values_dict) assert_allclose(layer.n.get_value(), np.array([5.]))