-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
172 lines (129 loc) · 6.18 KB
/
main.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from __future__ import print_function
from pprint import pformat
import tensorflow as tf
import itertools as it
from random import randint
import argparse
from rbm import RBM
import numpy as np
import json
def main()
# Initialize the command line parser
parser = argparse.ArgumentParser()
# Read command line arguments
parser.add_argument('command',type=str,help='command to execute')
parser.add_argument('-nV',type=int,default=4,help='number of visible nodes')
parser.add_argument('-nH',type=int,default=4,help='number of hidden nodes')
parser.add_argument('-steps',type=int,default=1000000,help='training steps')
parser.add_argument('-lr',type=float,default=1e-3,help='learning rate')
parser.add_argument('-bs',type=int,default=100,help='batch size')
parser.add_argument('-CD',type=int,default=10,help='steps of contrastive divergence')
parser.add_argument('-nC',type=float,default=10,help='number of chains in PCD')
# Parse the arguments
args = parser.parse_args()
if args.command == 'train':
train(args)
if args.command == 'sample':
sample(args)
class Args(object):
pass
class Placeholders(object):
pass
class Ops(object):
pass
def train(args):
# Simulation parameters
num_visible = args.nV # number of visible nodes
num_hidden = args.nH # number of hidden nodes
nsteps = args.steps # training steps
bsize = args.bs # batch size
learning_rate_b=args.lr # learning rate
num_gibbs = args.CD # number of Gibbs iterations
num_samples = args.nC # number of chains in PCD
weights=None # weights
visible_bias=None # visible bias
hidden_bias=None # hidden bias
bcount=0 # counter
epochs_done=1 # epochs counter
# *************************************************************
# INSERT HERE THE PATH TO THE TRAINING AND TESTING DATASETS
trainName = '*******************'
testName = '*******************'
# Loading the data
xtrain = np.loadtxt(trainName)
xtest = np.loadtxt(testName)
ept=np.random.permutation(xtrain) # random permutation of training data
epv=np.random.permutation(xtest) # random permutation of test data
iterations_per_epoch = xtrain.shape[0] / bsize # gradient iteration per epoch
# Initialize RBM class
rbm = RBM(num_hidden=num_hidden, num_visible=num_visible, weights=weights, visible_bias=visible_bias,hidden_bias=hidden_bias, num_samples=num_samples)
# Initialize operations and placeholders classes
ops = Ops()
placeholders = Placeholders()
placeholders.visible_samples = tf.placeholder(tf.float32, shape=(None, num_visible), name='v') # placeholder for training data
total_iterations = 0 # starts at zero
ops.global_step = tf.Variable(total_iterations, name='global_step_count', trainable=False)
# Decaying learning rate
learning_rate = tf.train.exponential_decay(
learning_rate_b,
ops.global_step,
100 * xtrain.shape[0]/bsize,
1.0 # decay rate =1 means no decay
)
cost = rbm.neg_log_likelihood_grad(placeholders.visible_samples, num_gibbs=num_gibbs)
optimizer = tf.train.AdamOptimizer(learning_rate, epsilon=1e-2)
# Define operations
ops.lr=learning_rate
ops.train = optimizer.minimize(cost, global_step=ops.global_step)
ops.init = tf.group(tf.initialize_all_variables(), tf.initialize_local_variables())
with tf.Session() as sess:
sess.run(ops.init)
for ii in range(nsteps):
if bcount*bsize+ bsize>=xtrain.shape[0]:
bcount=0
ept=np.random.permutation(xtrain)
batch=ept[ bcount*bsize: bcount*bsize+ bsize,:]
bcount=bcount+1
feed_dict = {placeholders.visible_samples: batch}
_, num_steps = sess.run([ops.train, ops.global_step], feed_dict=feed_dict)
if num_steps % iterations_per_epoch == 0:
print ('Epoch = %d ' % epochs_done)
#save_parameters(sess, rbm)
epochs_done += 1
def sample(args):
num_visible = args.nV # number of visible nodes
num_hidden = args.nH # number of hidden nodes
# *************************************************************
# INSERT HERE THE PATH TO THE PARAMETERS FILE
path_to_params = '*******************'
# Load the RBM parameters
params = np.load(path_to_params)
weights = params['weights']
visible_bias = params['visible_bias']
hidden_bias = params['hidden_bias']
hidden_bias=np.reshape(hidden_bias,(hidden_bias.shape[0],1))
visible_bias=np.reshape(visible_bias,(visible_bias.shape[0],1))
# Sampling parameters
num_samples=1000 # how many independent chains will be sampled
gibb_updates=100 # how many gibbs updates per call to the gibbs sampler
nbins=1000 # number of calls to the RBM sampler
# Initialize RBM class
rbm = RBM(num_hidden=num_hidden, num_visible=num_visible, weights=weights, visible_bias=visible_bias,hidden_bias=hidden_bias, num_samples=num_samples)
hsamples,vsamples=rbm.stochastic_maximum_likelihood(gibb_updates)
# Initialize tensorflow
init = tf.group(tf.initialize_all_variables(), tf.initialize_local_variables())
with tf.Session() as sess:
sess.run(init)
for i in range(nbins):
print ('bin %d\t' %i)
# Gibbs sampling
_,samples=sess.run([hsamples,vsamples])
def save_parameters(sess,rbm,epochs):
weights, visible_bias, hidden_bias = sess.run([rbm.weights, rbm.visible_bias, rbm.hidden_bias])
# *************************************************************
# INSERT HERE THE PATH TO THE PARAMETERS FILE
parameter_file_path = '*******************'
np.savez_compressed(parameter_file_path, weights=weights, visible_bias=visible_bias, hidden_bias=hidden_bias,
epochs=epochs)
if __name__=='__main__':
main()