From 01d92e5d87f1abe1870b544fc167e500e392e1a1 Mon Sep 17 00:00:00 2001 From: "Team AiGameDev.com" Date: Thu, 2 Apr 2015 15:29:07 +0200 Subject: [PATCH] Support for using the GPU by default, then falling back to the CPU if none is found. --- README.rst | 1 - sknn/mlp.py | 10 ++++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index f9287b3..dd9b5de 100644 --- a/README.rst +++ b/README.rst @@ -12,7 +12,6 @@ Upcoming Features * An example that covers hand-written digit recognition from the MNIST dataset. * Better error checking for the layer specifications, useful messages otherwise. -* Support for automatically using the GPU, with manually specified CPU fallback. * Use pylearn2's monitoring code to stop training upon detecting convergence. diff --git a/sknn/mlp.py b/sknn/mlp.py index d4a6d99..423d5af 100644 --- a/sknn/mlp.py +++ b/sknn/mlp.py @@ -2,13 +2,16 @@ __all__ = ['MultiLayerPerceptronRegressor', 'MultiLayerPerceptronClassifier'] - +import os import logging log = logging.getLogger('sknn') +# By default, we force Theano to use a GPU and fallback to CPU, using 32-bits. +# This must be done in the code before Theano is imported for the first time. +os.environ['THEANO_FLAGS'] = "device=gpu,floatX=float32" +import theano import numpy -import theano import sklearn.base import sklearn.preprocessing @@ -322,6 +325,9 @@ def predict(self, X): y = numpy.zeros((X.shape[0], self.unit_counts[-1])) self.initialize(X, y) + if X.dtype != numpy.float32: + X = X.astype(numpy.float32) + if self.is_convolution: X = numpy.array([X]).transpose(1,2,3,0)