Esempio n. 1
0
def one_hot(labels , load_type ):#= LOAD.NUMERIC):
    #print load_type
    if load_type != LOAD.NUMERIC :
        n_classes = len(lp.ascii_ymatrix(alphabet_set=load_type)) 
        classes = np.unique(labels)
        one_hot_labels = np.zeros(labels.shape + (n_classes,))
        #print labels.shape, labels
    else:
        classes = np.unique(labels)
        n_classes = classes.size
        one_hot_labels = np.zeros(labels.shape + (n_classes,))
    for c in classes:
        one_hot_labels[labels == c, c] = 1
    return one_hot_labels
def run(max_iter=10, n_train_samples=300):
    name = "alpha"
    print str(datetime.datetime.now())
    
    def signal_handler(signal, frame) :
        print(" \n...you want to exit!")
        for layer in nn.layers:
            if (isinstance(layer, lnnet.ParamMixin)) : print "len: " + str(len(layer.W))
        if True  :
            print("save weights.")
            sys.stdout.flush()
            nn.save_file(name=name)
        sys.exit(0)

    signal.signal(signal.SIGINT,signal_handler)

    #n_train_samples = 3000 #3000
    # Fetch data
    
    
    #dset = lp.get_dataset(load_type=LOAD.ALPHA)
    t1, l1, files = lp.batch_load_alpha( 0,  n_train_samples, True, [], LOAD.ALPHA)
    X_train = t1 #dset[0]
    y_train = l1 #dset[1]
    
    X_train = np.reshape(X_train, (-1, 1, 28, 28))
    y_train = np.array(y_train)
    
    '''
    train_idxs = np.random.random_integers(0, len(dset[0])-1, n_train_samples)
    #train_idxs = np.array([i for i in range(n_train_samples)])
    X_train = X_train[train_idxs, ...]
    y_train = y_train[train_idxs, ...]
    '''
    
    # Downsample training data

    
    n_classes = len(lp.ascii_ymatrix(LOAD.ALPHA))

    # Setup convolutional neural network
    nn = cnnet.NeuralNetwork(
        layers=[
            conv.Conv(
                n_feats=12,
                filter_shape=(5, 5),
                strides=(1, 1),
                weight_scale=0.1,
                weight_decay=0.001,
            ),
            lnnet.Activation('relu'),
            conv.Pool(
                pool_shape=(2, 2),
                strides=(2, 2),
                mode='max',
            ),
            conv.Conv(
                n_feats=16,
                filter_shape=(5, 5),
                strides=(1, 1),
                weight_scale=0.1,
                weight_decay=0.001,
            ),
            
            lnnet.Activation('relu'),
            conv.Flatten(),
            
            #lnnet.Linear(
            #    n_out=500,
            #    weight_scale=0.1,
            #    weight_decay=0.02,
            #),
            
            #lnnet.Activation('relu'),
            
            lnnet.Linear(
                n_out=n_classes,
                weight_scale=0.1,
                weight_decay=0.02,
            ),
            
            lnnet.LogRegression(),
        ],
    )

    
    # Train neural network
    t0 = time.time()
    nn.set_android_load(False)
    if n_train_samples != 300 and False : nn.set_interrupt(True)
    if max_iter < 0 :
        X = X_train
        Y = y_train
        Y_one_hot = one_hot(Y , load_type=LOAD.ALPHA)
        nn.set_name(name)
        nn._setup(X, Y_one_hot)
        nn.load_file(name=name)
        nn.status(-1,X,Y,Y_one_hot) ##end
    else:
        nn.fit(X_train, y_train, learning_rate=0.05, max_iter=max_iter, batch_size=64, name=name, load_type = LOAD.ALPHA)
    t1 = time.time()
    print('Duration: %.1fs' % (t1-t0))

    if False:
        # Evaluate on test data
        error = nn.error(X_test, y_test)
        print('Test error rate: %.4f' % error)
