Ejemplo n.º 1
0
n_test_batches=test_set_x.get_value(borrow=True).shape[0]/batch_size;

print "[MESSAGE] The data is loaded"

X=T.matrix("data");
y=T.ivector("label");
idx=T.lscalar();

layers=[ReLULayer(in_dim=784, out_dim=50)];

for i in xrange(20):
    layers.append(HighwayReLULayer(in_dim=50));
    
layers.append(SoftmaxLayer(in_dim=50, out_dim=10));

model=FeedForward(layers=layers);
out=model.fprop(X);
cost=categorical_cross_entropy_cost(out[-1], y);
updates=gd_updates(cost=cost, params=model.params, method="sgd", learning_rate=0.01, momentum=0.9);


train=theano.function(inputs=[idx],
                      outputs=cost,
                      updates=updates,
                      givens={X: train_set_x[idx * batch_size: (idx + 1) * batch_size],
                              y: train_set_y[idx * batch_size: (idx + 1) * batch_size]});

test=theano.function(inputs=[idx],
                     outputs=model.layers[-1].error(out[-1], y),
                     givens={X: test_set_x[idx * batch_size: (idx + 1) * batch_size],
                             y: test_set_y[idx * batch_size: (idx + 1) * batch_size]});
Ejemplo n.º 2
0
n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size

print "[MESSAGE] The data is loaded"

X = T.matrix("data")
y = T.ivector("label")
idx = T.lscalar()
dropout_rate = T.fscalar()

layer_0 = ReLULayer(in_dim=784, out_dim=500)
layer_1 = ReLULayer(in_dim=500, out_dim=200)
layer_2 = SoftmaxLayer(in_dim=200, out_dim=10)

dropout = multi_dropout([(batch_size, 784), (batch_size, 500), (batch_size, 200)], dropout_rate)

model = FeedForward(layers=[layer_0, layer_1, layer_2], dropout=dropout)
model_test = FeedForward(layers=[layer_0, layer_1, layer_2])
# model=FeedForward(layers=[layer_0, layer_1, layer_2]);

out = model.fprop(X)
out_test = model_test.fprop(X)
cost = categorical_cross_entropy_cost(out[-1], y)

# Configurations that work
updates = gd_updates(cost=cost, params=model.params, method="sgd", learning_rate=0.01, momentum=0.9)

