예제 #1
0
def run_mlp():
    # test the new way to automatically fill in inputs for models
    mlp = Prototype()
    x = ((None, 784), matrix("x"))
    mlp.add(Dense(inputs=x, outputs=1000, activation='rectifier'))
    mlp.add(Dense, outputs=1500, activation='tanh')
    mlp.add(Softmax, outputs=10, out_as_probs=False)

    # define our loss to optimize for the model (and the target variable)
    # targets from MNIST are int64 numbers 0-9
    y = lvector('y')
    loss = Neg_LL(inputs=mlp.models[-1].p_y_given_x, targets=y, one_hot=False)

    mnist = MNIST()

    optimizer = AdaDelta(model=mlp, loss=loss, dataset=mnist, epochs=10)
    optimizer.train()

    test_data, test_labels = mnist.test_inputs, mnist.test_targets
    test_data = test_data[:25]
    test_labels = test_labels[:25]
    # use the run function!
    yhat = mlp.run(test_data)
    print('-------')
    print('Prediction: %s' % str(yhat))
    print('Actual:     %s' % str(test_labels.astype('int32')))
예제 #2
0
        Dense(inputs=(dense_in_shape, dense_input),
              outputs=500,
              activation='tanh'))
    lenet.add(Noise, noise='dropout', noise_level=0.5)
    # softmax classification layer!
    lenet.add(Softmax, outputs=10, out_as_probs=False)

    ################
    # Optimization #
    ################
    # Now that our model is complete, let's define the loss function to optimize
    # first need a target variable
    labels = T.lvector('ys')
    # negative log-likelihood for classification cost
    loss = Neg_LL(inputs=lenet.models[-1].p_y_given_x,
                  targets=labels,
                  one_hot=False)
    # make a monitor to view average accuracy per batch
    accuracy = Monitor(name='Accuracy',
                       expression=1 -
                       (T.mean(T.neq(lenet.models[-1].y_pred, labels))),
                       valid=True,
                       test=True)

    # Now grab our MNIST dataset. The version given here has each image as a single 784-dimensional vector.
    # because convolutions work over 2d, let's reshape our data into the (28,28) images they originally were
    # (only one channel because they are black/white images not rgb)
    mnist = MNIST()
    process_image = lambda img: np.reshape(img, (1, 28, 28))
    mnist.train_inputs = ModifyStream(mnist.train_inputs, process_image)
    mnist.valid_inputs = ModifyStream(mnist.valid_inputs, process_image)
예제 #3
0
    mnist = MNIST()
    # create the basic layer
    layer1 = Dense(inputs=((None, 28 * 28), matrix("x")),
                   outputs=1000,
                   activation='linear')
    layer1_act = Activation(inputs=((None, 1000), layer1.get_outputs()),
                            activation='relu')
    # create the softmax classifier
    layer2 = Softmax(inputs=((None, 1000), layer1_act.get_outputs()),
                     outputs=10,
                     out_as_probs=True)
    # create the mlp from the two layers
    mlp = Prototype(layers=[layer1, layer1_act, layer2])
    # define the loss function
    loss = Neg_LL(inputs=mlp.get_outputs(),
                  targets=vector("y", dtype="int64"),
                  one_hot=False)

    # make an optimizer to train it (AdaDelta is a good default)
    # optimizer = AdaDelta(model=mlp, dataset=mnist, n_epoch=20)
    optimizer = AdaDelta(dataset=mnist, loss=loss, epochs=20)
    # perform training!
    # optimizer.train()
    mlp.train(optimizer)

    # test it on some images!
    test_data, test_labels = mnist.test_inputs, mnist.test_targets
    test_data = test_data[:25]
    test_labels = test_labels[:25]
    # use the run function!
    preds = mlp.run(test_data)[0]
예제 #4
0
    # grab the MNIST dataset
    mnist = MNIST()
    # create the basic layer
    layer1 = Dense(inputs=((None, 28*28), matrix("x")),
                   outputs=1000,
                   activation='linear')
    layer1_act = Activation(inputs=((None, 1000), layer1.get_outputs()), activation='relu')
    # create the softmax classifier
    layer2 = Softmax(inputs=((None, 1000), layer1_act.get_outputs()),
                     outputs=10,
                     out_as_probs=True)
    # create the mlp from the two layers
    mlp = Prototype(layers=[layer1, layer1_act, layer2])
    # define the loss function
    loss = Neg_LL(inputs=mlp.get_outputs(), targets=vector("y", dtype="int64"), one_hot=False)

    #plot the loss
    if BOKEH_AVAILABLE:
        plot = Plot("mlp_mnist", monitor_channels=Monitor("loss", loss.get_loss()), open_browser=True)
    else:
        plot = None

    # make an optimizer to train it (AdaDelta is a good default)
    # optimizer = AdaDelta(model=mlp, dataset=mnist, n_epoch=20)
    optimizer = AdaDelta(dataset=mnist, loss=loss, epochs=20)
    # perform training!
    # optimizer.train()
    mlp.train(optimizer, plot=plot)

    # test it on some images!