def run():
    load_type = LOAD.ALPHA
    #X_train, y_train = fetch_mnist_img()
    n_classes = len(lp.ascii_ymatrix(load_type))
    X_setup = np.zeros(shape=(1,1,28,28))
    y_setup = np.zeros(shape=(1,n_classes))
    
    name = "alpha"

    # Setup convolutional neural network
    nn1 = cnnet.NeuralNetwork(
        layers=[
            conv.Conv(
                n_feats=12,
                filter_shape=(5, 5),
                strides=(1, 1),
                weight_scale=0.1,
                weight_decay=0.001,
            ),
            lnnet.Activation('relu'),
            conv.Pool(
                pool_shape=(2, 2),
                strides=(2, 2),
                mode='max',
            ),
            conv.Conv(
                n_feats=16,
                filter_shape=(5, 5),
                strides=(1, 1),
                weight_scale=0.1,
                weight_decay=0.001,
            ),
            
            lnnet.Activation('relu'),
            conv.Flatten(),
            
            #lnnet.Linear(
            #    n_out=500,
            #    weight_scale=0.1,
            #    weight_decay=0.02,
            #),
            
            #lnnet.Activation('relu'),
            
            lnnet.Linear(
                n_out=n_classes,
                weight_scale=0.1,
                weight_decay=0.02,
            ),
            
            lnnet.LogRegression(),
        ],
    )
    
    nn1.set_android_load(False)
    nn1._setup(X_setup, y_setup)
    nn1.load_file(name=name)

    ii = 0
    tot_right = 0
    still_looping = True
    while still_looping:
        ii += 1
        X_train, y_train = fetch_alpha_img()
        
        ## the following three lines find the prediction for one image
        X = X_train[0][0]
        X = np.reshape(X,(-1,1,28,28))
        pred = nn1.predict(X)[0]
        
        ## the following two lines are just for textual display
        X_disp = shape_x(X_train[0][0])
        show_xvalues([X_disp], index=0)
        
        print "stored value: " + str( int(y_train[0]))
        print("prediction:   " + str( pred ))
        print (show_ycharacter(pred))
        if int(pred) == int(y_train[0]):
            tot_right += 1
        
        print("tot right: " + str(tot_right))
        print("percent right: " + str(tot_right/float(ii) * 100 ))
        xx = raw_input("more? (Y/n): ")
        if xx.strip() == 'n' or xx.strip() == 'N' : still_looping = False
def show_ycharacter(l):
    mat = lp.ascii_ymatrix(LOAD.ALPHA)[l][1]
    return mat
Esempio n. 5
0
def run(max_iter=10, n_train_samples=300):
    def signal_handler(signal, frame):
        print(" you want to exit!")
        for layer in nn.layers:
            if (isinstance(layer, lnnet.ParamMixin)):
                print "len: " + str(len(layer.W))
        if True:
            print("save weights.")
            sys.stdout.flush()
            nn.save_file(name="mnist")
        sys.exit(0)

    signal.signal(signal.SIGINT, signal_handler)

    #conv.conv.print_test()
    # Fetch data
    mnist = sklearn.datasets.fetch_mldata('MNIST original', data_home='./data')
    split = 60000
    X_train = np.reshape(mnist.data[:split], (-1, 1, 28, 28)) / 255.0
    y_train = mnist.target[:split]
    X_test = np.reshape(mnist.data[split:], (-1, 1, 28, 28)) / 255.0
    y_test = mnist.target[split:]
    n_classes = np.unique(y_train).size

    n_classes = len(lp.ascii_ymatrix(LOAD.NUMERIC))
    # Downsample training data
    #n_train_samples = 1000 #3000
    train_idxs = np.random.random_integers(0, split - 1, n_train_samples)
    #train_idxs = np.array([i for i in range(n_train_samples)])
    X_train = X_train[train_idxs, ...]
    y_train = y_train[train_idxs, ...]
    name = "mnist"

    # Setup convolutional neural network
    nn = cnnet.NeuralNetwork(
        layers=[
            conv.Conv(
                n_feats=12,
                filter_shape=(5, 5),
                strides=(1, 1),
                weight_scale=0.1,
                weight_decay=0.001,
            ),
            lnnet.Activation('relu'),
            conv.Pool(
                pool_shape=(2, 2),
                strides=(2, 2),
                mode='max',
            ),
            conv.Conv(
                n_feats=16,
                filter_shape=(5, 5),
                strides=(1, 1),
                weight_scale=0.1,
                weight_decay=0.001,
            ),
            lnnet.Activation('relu'),
            conv.Flatten(),

            #lnnet.Linear(
            #    n_out=500,
            #    weight_scale=0.1,
            #    weight_decay=0.02,
            #),

            #lnnet.Activation('relu'),
            lnnet.Linear(
                n_out=n_classes,
                weight_scale=0.1,
                weight_decay=0.02,
            ),
            lnnet.LogRegression(),
        ], )

    # Train neural network
    t0 = time.time()
    nn.set_android_load(False)
    if max_iter < 0:
        X = X_train
        Y = y_train
        Y_one_hot = one_hot(Y, load_type=LOAD.NUMERIC)
        nn.set_name(name)
        nn._setup(X, Y_one_hot)
        nn.load_file(name=name)
        nn.status(-1, X, Y, Y_one_hot)  ##end
    else:
        nn.fit(X_train,
               y_train,
               learning_rate=0.05,
               max_iter=max_iter,
               batch_size=64,
               name=name,
               load_type=LOAD.NUMERIC)
    t1 = time.time()
    print('Duration: %.1fs' % (t1 - t0))

    if False:
        # Evaluate on test data
        error = nn.error(X_test, y_test)
        print('Test error rate: %.4f' % error)