train = theano.function(
    inputs=[idx, dropout_rate],
    outputs=cost,
    updates=updates,
    givens={
Ejemplo n.º 3
0
n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size

print "[MESSAGE] The data is loaded"

X = T.matrix("data")
y = T.ivector("label")
idx = T.lscalar()

layers = [ReLULayer(in_dim=784, out_dim=50)]

for i in xrange(20):
    layers.append(HighwayReLULayer(in_dim=50))

layers.append(SoftmaxLayer(in_dim=50, out_dim=10))

model = FeedForward(layers=layers)
out = model.fprop(X)
cost = categorical_cross_entropy_cost(out[-1], y)
updates = gd_updates(cost=cost,
                     params=model.params,
                     method="sgd",
                     learning_rate=0.01,
                     momentum=0.9)

train = theano.function(
    inputs=[idx],
    outputs=cost,
    updates=updates,
    givens={
        X: train_set_x[idx * batch_size:(idx + 1) * batch_size],
        y: train_set_y[idx * batch_size:(idx + 1) * batch_size]
Ejemplo n.º 4
0
pool_1 = MaxPooling(pool_size=(2, 2))

flattener = Flattener()

layer_2 = ReLULayer(in_dim=320, out_dim=200)

layer_3 = SoftmaxLayer(in_dim=200, out_dim=10)

#dropout=multi_dropout([(batch_size, 1, 28, 28), None, (batch_size, 50, 11, 11), None, None, None, None], prob=0.5);
dropout = multi_dropout([(batch_size, 1, 28, 28), None,
                         (batch_size, 50, 11, 11), None, None, None, None],
                        prob=0.5)

model = FeedForward(
    layers=[layer_0, pool_0, layer_1, pool_1, flattener, layer_2, layer_3],
    dropout=dropout)

out = model.fprop(images)
cost = categorical_cross_entropy_cost(out[-1], y) + L2_regularization(
    model.params, 0.01)
updates = gd_updates(cost=cost,
                     params=model.params,
                     method="sgd",
                     learning_rate=0.1)

train = theano.function(
    inputs=[idx],
    outputs=cost,
    updates=updates,
    givens={
while (epoch < n_epochs):
    epoch = epoch + 1;
    c = []
    for batch_index in xrange(n_train_batches):
        train_cost=train(batch_index)
        c.append(train_cost);
        
    print 'Training epoch %d, cost ' % epoch, np.mean(c);
    
print "[MESSAGE] The Lyer 0 model is trained"

################################## SECOND LAYER #######################################

## append a max-pooling layer

model_trans=FeedForward(layers=[layer_0_en, MaxPooling(pool_size=(2,2))]);
out_trans=model_trans.fprop(images);

## construct new dConvAE

layer_1_en=ReLUConvLayer(filter_size=(4,4),
                         num_filters=32,
                         num_channels=64,
                         fm_size=(16,16),
                         batch_size=batch_size,
                         border_mode="same");
                                                   
layer_1_de=IdentityConvLayer(filter_size=(4,4),
                             num_filters=64,
                             num_channels=32,
                             fm_size=(16,16),
                                     high=corr_best + 0.1,
                                     size=1).astype("float32")
            max_iter = 0
        else:
            max_iter += 1

    print 'Training epoch %d, cost ' % epoch, np.mean(
        c), corr_best, min_cost, max_iter

print "[MESSAGE] The Lyer 0 model is trained"

################################## SECOND LAYER #######################################

## append a max-pooling layer

model_trans = FeedForward(
    layers=[layer_0_en, MaxPooling(pool_size=(2, 2))])
out_trans = model_trans.fprop(images)

## construct new dConvAE

layer_1_en = ReLUConvLayer(filter_size=(4, 4),
                           num_filters=32,
                           num_channels=64,
                           fm_size=(16, 16),
                           batch_size=batch_size,
                           border_mode="same")

layer_1_de = IdentityConvLayer(filter_size=(4, 4),
                               num_filters=64,
                               num_channels=32,
                               fm_size=(16, 16),
Ejemplo n.º 7
0
                      num_channels=64,
                      fm_size=(16,16),
                      batch_size=batch_size,
                      border_mode="same");

pool_1=MaxPooling(pool_size=(2,2));

flattener=Flattener();

layer_2=ReLULayer(in_dim=32*64,
                  out_dim=800);
                  
layer_3=SoftmaxLayer(in_dim=800,
                     out_dim=10);
                     
model=FeedForward(layers=[layer_0, pool_0, layer_1, pool_1, flattener, layer_2, layer_3]);

out=model.fprop(images);
cost=categorical_cross_entropy_cost(out[-1], y);
updates=gd_updates(cost=cost, params=model.params, method="sgd", learning_rate=0.01, momentum=0.9);

train=theano.function(inputs=[idx],
                      outputs=cost,
                      updates=updates,
                      givens={X: train_set_x[idx * batch_size: (idx + 1) * batch_size],
                              y: train_set_y[idx * batch_size: (idx + 1) * batch_size]});

test=theano.function(inputs=[idx],
                     outputs=model.layers[-1].error(out[-1], y),
                     givens={X: test_set_x[idx * batch_size: (idx + 1) * batch_size],
                             y: test_set_y[idx * batch_size: (idx + 1) * batch_size]});
Ejemplo n.º 8
0
print "[MESSAGE] The data is loaded"

X = T.matrix("data")
y = T.ivector("label")
idx = T.lscalar()
dropout_rate = T.fscalar()

layer_0 = ReLULayer(in_dim=1024, out_dim=500)
layer_1 = ReLULayer(in_dim=500, out_dim=200)
layer_2 = SoftmaxLayer(in_dim=200, out_dim=10)

dropout = multi_dropout([(batch_size, 1024), (batch_size, 500),
                         (batch_size, 200)], dropout_rate)

model = FeedForward(layers=[layer_0, layer_1, layer_2], dropout=dropout)
model_test = FeedForward(layers=[layer_0, layer_1, layer_2])
#model=FeedForward(layers=[layer_0, layer_1, layer_2]);

out = model.fprop(X)
out_test = model_test.fprop(X)
cost = categorical_cross_entropy_cost(out[-1], y)

# Configurations that work
updates = gd_updates(cost=cost,
                     params=model.params,
                     method="sgd",
                     learning_rate=0.01,
                     momentum=0.9)

train = theano.function(