Skip to content

Instantly share code, notes, and snippets.

@peterroelants
peterroelants / gist:4946cdbf189c5e75f2b7
Created November 20, 2014 19:23
Theano gradient of sparse matrix mulitplication
{
"metadata": {
"name": "",
"signature": "sha256:14e43d708aea630a4c8f33c6cabbbe3227646db0888b99449a3db1735adc1a9d"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
@peterroelants
peterroelants / gist:207bac20abd2adf5205d
Created November 21, 2014 16:37
Illustration of computing the gradient of a sparse sum
{
"metadata": {
"name": "",
"signature": "sha256:4a13ffd7eefcebc41dd8407752bb82616665a8867e1e2e6e28d10191a393b233"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"metadata": {
"name": "",
"signature": "sha256:beeb4a3b3af525f231ff90355670685335a8c0d936dfde90970b9dae83b73b01"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"metadata": {
"name": "",
"signature": "sha256:32eee8b1ca84be1664af41531e486282214748e0d105940b99c250a717c02333"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
@peterroelants
peterroelants / estimator_creation.py
Last active August 24, 2017 11:43
TensorFlow Estimator creation
return tf.estimator.Estimator(
model_fn=model_fn, # First-class function
params=params, # HParams
config=run_config # RunConfig
)
@peterroelants
peterroelants / create_experiment.py
Last active August 24, 2017 11:49
TensorFlow Experiment creation
experiment = tf.contrib.learn.Experiment(
estimator=estimator, # Estimator
train_input_fn=train_input_fn, # First-class function
eval_input_fn=eval_input_fn, # First-class function
train_steps=params.train_steps, # Minibatch steps
min_eval_frequency=params.min_eval_frequency, # Eval frequency
train_monitors=[train_input_hook], # Hooks for training
eval_hooks=[eval_input_hook], # Hooks for evaluation
eval_steps=None # Use evaluation feeder until its empty
)
@peterroelants
peterroelants / run_experiment.py
Created August 24, 2017 11:51
TensorFlow run Experiment
learn_runner.run(
experiment_fn=experiment_fn, # First-class function
run_config=run_config, # RunConfig
schedule="train_and_evaluate", # What to run
hparams=params # HParams
)
# Define the training inputs
def get_train_inputs(batch_size, mnist_data):
"""Return the input function to get the training data.
Args:
batch_size (int): Batch size of training iterator that is returned
by the input function.
mnist_data (Object): Object holding the loaded mnist data.
Returns:
@peterroelants
peterroelants / iterator_initializer_hook.py
Created August 24, 2017 11:56
Iterator Initializer Hook
class IteratorInitializerHook(tf.train.SessionRunHook):
"""Hook to initialise data iterator after Session is created."""
def __init__(self):
super(IteratorInitializerHook, self).__init__()
self.iterator_initializer_func = None
def after_create_session(self, session, coord):
"""Initialise the iterator after the session has been created."""
self.iterator_initializer_func(session)
@peterroelants
peterroelants / create_estimator.py
Created June 6, 2018 16:33
Create a tensorflow estimator
return tf.estimator.Estimator(
model_fn=model_fn,
config=config,
params=params,
)