def _build_model(): r = xnn.nonlinearities.rectify s = xnn.nonlinearities.scale m2 = Model("test convenience") l_in = m2.make_bound_input_layer((10, 1, 20, 10), "pixels") l_in2 = m2.make_bound_input_layer((10, 1, 20, 10), "pixels") l_conv = m2.add_layer(xnn.layers.Conv2DLayer(l_in2, 3, 4), name="l_conv") l_den = m2.make_dense_drop_stack( l_in, [60, 3, 2], [0.6, 0.4, 0.3], drop_type_list=["gauss", "gauss", "standard"], nonlin_list=[r, s(2.0), r] ) l_reshp = m2.add_layer(xnn.layers.ReshapeLayer(l_conv, (10, -1))) l_mer = xnn.layers.ConcatLayer([l_reshp, l_den]) m2.add_layer(l_mer, name="merger") l_bn = xnn.layers.BatchNormLayer(l_in2, nonlinearity=xnn.nonlinearities.sigmoid) m2.add_layer(l_bn) l_loc = xnn.layers.LocalLayer(l_bn, num_units=32, img_shape=(20, 10), local_filters=[(2, 1)], seed=121212) np.random.seed(int(time.time() * 10000 % 10000)) m2.add_layer(l_loc) l_out = m2.add_layer(xnn.layers.DenseLayer(l_loc, num_units=2, nonlinearity=xnn.nonlinearities.linear), name="out") l_pr = m2.add_layer(xnn.layers.PReLULayer(l_out, xnn.init.Constant(0.25))) m2.bind_output(l_pr, xnn.objectives.squared_error, "age", "label", "mean", is_eval_output=True, scale=2) l_out2 = m2.add_layer( xnn.layers.DenseLayer(l_loc, num_units=2, nonlinearity=xnn.nonlinearities.softmax), name="out2" ) m2.bind_output(l_out2, xnn.objectives.hinge_loss(threshold=2), "age", "label", "mean", is_eval_output=False) return m2
def test_aggregation(): batch_size = 128 img_size = 10 num_out = 3 m = Model('test model cpu') l_in = m.add_layer(layers.InputLayer(shape=(batch_size,img_size)), name="l_in") l_out_mean = m.add_layer(layers.DenseLayer(l_in, num_out), name="l_out_mean") l_out_sum = m.add_layer(layers.DenseLayer(l_in, num_out), name="l_out_sum") m.bind_input(l_in, "pixels") m.bind_output(l_out_mean, lasagne.objectives.squared_error, "emotions", "label", "mean") m.bind_output(l_out_sum, lasagne.objectives.squared_error, "emotions", "label", "sum") from pprint import pprint pprint(m.to_dict()) global_update_settings = ParamUpdateSettings(update=lasagne.updates.nesterov_momentum,learning_rate=0.1, momentum=0.5) trainer = Trainer(m, global_update_settings) pixels = np.random.rand(batch_size,img_size).astype(theano.config.floatX) emotions = np.random.rand(batch_size,num_out).astype(theano.config.floatX) batch_dict = dict( # learning_rate_default=0.1, # momentum_default=0.5, pixels=pixels, emotions=emotions, exmotions=emotions.copy() ) outs = trainer.train_step(batch_dict) print "Aggregation test succeeded"
def _build_lr_net(batch_size, in_size, out_size): np.random.seed(100) m = Model() l_in = m.add_layer(InputLayer(shape=(batch_size,in_size))) l_out = m.add_layer(DenseLayer(l_in, out_size, nonlinearity=softmax)) m.bind_input(l_in, "inputs") m.bind_output(l_out, squared_error, "labels", "label", "mean") return m, l_in, l_out
def _build_lr_net(batch_size, in_size, out_size): np.random.seed(100) m = Model() l_in = m.add_layer(InputLayer(shape=(batch_size, in_size))) l_out = m.add_layer(DenseLayer(l_in, out_size, nonlinearity=softmax)) m.bind_input(l_in, "inputs") m.bind_output(l_out, squared_error, "labels", "label", "mean") return m, l_in, l_out
def test_build_model(): m = Model("test model") l_in = m.add_layer(xnn.layers.InputLayer(shape=(10, 200)), name="l_in") l_h1 = m.add_layer(xnn.layers.DenseLayer(l_in, 100), name="l_h1") l_out = m.add_layer(xnn.layers.DenseLayer(l_h1, 200), name="l_out") m.bind_input(l_in, "pixels") m.bind_output(l_h1, xnn.objectives.categorical_crossentropy, "emotions", "label", "mean") m.bind_output(l_out, xnn.objectives.squared_error, "l_in", "recon", "mean")
def test_build_model(): m = Model('test model') l_in = m.add_layer(xnn.layers.InputLayer(shape=(10, 200)), name="l_in") l_h1 = m.add_layer(xnn.layers.DenseLayer(l_in, 100), name="l_h1") l_out = m.add_layer(xnn.layers.DenseLayer(l_h1, 200), name="l_out") m.bind_input(l_in, "pixels") m.bind_output(l_h1, xnn.objectives.categorical_crossentropy, "emotions", "label", "mean") m.bind_output(l_out, xnn.objectives.squared_error, "l_in", "recon", "mean")
def _build_model(batch_size,img_size,num_hid): m = Model('test model cpu') l_in = m.add_layer(layers.InputLayer(shape=(batch_size,img_size)), name="l_in") l_loc = m.add_layer(layers.LocalLayer(l_in,num_units=3,img_shape=(2,5),local_filters=[(2,1)])) l_h1 = m.add_layer(layers.DenseLayer(l_loc, num_hid), name="l_h1") l_out = m.add_layer(layers.DenseLayer(l_h1, img_size), name="l_out") m.bind_input(l_in, "pixels") m.bind_output(l_h1, xnn.objectives.kl_divergence, "emotions", "label", "mean") m.bind_output(l_out, xnn.objectives.squared_error, "l_in", "recon", "mean") return m
def _build_model(batch_size, img_size, num_hid): m = Model('test model cpu') l_in = m.add_layer(layers.InputLayer(shape=(batch_size, img_size)), name="l_in") l_loc = m.add_layer( layers.LocalLayer(l_in, num_units=3, img_shape=(2, 5), local_filters=[(2, 1)])) l_h1 = m.add_layer(layers.DenseLayer(l_loc, num_hid), name="l_h1") l_out = m.add_layer(layers.DenseLayer(l_h1, img_size), name="l_out") m.bind_input(l_in, "pixels") m.bind_output(l_h1, xnn.objectives.kl_divergence, "emotions", "label", "mean") m.bind_output(l_out, xnn.objectives.squared_error, "l_in", "recon", "mean") return m
def _build_model(): r = xnn.nonlinearities.rectify s = xnn.nonlinearities.scale m2 = Model('test convenience') l_in = m2.make_bound_input_layer((10, 1, 20, 10), 'pixels') l_in2 = m2.make_bound_input_layer((10, 1, 20, 10), 'pixels') l_conv = m2.add_layer(xnn.layers.Conv2DLayer(l_in2, 3, 4), name='l_conv') l_den = m2.make_dense_drop_stack( l_in, [60, 3, 2], [.6, .4, .3], drop_type_list=['gauss', 'gauss', 'standard'], nonlin_list=[r, s(2.), r]) l_reshp = m2.add_layer(xnn.layers.ReshapeLayer(l_conv, (10, -1))) l_mer = xnn.layers.ConcatLayer([l_reshp, l_den]) m2.add_layer(l_mer, name='merger') l_bn = xnn.layers.BatchNormLayer(l_in2, nonlinearity=xnn.nonlinearities.sigmoid) m2.add_layer(l_bn) l_loc = xnn.layers.LocalLayer(l_bn, num_units=32, img_shape=(20, 10), local_filters=[(2, 1)], seed=121212) np.random.seed(int(time.time() * 10000 % 10000)) m2.add_layer(l_loc) l_out = m2.add_layer(xnn.layers.DenseLayer( l_loc, num_units=2, nonlinearity=xnn.nonlinearities.linear), name='out') l_pr = m2.add_layer(xnn.layers.PReLULayer(l_out, xnn.init.Constant(.25))) m2.bind_output(l_pr, xnn.objectives.squared_error, 'age', 'label', 'mean', is_eval_output=True, scale=2) l_out2 = m2.add_layer(xnn.layers.DenseLayer( l_loc, num_units=2, nonlinearity=xnn.nonlinearities.softmax), name='out2') m2.bind_output(l_out2, xnn.objectives.hinge_loss(threshold=2), 'age', 'label', 'mean', is_eval_output=False) return m2
def test_aggregation(): batch_size = 128 img_size = 10 num_out = 3 m = Model('test model cpu') l_in = m.add_layer(layers.InputLayer(shape=(batch_size, img_size)), name="l_in") l_out_mean = m.add_layer(layers.DenseLayer(l_in, num_out), name="l_out_mean") l_out_sum = m.add_layer(layers.DenseLayer(l_in, num_out), name="l_out_sum") m.bind_input(l_in, "pixels") m.bind_output(l_out_mean, lasagne.objectives.squared_error, "emotions", "label", "mean") m.bind_output(l_out_sum, lasagne.objectives.squared_error, "emotions", "label", "sum") from pprint import pprint pprint(m.to_dict()) global_update_settings = ParamUpdateSettings( update=lasagne.updates.nesterov_momentum, learning_rate=0.1, momentum=0.5) trainer = Trainer(m, global_update_settings) pixels = np.random.rand(batch_size, img_size).astype(theano.config.floatX) emotions = np.random.rand(batch_size, num_out).astype(theano.config.floatX) batch_dict = dict( # learning_rate_default=0.1, # momentum_default=0.5, pixels=pixels, emotions=emotions, exmotions=emotions.copy()) outs = trainer.train_step(batch_dict) print "Aggregation test succeeded"