k = p.name d[k] = p.get_value(borrow=False) np.savez(fname, **d) def load_parameters(epoch=None): fname = "weights.npz" if epoch is None else "weights.%d.npz" % epoch f = np.load(fname) for p in parameters: p.set_value(f[p.name]) def report(d): import json with open("learning_curve.txt", "a") as fh: fh.write(json.dumps(d)) fh.write("\n") print "epoch=%-6s -- trn_cost=%0.8f tst_cost=%0.8f" % (d['_epoch'], d['trn'], d['tst']) if __name__ == '__main__': # generate some simple training data import uuid run_name = uuid.uuid4().hex[:8] n_samples = 10000 x_train_val, y_train_val = datasets.sinewaves(n_steps, n=n_samples) x_test_val, y_test_val = datasets.sinewaves( n_steps, n=20) # (n_samples, n_steps, n_x) train(x_train_val, y_train_val, x_test_val, y_test_val)
def train(x_train_val, y_train_val): x_train = theano.shared(x_train_val) y_train = theano.shared(y_train_val) n_samples = x_train_val.shape[0] # creating training function learn_fn = make_learner(x_train, y_train, param_updates) # start gradient descent w/ batch_size==1 max_epochs = 1000 lr_val = 0.5 for epoch in range(max_epochs): costs = [] for idx in range(n_samples): sample_cost = learn_fn(idx, lr_val) costs.append(sample_cost[0]) print "epoch=%-6s -- cost=%0.8f" % (epoch, np.mean(costs)) if __name__ == '__main__': # generate some simple training data n_samples = 100 timesteps = 20 x_train_val, y_train_val = datasets.sinewaves(timesteps, n=n_samples) train(x_train_val, y_train_val) predictor = make_predictor()
for p in parameters: k = p.name d[k] = p.get_value(borrow=False) np.savez("weights.npz", **d) def load_parameters(): f = np.load("weights.npz") for p in parameters: p.set_value(f[p.name]) def report(d): import json with open("learning_curve.txt", "a") as fh: fh.write(json.dumps(d)) fh.write("\n") print "epoch=%-6s -- trn_cost=%0.8f tst_cost=%0.8f" % (d["_epoch"], d["trn"], d["tst"]) if __name__ == "__main__": # generate some simple training data import uuid run_name = uuid.uuid4().hex[:8] n_samples = 1 x_train_val, y_train_val = datasets.sinewaves(n_steps, n=n_samples) x_test_val, y_test_val = datasets.sinewaves(n_steps, n=20) train(x_train_val, y_train_val, x_test_val, y_test_val)