-
Notifications
You must be signed in to change notification settings - Fork 34
/
utils_pg.py
35 lines (28 loc) · 874 Bytes
/
utils_pg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pylint: skip-file
import numpy as np
import theano
import theano.tensor as T
import cPickle as pickle
# set use gpu programatically
import theano.sandbox.cuda
def use_gpu(gpu_id):
if gpu_id > -1:
theano.sandbox.cuda.use("gpu" + str(gpu_id))
def floatX(X):
return np.asarray(X, dtype=theano.config.floatX)
def init_weights(shape, name):
return theano.shared(floatX(np.random.randn(*shape) * 0.1), name)
def init_gradws(shape, name):
return theano.shared(floatX(np.zeros(shape)), name)
def init_bias(size, name):
return theano.shared(floatX(np.zeros((size,))), name)
def save_model(f, model):
ps = {}
for p in model.params:
ps[p.name] = p.get_value()
pickle.dump(ps, open(f, "wb"))
def load_model(f, model):
ps = pickle.load(open(f, "rb"))
for p in model.params:
p.set_value(ps[p.name])
return model