コード例 #1
0
def AutoEncoder_demo(learning_rate=0.1,
                     training_epochs=2,
                     dataset='mnist.pkl.gz',
                     batch_size=20,
                     output_folder='dA_plots'):

    datasets = load_data(dataset)
    train_set_x, train_set_y = datasets[0]
    n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
    index = T.lscalar()  # index to a [mini]batch
    x = T.matrix('x')

    if not os.path.isdir(output_folder):
        os.makedirs(output_folder)
    os.chdir(output_folder)

    #####################################
    # BUILDING THE MODEL CORRUPTION 0% #
    #####################################
    rng = numpy.random.RandomState(123)
    theano_rng = RandomStreams(rng.randint(2**30))

    da = AutoEncoder(np_rng=rng,
                     theano_rng=theano_rng,
                     input=x,
                     n_vis=28 * 28,
                     n_hid=500)
    cost, updates = da.get_cost_updates(corruption_level=0.,
                                        learning_rate=learning_rate)
    train_da = theano.function(
        inputs=[index],
        outputs=[cost],
        updates=updates,
        givens={x: train_set_x[index * batch_size:(index + 1) * batch_size]})

    start_time = time.clock()
    for epoch in xrange(training_epochs):
        # go through trainng set
        c = []
        for batch_index in xrange(n_train_batches):
            c.append(train_da(batch_index))

        print 'Training epoch %d, cost ' % epoch, numpy.mean(c)
    end_time = time.clock()

    training_time = (end_time - start_time)
    print >> sys.stderr, ('The no corruption code ran for %.2fm' %
                          ((training_time) / 60.))
    image = PIL.Image.fromarray(
        tile_raster_images(X=da.W.get_value(borrow=True).T,
                           img_shape=(28, 28),
                           tile_shape=(10, 10),
                           tile_spacing=(1, 1)))
    image.save('filters_corruption_0.jpg')

    #####################################
    # BUILDING THE MODEL CORRUPTION 30% #
    #####################################
    rng = numpy.random.RandomState(123)
    theano_rng = RandomStreams(rng.randint(2**30))

    da = AutoEncoder(np_rng=rng,
                     theano_rng=theano_rng,
                     input=x,
                     n_vis=28 * 28,
                     n_hid=500)
    cost, updates = da.get_cost_updates(corruption_level=0.3,
                                        learning_rate=learning_rate)
    train_da = theano.function(
        inputs=[index],
        outputs=[cost],
        updates=updates,
        givens={x: train_set_x[index * batch_size:(index + 1) * batch_size]})

    start_time = time.clock()
    for epoch in xrange(training_epochs):
        c = []
        for batch_index in xrange(n_train_batches):
            c.append(train_da(batch_index))

        print 'Training epoch %d, cost ' % epoch, numpy.mean(c)
    end_time = time.clock()

    training_time = (end_time - start_time)
    print >> sys.stderr, ('The 30 percent corruption code ran for %.2fm' %
                          ((training_time) / 60.))
    image = PIL.Image.fromarray(
        tile_raster_images(X=da.W.get_value(borrow=True).T,
                           img_shape=(28, 28),
                           tile_shape=(10, 10),
                           tile_spacing=(1, 1)))
    image.save('filters_corruption_30.jpg')

    os.chdir('../')
コード例 #2
0
# -*- coding: utf-8 -*-

import theano
import theano.tensor as T
from loadDataset import load_data
from LogisticRegression import LogisticRegression

batch_size = 20
datasets = load_data(dataset)

train_set_x, train_set_y = datasets[0]
valid_set_x, valid_set_y = datasets[1]
test_set_x, test_set_y = datasets[2]

n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size
n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size

print '... building the model'

index = T.lscalar()

x = T.matrix('x')
y = T.ivector('y')

classifier = LogisticRegression(x, y, n_in=784, n_out=10)

#cost = T.log(classifier.p_y_given_x)[T.arange(y.shape[0]), y]
cost = T.log(classifier.p_y_given_x)[T.arange(y.shape[0]), y]

