Esempio n. 1
0
def train_ensemble(prototype_net, dataset, outfile=None, n_nets=10, use_gpu=True):
    ''' Trains a given number of networks on a given dataset.

    All networks will be clones of the given prototoype, and they will all
    be pickled into the given outfile.'''
    from binet import op
    if use_gpu:
        gc.collect()
        if not op._IS_CUDA_INITIALIZED:
            logger = logging.getLogger(__name__)
            logger.warn("CUDA not initialized, initializing GPU 0")
            op.init_gpu(0)

        X, y, Xvalid, yvalid = [op.to_gpu(d) for d in dataset]
        prototype_net = op.to_gpu(prototype_net)
    else:
        X, y, Xvalid, yvalid = dataset
    if outfile is not None:
        f = open(outfile, "wb")
    nets = []
    try:
        for i in range(n_nets):
            prototype_net.reset()
            if use_gpu:
                prototype_net = op.to_gpu(prototype_net)
            prototype_net.fit(X, y, Xvalid, yvalid)
            prototype_net = op.to_cpu(prototype_net)
            nets.append(copy.deepcopy(prototype_net))
            if outfile is not None:
                pickle.dump(prototype_net, f, -1)
    finally:
        if outfile is not None:
            f.close()
    return nets
Esempio n. 2
0
def train(net, dataset, fname=None, skip_output=25,
          show_plots=False, use_gpu=True, **kwargs):
    ''' Trains a neural network on the given dataset.

    If desired, the log-statements during training can be buffered into a
    StringIO object. This has the drawback that the output is only visible
    once the net has been fully trained, but it allows to only print only every
    n-th message.

    Parameters
    ----------
    net: the neural net.
    dataset: tuple containing 'trainx', 'trainy', 'validx', 'validy'
    fname: file-name in which to store the (pickled) network after training.
           The file will be stored in the 'data' subfolder of the CWD.
    skip_output: how many lines of output to skip between two lines that
                 will actually be printed.
    show_plots: If True, plot the first 256 weights of the lowest layer.
    use_gpu: if True, use gnumpy to run the code on the GPU.
    **kwargs: additional parameters for the `plotImages` cool when
              `plot_weights=True`.
    '''
    from binet import op
    if use_gpu:
        gc.collect()
        if not op._IS_CUDA_INITIALIZED:
            logger = logging.getLogger(__name__)
            logger.warn("CUDA not initialized, initializing GPU 0")
            op.init_gpu(0)

        X, y, Xvalid, yvalid = [op.to_gpu(d) for d in dataset]
        net = op.to_gpu(net)
    else:
        X, y, Xvalid, yvalid = dataset
    try:
        init_out = net.transform(X)
        init_err = net._get_loss(y, init_out)
        net.track_progress(time.time(), init_err, X, y, Xvalid, yvalid)
        net.fit(X, y, Xvalid, yvalid, skip_output=skip_output)
        #if net.verbose and net.current_epoch % skip_output != 0: # make sure we show the last line
        #    net.track_progress(time.time(), -1, X, y, Xvalid, yvalid)
    except KeyboardInterrupt:
        print("Intercepted KeyboardInterrupt, stopping... current status:")
        net.track_progress(time.time(), -1, X, y, Xvalid, yvalid)
        net.statistics = net.statistics[:-1] # we just added an invalid point
    finally:
        net = op.to_cpu(net)
        if fname:
            if not os.path.exists("data"):
                warnings.warn("creating 'data' directory to store pickled net")
                os.mkdir("data")
            with open(os.path.join("data", fname), "wb") as f:
                pickle.dump(net, f, -1)
        if show_plots:
            plot_images(net.weights[0], 16, 16, **kwargs)
            plot_learning_curves(net, **kwargs)
    return net
Esempio n. 3
0
def train_ensemble(prototype_net,
                   dataset,
                   outfile=None,
                   n_nets=10,
                   use_gpu=True):
    ''' Trains a given number of networks on a given dataset.

    All networks will be clones of the given prototoype, and they will all
    be pickled into the given outfile.'''
    from binet import op
    if use_gpu:
        gc.collect()
        if not op._IS_CUDA_INITIALIZED:
            logger = logging.getLogger(__name__)
            logger.warn("CUDA not initialized, initializing GPU 0")
            op.init_gpu(0)

        X, y, Xvalid, yvalid = [op.to_gpu(d) for d in dataset]
        prototype_net = op.to_gpu(prototype_net)
    else:
        X, y, Xvalid, yvalid = dataset
    if outfile is not None:
        f = open(outfile, "wb")
    nets = []
    try:
        for i in range(n_nets):
            prototype_net.reset()
            if use_gpu:
                prototype_net = op.to_gpu(prototype_net)
            prototype_net.fit(X, y, Xvalid, yvalid)
            prototype_net = op.to_cpu(prototype_net)
            nets.append(copy.deepcopy(prototype_net))
            if outfile is not None:
                pickle.dump(prototype_net, f, -1)
    finally:
        if outfile is not None:
            f.close()
    return nets
