Exemplo n.º 1
0
def run():

    #X_train, y_train = fetch_mnist_img()
    n_classes = 10
    X_setup = np.zeros(shape=(1, 1, 28, 28))
    y_setup = np.zeros(shape=(1, n_classes))

    name = "mnist"

    # 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_mnist_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))

        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
Exemplo n.º 2
0
    def run(self):
        load_type_a = LOAD.ALPHA
        
        n_classes_a = len(ascii_ymatrix(load_type_a))
        X_setup_a = np.zeros(shape=(1,1,28,28))
        y_setup_a = np.zeros(shape=(1,n_classes_a))
        
        load_type_n = LOAD.NUMERIC
        
        n_classes_n = len(ascii_ymatrix(load_type_n))
        X_setup_n = np.zeros(shape=(1,1,28,28))
        y_setup_n = np.zeros(shape=(1,n_classes_n))
        
        self.name_a = "alpha"
        self.name_n = "mnist"

        # Setup convolutional neural network
        self.nn_a = 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=n_classes_a,
                    weight_scale=0.1,
                    weight_decay=0.02,
                ),
                lnnet.LogRegression(),
            ],
        )
        
        self.nn_a.set_name(self.name_a)
        self.nn_a._setup(X_setup_a, y_setup_a)
        self.nn_a.set_android_load(self.android_load)

        self.nn_n = 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=n_classes_n,
                    weight_scale=0.1,
                    weight_decay=0.02,
                ),
                lnnet.LogRegression(),
            ],
        )
        
        self.nn_n.set_name(self.name_n)
        self.nn_n._setup(X_setup_n, y_setup_n)
        self.nn_n.set_android_load(self.android_load)
Exemplo n.º 3
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(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)