train_model = theano.function(
コード例 #3
0
ファイル: test.py プロジェクト: kmakantasis/DL-Multispectral
# -*- coding: utf-8 -*-

import theano
import theano.tensor as T
from loadDataset import load_data
from LogisticRegression import LogisticRegression


batch_size = 20
datasets = load_data(dataset)

train_set_x, train_set_y = datasets[0]
valid_set_x, valid_set_y = datasets[1]
test_set_x, test_set_y = datasets[2]

n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size
n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size
    
print '... building the model'

    
index = T.lscalar()  


x = T.matrix('x') 
y = T.ivector('y') 

classifier = LogisticRegression(x, y, n_in=784, n_out=10)

#cost = T.log(classifier.p_y_given_x)[T.arange(y.shape[0]), y]
コード例 #4
0
import numpy as np
from pylab import *
from loadDataset import load_data_2d as load_data
from random import random
import cv2

#For testing dataset for training. Randomly shows data and print it's value.

(X_train, y_train, X_test, y_test) = load_data()

labels = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9']
t =0
while True:
    #t = int(random()*120000)
    if labels[np.argmax(y_train[t])] != ' ':
        im = np.reshape(X_train[t],(28,28))
        cv2.imshow('a',im)
        print(labels[np.argmax(y_train[t])])
        cv2.waitKey(0)
    t=t+1
cv2.destroyAllWindows()
コード例 #5
0
def AutoEncoder_demo(learning_rate=0.1, training_epochs=2, dataset='mnist.pkl.gz', batch_size=20, output_folder='dA_plots'):
   
    datasets = load_data(dataset)
    train_set_x, train_set_y = datasets[0]
    n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
    index = T.lscalar()    # index to a [mini]batch
    x = T.matrix('x')  
    
    if not os.path.isdir(output_folder):
        os.makedirs(output_folder)
    os.chdir(output_folder)
 
   
    #####################################
    # BUILDING THE MODEL CORRUPTION 0% #
    #####################################    
    rng = numpy.random.RandomState(123)
    theano_rng = RandomStreams(rng.randint(2 ** 30))

    da = AutoEncoder(np_rng=rng, theano_rng=theano_rng, input=x, n_vis=28 * 28, n_hid=500)
    cost, updates = da.get_cost_updates(corruption_level=0., learning_rate=learning_rate)
    train_da = theano.function(inputs=[index], 
                               outputs=[cost], 
                               updates=updates,
                               givens={x: train_set_x[index * batch_size: (index + 1) * batch_size]})

    start_time = time.clock()
    for epoch in xrange(training_epochs):
        # go through trainng set
        c = []
        for batch_index in xrange(n_train_batches):
            c.append(train_da(batch_index))

        print 'Training epoch %d, cost ' % epoch, numpy.mean(c)
    end_time = time.clock()

    training_time = (end_time - start_time)
    print >> sys.stderr, ('The no corruption code ran for %.2fm' % ((training_time) / 60.))
    image = PIL.Image.fromarray(tile_raster_images(X=da.W.get_value(borrow=True).T,
                                                   img_shape=(28, 28), tile_shape=(10, 10),
                                                   tile_spacing=(1, 1)))
    image.save('filters_corruption_0.jpg')


    #####################################
    # BUILDING THE MODEL CORRUPTION 30% #
    #####################################
    rng = numpy.random.RandomState(123)
    theano_rng = RandomStreams(rng.randint(2 ** 30))

    da = AutoEncoder(np_rng=rng, theano_rng=theano_rng, input=x, n_vis=28 * 28, n_hid=500)
    cost, updates = da.get_cost_updates(corruption_level=0.3, learning_rate=learning_rate)
    train_da = theano.function(inputs=[index], 
                               outputs=[cost], 
                               updates=updates,
                               givens={x: train_set_x[index * batch_size:(index + 1) * batch_size]})

    start_time = time.clock()   
    for epoch in xrange(training_epochs):
        c = []
        for batch_index in xrange(n_train_batches):
            c.append(train_da(batch_index))

        print 'Training epoch %d, cost ' % epoch, numpy.mean(c)
    end_time = time.clock()
    
    training_time = (end_time - start_time)
    print >> sys.stderr, ('The 30 percent corruption code ran for %.2fm' % ((training_time) / 60.))
    image = PIL.Image.fromarray(tile_raster_images(X=da.W.get_value(borrow=True).T,
                                                   img_shape=(28, 28), tile_shape=(10, 10),
                                                   tile_spacing=(1, 1)))
    image.save('filters_corruption_30.jpg')

    os.chdir('../')