예제 #5
0
    def testLeNet(self):
        try:
            # quick and dirty way to create a model from arbitrary layers
            lenet = Prototype(outdir=None)
            # our input is going to be 4D tensor of images with shape (batch_size, 1, 28, 28)
            x = ((None, 1, 28, 28), ftensor4('x'))
            # our first convolutional layer
            lenet.add(
                Conv2D(inputs=x, n_filters=20, filter_size=(5, 5),
                       outdir=None))
            # our first pooling layer, automatically hooking inputs to the previous convolutional outputs
            lenet.add(Pool2D, size=(2, 2))
            # our second convolutional layer
            lenet.add(Conv2D, n_filters=50, filter_size=(5, 5), outdir=None)
            # our second pooling layer
            lenet.add(Pool2D, size=(2, 2))
            # now we need to flatten the 4D convolution outputs into 2D matrix (just flatten the trailing dimensions)
            lenet.add(Flatten, ndim=2)
            # one dense hidden layer
            lenet.add(Dense, outputs=500, activation='tanh', outdir=None)
            # hook a softmax classification layer, outputting the probabilities.
            lenet.add(Softmax, outputs=10, out_as_probs=True, outdir=None)

            # Grab the MNIST dataset
            data = MNIST(path="../../../datasets/{!s}".format(mnist_name),
                         concat_train_valid=False,
                         flatten=False)
            # define our loss to optimize for the model (and the target variable)
            # targets from MNIST are int64 numbers 0-9
            y = lvector('y')
            loss = Neg_LL(inputs=lenet.get_outputs(), targets=y, one_hot=False)
            # monitor
            error_monitor = Monitor(
                name='error',
                expression=mean(neq(lenet.models[-1].y_pred, y)),
                valid=True,
                test=True,
                out_service=FileService('outputs/lenet_error.txt'))
            # optimize our model to minimize loss given the dataset using SGD
            optimizer = SGD(model=lenet,
                            dataset=data,
                            loss=loss,
                            epochs=10,
                            batch_size=128,
                            learning_rate=.1,
                            momentum=False)
            print("Training LeNet...")
            optimizer.train(monitor_channels=error_monitor)

            def test_subset(filename, expected, conf=0.001):
                with open(filename, 'r') as f:
                    errs = [float(err) for err in f]
                for i, (err, exp) in enumerate(zip(errs, expected)):
                    if i == 0:
                        c = conf * 10
                    else:
                        c = conf
                    self.assertTrue(
                        exp - c < round(err, 4) < exp + c,
                        "Errors: {!s} and Expected: {!s} -- Error at {!s} and {!s}"
                        .format(errs, expected, err, exp))

            test_subset('outputs/lenet_error_train.txt', [
                .0753, .0239, .0159, .0113, .0088, .0064, .0050, .0037, .0026,
                .0019
            ])
            test_subset('outputs/lenet_error_valid.txt', [
                .0283, .0209, .0170, .0151, .0139, .0129, .0121, .0118, .0112,
                .0113
            ])
            test_subset('outputs/lenet_error_test.txt', [
                .0319, .0213, .0167, .0134, .0122, .0119, .0116, .0107, .0104,
                .0105
            ])
            shutil.rmtree('outputs/')

        finally:
            if 'lenet' in locals():
                del lenet
            if 'data' in locals():
                del data
            if 'y' in locals():
                del y
            if 'x' in locals():
                del x
            if 'loss' in locals():
                del loss
            if 'optimizer' in locals():
                del optimizer