Esempio n. 4
0
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import

import nose
import copy
import numpy as np
from nose.tools import assert_raises
from numpy.testing import (assert_allclose, assert_array_equal,
                           assert_almost_equal)

from binet import op
from binet import NeuralNet, load_dataset

op.init_gpu()

X, y, Xval, yval = load_dataset("mnist_basic")
Xd, yd = op.to_gpu(X.copy()), op.to_gpu(y.copy())


def test_gpucpu_fprop_equality():
    '''Test forward propagation CPU/GPU equality.'''
    neth = NeuralNet([X.shape[1], 128, 32, y.shape[1]])
    netd = op.to_gpu(copy.deepcopy(neth))
    outh = neth.forward_pass(X)
    outd = netd.forward_pass(Xd)
    assert_allclose(outd.get(), outh, rtol=1e-5, err_msg="frop error")


def test_gpucpu_bprop_equality():
    '''Test backpropagation CPU/GPU equality.'''
Esempio n. 5
0
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import

import nose
import copy
import numpy as np
from nose.tools import assert_raises
from numpy.testing import (assert_allclose, assert_array_equal,
                          assert_almost_equal)

from binet import op
from binet import NeuralNet, load_dataset

op.init_gpu()

X, y, Xval, yval = load_dataset("mnist_basic")
Xd, yd = op.to_gpu(X.copy()),op.to_gpu(y.copy())

def test_gpucpu_fprop_equality():
    '''Test forward propagation CPU/GPU equality.'''
    neth = NeuralNet([X.shape[1], 128, 32, y.shape[1]])
    netd = op.to_gpu(copy.deepcopy(neth))
    outh = neth.forward_pass(X)
    outd = netd.forward_pass(Xd)
    assert_allclose(outd.get(), outh, rtol=1e-5, err_msg="frop error")


def test_gpucpu_bprop_equality():
    '''Test backpropagation CPU/GPU equality.'''
Esempio n. 6
0
def train(net,
          dataset,
          fname=None,
          skip_output=25,
          show_plots=False,
          use_gpu=True,
          **kwargs):
    ''' Trains a neural network on the given dataset.

    If desired, the log-statements during training can be buffered into a
    StringIO object. This has the drawback that the output is only visible
    once the net has been fully trained, but it allows to only print only every
    n-th message.

    Parameters
    ----------
    net: the neural net.
    dataset: tuple containing 'trainx', 'trainy', 'validx', 'validy'
    fname: file-name in which to store the (pickled) network after training.
           The file will be stored in the 'data' subfolder of the CWD.
    skip_output: how many lines of output to skip between two lines that
                 will actually be printed.
    show_plots: If True, plot the first 256 weights of the lowest layer.
    use_gpu: if True, use gnumpy to run the code on the GPU.
    **kwargs: additional parameters for the `plotImages` cool when
              `plot_weights=True`.
    '''
    from binet import op
    if use_gpu:
        gc.collect()
        if not op._IS_CUDA_INITIALIZED:
            logger = logging.getLogger(__name__)
            logger.warn("CUDA not initialized, initializing GPU 0")
            op.init_gpu(0)

        X, y, Xvalid, yvalid = [op.to_gpu(d) for d in dataset]
        net = op.to_gpu(net)
    else:
        X, y, Xvalid, yvalid = dataset
    try:
        init_out = net.transform(X)
        init_err = net._get_loss(y, init_out)
        net.track_progress(time.time(), init_err, X, y, Xvalid, yvalid)
        net.fit(X, y, Xvalid, yvalid, skip_output=skip_output)
        #if net.verbose and net.current_epoch % skip_output != 0: # make sure we show the last line
        #    net.track_progress(time.time(), -1, X, y, Xvalid, yvalid)
    except KeyboardInterrupt:
        print("Intercepted KeyboardInterrupt, stopping... current status:")
        net.track_progress(time.time(), -1, X, y, Xvalid, yvalid)
        net.statistics = net.statistics[:-1]  # we just added an invalid point
    finally:
        net = op.to_cpu(net)
        if fname:
            if not os.path.exists("data"):
                warnings.warn("creating 'data' directory to store pickled net")
                os.mkdir("data")
            with open(os.path.join("data", fname), "wb") as f:
                pickle.dump(net, f, -1)
        if show_plots:
            plot_images(net.weights[0], 16, 16, **kwargs)
            plot_learning_curves(net, **kwargs)
    return net