Test if GPU is used or not

So from the server terminal, first go to the bash environment, then start the test code via THEANO_FLAGS='floatX=float32,device=gpu0,nvcc.flags=-D_FORCE_INLINES ' python /aa/bb/cc/test1.py

import scipy.io as sio

import theano.sandbox.cuda

# theano.sandbox.cuda.use("gpu0")

i = 1

while True:

mat2 = sio.loadmat('/user/xyz/Desktop/Test1.mat')

i += 1

print(i)

The following pops up:

Using gpu device 0: NAME_OF_THE_GPU (CNMeM is disabled, cuDNN not available)

From the serveral terminal, run "watch -n 1 nvidia-smi" to check the gpu usage status and the GPU is used.

To load a Theano GPU model on a CPU-only machine

The main idea is to save the parameters of the layers (i.e. transition and bias) directly. And on a CPU machine, generate the same structure, and load these parameters for each layer.

The first step: serialisation of the GPU model on a GPU machine

import theanets

import theano.sandbox.cuda

LossFunName = 'mse'

inputlayer = dict(name='myinput', size=1234)

hidlayer1 = dict(name='myhid1', activation='relu', size=1234)#, std=0.03)

hidlayer2 = dict(name='myhid2', activation='relu', size=1234)#, std=0.03)#, sparsity=0.9)

hidlayer3 = dict(name='myhid3', activation='relu', size=1234)#, std=0.03)#, sparsity=0.9)

outputlayer = dict(name='myoutput', size=1234)#, std=0.05)

exp = theanets.Experiment(NetworkType, layers=[inputlayer, hidlayer1, hidlayer2, hidlayer3, outputlayer], loss=LossFunName, weighted=False, rng=12345)

# convert the network to lists of tuples storing parameters

def to_param_list(network):

newlayers = list()

for layer in network.layers:

params = list()

for param in layer.params:

name = str(param)

values = param.get_value()

# print(type(values))

params.append((name,values))

newlayers.append(params)

return newlayers

GPUModelName = '/aabbcc/GPUModelName.save'

CPUModelName = '/aabbcc/CPUModelName.save'

exp.load(GPUModelName)

param_list = to_param_list(exp.network)

import pickle

with open(CPUModelName,'w') as f:

pickle.dump(param_list,f)

The second step: load the params to the model on a CPU machine

import theanets

import numpy as np

import pickle

LossFunName = 'mse'

inputlayer = dict(name='myinput', size=1234)

hidlayer1 = dict(name='myhid1', activation='relu', size=1234)#, std=0.03)

hidlayer2 = dict(name='myhid2', activation='relu', size=1234)#, std=0.03)#, sparsity=0.9)

hidlayer3 = dict(name='myhid3', activation='relu', size=1234)#, std=0.03)#, sparsity=0.9)

outputlayer = dict(name='myoutput', size=1234)#, std=0.05)

exp = theanets.Experiment(NetworkType, layers=[inputlayer, hidlayer1, hidlayer2, hidlayer3, outputlayer], loss=LossFunName, weighted=False, rng=12345)

ModelName = '/aabbcc/CPUModelName.save'

# Load the saved model

# take a network of the same configuration and load it up

def set_network(network, param_list):

for layeri in range(0,len(network.layers)):

# print layeri

player = param_list[layeri]

layer = network.layers[layeri]

for parami in range(0,len(layer.params)):

pparam = player[parami]

param = layer.params[parami]

if not str(param) == pparam[0]:

raise Exception(" %s != %s ", str(param), pparam[0])

param.set_value(pparam[1])

try:

print('try to load the GPU-->CPU params')

param_list = pickle.load(open(ModelName, 'rb'))

set_network(exp.network, param_list)

except:

print('try to load the model directly')

exp.load(ModelName)