def run():
    load_type = LOAD.ALPHA
    #X_train, y_train = fetch_mnist_img()
    n_classes = len(lp.ascii_ymatrix(load_type))
    X_setup = np.zeros(shape=(1, 1, 28, 28))
    y_setup = np.zeros(shape=(1, n_classes))

    name = "alpha"

    # Setup convolutional neural network
    nn1 = cnnet.NeuralNetwork(
        layers=[
            conv.Conv(
                n_feats=12,
                filter_shape=(5, 5),
                strides=(1, 1),
                weight_scale=0.1,
                weight_decay=0.001,
            ),
            lnnet.Activation('relu'),
            conv.Pool(
                pool_shape=(2, 2),
                strides=(2, 2),
                mode='max',
            ),
            conv.Conv(
                n_feats=16,
                filter_shape=(5, 5),
                strides=(1, 1),
                weight_scale=0.1,
                weight_decay=0.001,
            ),
            lnnet.Activation('relu'),
            conv.Flatten(),

            #lnnet.Linear(
            #    n_out=500,
            #    weight_scale=0.1,
            #    weight_decay=0.02,
            #),

            #lnnet.Activation('relu'),
            lnnet.Linear(
                n_out=n_classes,
                weight_scale=0.1,
                weight_decay=0.02,
            ),
            lnnet.LogRegression(),
        ], )

    nn1.set_android_load(False)
    nn1._setup(X_setup, y_setup)
    nn1.load_file(name=name)

    ii = 0
    tot_right = 0
    still_looping = True
    while still_looping:
        ii += 1
        X_train, y_train = fetch_alpha_img()

        ## the following three lines find the prediction for one image
        X = X_train[0][0]
        X = np.reshape(X, (-1, 1, 28, 28))
        pred = nn1.predict(X)[0]

        ## the following two lines are just for textual display
        X_disp = shape_x(X_train[0][0])
        show_xvalues([X_disp], index=0)

        print "stored value: " + str(int(y_train[0]))
        print("prediction:   " + str(pred))
        print(show_ycharacter(pred))
        if int(pred) == int(y_train[0]):
            tot_right += 1

        print("tot right: " + str(tot_right))
        print("percent right: " + str(tot_right / float(ii) * 100))
        xx = raw_input("more? (Y/n): ")
        if xx.strip() == 'n' or xx.strip() == 'N': still_looping = False
def show_ycharacter(l):
    mat = lp.ascii_ymatrix(LOAD.ALPHA)[l][1]
    return mat
Esempio n. 8
0
def run(max_iter=10, n_train_samples=300):

    def signal_handler(signal, frame) :
        print(" you want to exit!")
        for layer in nn.layers:
            if (isinstance(layer, lnnet.ParamMixin)) : print "len: " + str(len(layer.W))
        if True  :
            print("save weights.")
            sys.stdout.flush()
            nn.save_file(name="mnist")
        sys.exit(0)

    signal.signal(signal.SIGINT,signal_handler)

    #conv.conv.print_test()
    # Fetch data
    mnist = sklearn.datasets.fetch_mldata('MNIST original', data_home='./data')
    split = 60000
    X_train = np.reshape(mnist.data[:split], (-1, 1, 28, 28))/255.0
    y_train = mnist.target[:split]
    X_test = np.reshape(mnist.data[split:], (-1, 1, 28, 28))/255.0
    y_test = mnist.target[split:]
    n_classes = np.unique(y_train).size

    n_classes = len(lp.ascii_ymatrix(LOAD.NUMERIC))
    # Downsample training data
    #n_train_samples = 1000 #3000
    train_idxs = np.random.random_integers(0, split-1, n_train_samples)
    #train_idxs = np.array([i for i in range(n_train_samples)])
    X_train = X_train[train_idxs, ...]
    y_train = y_train[train_idxs, ...]
    name = "mnist"

    # Setup convolutional neural network
    nn = cnnet.NeuralNetwork(
        layers=[
            conv.Conv(
                n_feats=12,
                filter_shape=(5, 5),
                strides=(1, 1),
                weight_scale=0.1,
                weight_decay=0.001,
            ),
            lnnet.Activation('relu'),
            conv.Pool(
                pool_shape=(2, 2),
                strides=(2, 2),
                mode='max',
            ),
            conv.Conv(
                n_feats=16,
                filter_shape=(5, 5),
                strides=(1, 1),
                weight_scale=0.1,
                weight_decay=0.001,
            ),
            
            lnnet.Activation('relu'),
            conv.Flatten(),
            
            #lnnet.Linear(
            #    n_out=500,
            #    weight_scale=0.1,
            #    weight_decay=0.02,
            #),
            
            #lnnet.Activation('relu'),
            
            lnnet.Linear(
                n_out=n_classes,
                weight_scale=0.1,
                weight_decay=0.02,
            ),
            
            lnnet.LogRegression(),
        ],
    )

    # Train neural network
    t0 = time.time()
    nn.fit(X_train, y_train, learning_rate=0.05, max_iter=max_iter, batch_size=64, name=name, load_type=LOAD.NUMERIC)
    t1 = time.time()
    print('Duration: %.1fs' % (t1-t0))

    if False:
        # Evaluate on test data
        error = nn.error(X_test, y_test)
        print('Test error rate: %.4f' % error)
