This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
from numpy import * | |
class AdaBoost: | |
def __init__(self, training_set): | |
self.training_set = training_set | |
self.N = len(self.training_set) | |
self.weights = ones(self.N)/self.N | |
self.RULES = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Trains an agent with (stochastic) Policy Gradients on Pong. Uses OpenAI Gym. """ | |
import numpy as np | |
import cPickle as pickle | |
import gym | |
# hyperparameters | |
H = 200 # number of hidden layer neurons | |
batch_size = 10 # every how many episodes to do a param update? | |
learning_rate = 1e-4 | |
gamma = 0.99 # discount factor for reward |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import theano | |
import theano.tensor as T | |
from theano.tensor.shared_randomstreams import RandomStreams | |
from theano.sandbox.rng_mrg import MRG_RandomStreams | |
from lasagne.updates import adam | |
from lasagne.utils import collect_shared_vars | |
from sklearn.datasets import fetch_mldata | |
from sklearn.cross_validation import train_test_split | |
from sklearn import preprocessing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def backward_grads(self, y, dy, training=True): | |
dy1, dy2 = dy | |
y1, y2 = y | |
with tf.GradientTape() as gtape: | |
gtape.watch(y1) | |
gy1 = self.g(y1, training=training) | |
grads_combined = gtape.gradient( | |
gy1, [y1] + self.g.trainable_variables, output_gradients=dy2) | |
dg = grads_combined[1:] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
checkpoint = tf.train.Checkpoint(model=model, optimizer=optimizer, | |
learning_rate=learning_rate, global_step=global_step) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
checkpoint.save(file_prefix) | |
checkpoint.restore(save_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tfe = tf.contrib.eager | |
model.call = tfe.defun(model.call) | |
model.compute_gradients = tfe.defun(model.compute_gradients) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def apply_gradients(optimizer, gradients, variables, global_step=None): | |
optimizer.apply_gradients( | |
zip(gradients, variables), global_step=global_step) | |
apply_gradients = tfe.defun(apply_gradients) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dataset = tf.data.TFRecordDataset(filename) | |
dataset = dataset.repeat(epochs).map(parser).batch(batch_size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for image, label in dataset: | |
logits = model(image, training=True) | |
... |
OlderNewer