예제 #1
0
def get_sequenced_mnist(sequence_number=0, seq_3d=False, seq_length=30, rng=None, flatten=True):
    """
        sequence_number : int, optional
            The sequence method to use if we want to put the input images into a specific order. 0 defaults to random.
        seq_3d : bool, optional
            When sequencing, whether the output should be
            3D tensors (batches, subsequences, data) or 2D (sequence, data).
        seq_length: int, optional
            The length of subsequences to split the data.
        rng : random, optional
            The random number generator to use when sequencing.
    """
    mnist = MNIST(flatten=flatten)

    # sequence the dataset
    if sequence_number is not None:
        _sequence(mnist, sequence_number=sequence_number, rng=rng)

    # optionally make 3D instead of 2D
    if seq_3d:
        print("Making 3D....")
        # chop up into sequences of length seq_length
        # first make sure to chop off the remainder of the data so seq_length can divide evenly.
        if mnist.train_inputs.shape[0] % seq_length != 0:
            length, dim = mnist.train_inputs.shape
            if mnist.train_targets.ndim == 1:
                ydim = 1
            else:
                ydim = mnist.train_targets.shape[-1]
            mnist.train_inputs = mnist.train_inputs[:seq_length * int(math.floor(length / seq_length))]
            mnist.train_targets = mnist.train_targets[:seq_length * int(math.floor(length / seq_length))]
            # now create the 3D tensor of sequences - they will be (num_sequences, sequence_size, 784)
            mnist.train_inputs = numpy.reshape(mnist.train_inputs, (length / seq_length, seq_length, dim))
            mnist.train_targets = numpy.reshape(mnist.train_targets, (length / seq_length, seq_length, ydim))

        if mnist.valid_inputs.shape[0] % seq_length != 0:
            length, dim = mnist.valid_inputs.shape
            if mnist.valid_targets.ndim == 1:
                ydim = 1
            else:
                ydim = mnist.valid_targets.shape[-1]
            mnist.valid_inputs = mnist.valid_inputs[:seq_length * int(math.floor(length / seq_length))]
            mnist.valid_targets = mnist.valid_targets[:seq_length * int(math.floor(length / seq_length))]
            # now create the 3D tensor of sequences - they will be (num_sequences, sequence_size, 784)
            mnist.valid_inputs = numpy.reshape(mnist.valid_inputs, (length / seq_length, seq_length, dim))
            mnist.valid_targets = numpy.reshape(mnist.valid_targets, (length / seq_length, seq_length, ydim))

        if mnist.test_inputs.shape[0] % seq_length != 0:
            length, dim = mnist.test_inputs.shape
            if mnist.test_targets.ndim == 1:
                ydim = 1
            else:
                ydim = mnist.test_targets.shape[-1]
            mnist.test_inputs = mnist.test_inputs[:seq_length * int(math.floor(length / seq_length))]
            mnist.test_targets = mnist.test_targets[:seq_length * int(math.floor(length / seq_length))]
            # now create the 3D tensor of sequences - they will be (num_sequences, sequence_size, 784)
            mnist.test_inputs = numpy.reshape(mnist.test_inputs, (length / seq_length, seq_length, dim))
            mnist.test_targets = numpy.reshape(mnist.test_targets, (length / seq_length, seq_length, ydim))

    print('Train shape is: {!s}, {!s}'.format(mnist.train_inputs.shape, mnist.train_targets.shape))
    print('Valid shape is: {!s}, {!s}'.format(mnist.valid_inputs.shape, mnist.valid_targets.shape))
    print('Test shape is: {!s}, {!s}'.format(mnist.test_inputs.shape, mnist.test_targets.shape))
    return mnist
예제 #2
0
    # 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)
    mnist.test_inputs = ModifyStream(mnist.test_inputs, process_image)

    # finally define our optimizer and train the model!
    optimizer = AdaDelta(model=lenet,
                         dataset=mnist,
                         loss=loss,
                         epochs=10,
                         batch_size=64)
    # train!
    optimizer.train(monitor_channels=accuracy)
예제 #3
0
    lenet.add(
        Softmax, outputs=10, out_as_probs=True
    )

    return lenet


if __name__ == '__main__':
    # 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)
    # optimize our model to minimize loss given the dataset using SGD
    optimizer = SGD(model=lenet,
                    dataset=data,
                    loss=loss,
                    epochs=200,
예제 #4
0
    ################
    # 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)
    mnist.test_inputs = ModifyStream(mnist.test_inputs, process_image)

    # finally define our optimizer and train the model!
    optimizer = AdaDelta(
        model=lenet,
        dataset=mnist,
        loss=loss,
        epochs=10,
        batch_size=64
    )
    # train!
    optimizer.train(monitor_channels=accuracy)
예제 #5
0
    lenet.add(Dense, outputs=500, activation='tanh')
    # hook a softmax classification layer, outputting the probabilities.
    lenet.add(Softmax, outputs=10, out_as_probs=True)

    return lenet


if __name__ == '__main__':
    # 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