예제 #6
0
if __name__ == '__main__':
    # set up the logging environment to display outputs (optional)
    # although this is recommended over print statements everywhere
    import logging
    from opendeep.log import config_root_logger
    config_root_logger()
    log = logging.getLogger(__name__)
    log.info("Creating softmax!")

    # grab the MNIST dataset
    mnist = MNIST()
    x = ((None, 28*28), matrix('x'))
    # create the softmax classifier
    s = Softmax(inputs=x, outputs=10, out_as_probs=False)
    # loss function
    loss = Neg_LL(inputs=s.p_y_given_x, targets=lvector('y'), one_hot=False)
    # make an optimizer to train it (AdaDelta is a good default)
    optimizer = AdaDelta(model=s, loss=loss, dataset=mnist, epochs=20)
    # perform training!
    optimizer.train()
    # test it on some images!
    test_data, test_labels = mnist.test_inputs[:25], mnist.test_targets[:25]
    # use the run function!
    preds = s.run(test_data)
    print('-------')
    print(preds)
    print(test_labels.astype('int32'))
    print()
    print()
    del mnist
    del s
예제 #7
0
    # Grab the MNIST dataset
    data = MNIST(concat_train_valid=False)

    # we need to convert the (784,) flat example from MNIST to (1, 28, 28) for a 2D greyscale image
    process_mnist = lambda img: np.reshape(img, (1, 28, 28))

    # we can do this by using ModifyStreams over the inputs!
    data.train_inputs = ModifyStream(data.train_inputs, process_mnist)
    data.valid_inputs = ModifyStream(data.valid_inputs, process_mnist)
    data.test_inputs = ModifyStream(data.test_inputs, process_mnist)

    # now build the actual model
    lenet = build_lenet()
    # define our loss to optimize for the model (and the target variable)
    # targets from MNIST are int64 numbers 0-9
    y = lvector('y')
    loss = Neg_LL(inputs=lenet.get_outputs(), targets=y, one_hot=False)
    error_monitor = Monitor(name='error',
                            expression=mean(neq(lenet.models[-1].y_pred, y)),
                            valid=True,
                            test=True,
                            out_service=FileService('outputs/lenet_error.txt'))
    # optimize our model to minimize loss given the dataset using SGD
    optimizer = SGD(model=lenet,
                    dataset=data,
                    loss=loss,
                    epochs=200,
                    batch_size=128,
                    learning_rate=.1,
                    momentum=False)
    optimizer.train(monitor_channels=error_monitor)
예제 #8
0
    mnist = MNIST()
    # create the basic layer
    layer1 = Dense(inputs=((None, 28 * 28), matrix("x")),
                   outputs=1000,
                   activation='linear')
    layer1_act = Activation(inputs=((None, 1000), layer1.get_outputs()),
                            activation='relu')
    # create the softmax classifier
    layer2 = Softmax(inputs=((None, 1000), layer1_act.get_outputs()),
                     outputs=10,
                     out_as_probs=True)
    # create the mlp from the two layers
    mlp = Prototype(layers=[layer1, layer1_act, layer2])
    # define the loss function
    loss = Neg_LL(inputs=mlp.get_outputs(),
                  targets=vector("y", dtype="int64"),
                  one_hot=False)

    #plot the loss
    if BOKEH_AVAILABLE:
        plot = Plot("mlp_mnist",
                    monitor_channels=Monitor("loss", loss.get_loss()),
                    open_browser=True)
    else:
        plot = None

    # make an optimizer to train it (AdaDelta is a good default)
    # optimizer = AdaDelta(model=mlp, dataset=mnist, n_epoch=20)
    optimizer = AdaDelta(dataset=mnist, loss=loss, epochs=20)
    # perform training!
    # optimizer.train()
예제 #9
0
lenet.add(Conv2D, n_filters=20, filter_size=(5, 5), border_mode="full", activation="relu")
lenet.add(Pool2D, size=(2, 2))
lenet.add(Noise, noise="dropout", noise_level=0.5)

dense_input = lenet.models[-1].get_outputs().flatten(2)
dense_in_shape = lenet.models[-1].output_size[:1] + (np.prod(lenet.models[-1].output_size[1:]), )

lenet.add(Dense(inputs=(dense_in_shape, dense_input), outputs=500, activation="relu"))
lenet.add(Noise, noise="dropout", noise_level=0.5)

lenet.add(Dense, outputs=2)

labels = T.lvector('ys')

loss = Neg_LL(inputs=lenet.models[-1].get_outputs(), targets=labels, one_hot=False)

#accuracy = Monitor(name="Accuracy", expression=1-(T.mean(T.neq(lenet.models[-1].y_pred, labels))),
#                   valid=True, test=True)


def greyscale_image(img):
    img = img.transpose(2, 1, 0)
    arr = np.average(img, 0).astype(int)
    return arr[None, :, :]


def target_preprocess(img):
    x, y, _ = filter_test.find_goals(img)[0]
    return x/img.shape[0], y/img.shape[1]