def run(max_iter=10, n_train_samples=300):
    name = "alpha"
    print str(datetime.datetime.now())

    def signal_handler(signal, frame):
        print(" \n...you want to exit!")
        for layer in nn.layers:
            if (isinstance(layer, lnnet.ParamMixin)):
                print "len: " + str(len(layer.W))
        if True:
            print("save weights.")
            sys.stdout.flush()
            nn.save_file(name=name)
        sys.exit(0)

    signal.signal(signal.SIGINT, signal_handler)

    #n_train_samples = 3000 #3000
    # Fetch data

    #dset = lp.get_dataset(load_type=LOAD.ALPHA)
    t1, l1, files = lp.batch_load_alpha(0, n_train_samples, True, [],
                                        LOAD.ALPHA)
    X_train = t1  #dset[0]
    y_train = l1  #dset[1]

    X_train = np.reshape(X_train, (-1, 1, 28, 28))
    y_train = np.array(y_train)
    '''
    train_idxs = np.random.random_integers(0, len(dset[0])-1, n_train_samples)
    #train_idxs = np.array([i for i in range(n_train_samples)])
    X_train = X_train[train_idxs, ...]
    y_train = y_train[train_idxs, ...]
    '''

    # Downsample training data

    n_classes = len(lp.ascii_ymatrix(LOAD.ALPHA))

    # Setup convolutional neural network
    nn = cnnet.NeuralNetwork(
        layers=[
            conv.Conv(
                n_feats=12,
                filter_shape=(5, 5),
                strides=(1, 1),
                weight_scale=0.1,
                weight_decay=0.001,
            ),
            lnnet.Activation('relu'),
            conv.Pool(
                pool_shape=(2, 2),
                strides=(2, 2),
                mode='max',
            ),
            conv.Conv(
                n_feats=16,
                filter_shape=(5, 5),
                strides=(1, 1),
                weight_scale=0.1,
                weight_decay=0.001,
            ),
            lnnet.Activation('relu'),
            conv.Flatten(),

            #lnnet.Linear(
            #    n_out=500,
            #    weight_scale=0.1,
            #    weight_decay=0.02,
            #),

            #lnnet.Activation('relu'),
            lnnet.Linear(
                n_out=n_classes,
                weight_scale=0.1,
                weight_decay=0.02,
            ),
            lnnet.LogRegression(),
        ], )

    # Train neural network
    t0 = time.time()
    nn.set_android_load(False)
    if n_train_samples != 300 and False: nn.set_interrupt(True)
    if max_iter < 0:
        X = X_train
        Y = y_train
        Y_one_hot = one_hot(Y, load_type=LOAD.ALPHA)
        nn.set_name(name)
        nn._setup(X, Y_one_hot)
        nn.load_file(name=name)
        nn.status(-1, X, Y, Y_one_hot)  ##end
    else:
        nn.fit(X_train,
               y_train,
               learning_rate=0.05,
               max_iter=max_iter,
               batch_size=64,
               name=name,
               load_type=LOAD.ALPHA)
    t1 = time.time()
    print('Duration: %.1fs' % (t1 - t0))

    if False:
        # Evaluate on test data
        error = nn.error(X_test, y_test)
        print('Test error rate: %.4f' % error)