Skip to content

Instantly share code, notes, and snippets.

View tokestermw's full-sized avatar

Motoki Wu tokestermw

View GitHub Profile
@tokestermw
tokestermw / pg-pong.py
Created June 1, 2016 06:01 — forked from karpathy/pg-pong.py
Training a Neural Network ATARI Pong agent with Policy Gradients from raw pixels
""" 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
@tokestermw
tokestermw / KeyValueMemNN.md
Created June 30, 2016 21:01 — forked from shagunsodhani/KeyValueMemNN.md
Summary of paper "Key-Value Memory Networks for Directly Reading Documents"

Key-Value Memory Networks for Directly Reading Documents

Introduction

  • Knowledge Bases (KBs) are effective tools for Question Answering (QA) but are often too restrictive (due to fixed schema) and too sparse (due to limitations of Information Extraction (IE) systems).
  • The paper proposes Key-Value Memory Networks, a neural network architecture based on Memory Networks that can leverage both KBs and raw data for QA.
  • The paper also introduces MOVIEQA, a new QA dataset that can be answered by a perfect KB, by Wikipedia pages and by an imperfect KB obtained using IE techniques thereby allowing a comparison between systems using any of the three sources.
  • Link to the paper.

Related Work

@tokestermw
tokestermw / glove.sh
Last active July 20, 2016 01:09
script to make word2vec format proper for gensim, then binarize, also save normed and unnormed vectors
vocab=400000
tokens=42
dim=300
filename="glove.${tokens}B.${dim}d"
n="_unnorm"
txt=".txt"
bin=".bin"
echo "${vocab} ${dim}" > $filename$n$txt
cat $filename$txt >> $filename$n$txt
@tokestermw
tokestermw / disk_memoize.py
Last active August 24, 2016 23:24
decorator memoize to file using cPickle (mostly use with caching vectorizers and featurizers so can focus on optimizing model [hyper]params)
"""
from here: https://github.com/petered/plato/pull/56/files
adapted by Motoki
"""
import hashlib
from collections import OrderedDict
import numpy as np
import cPickle as pickle
import os
@tokestermw
tokestermw / incremental_sparse_matrix.py
Last active March 20, 2019 12:49
Incrementally creating sparse matrices are memory-intensive (had some trouble when data size was large). this is an alternative using the array module. ref: http://maciejkula.github.io/2015/02/22/incremental-sparse-matrices/
import array
import numpy as np
import scipy.sparse as sp
class IncrementalCOOMatrix(object):
def __init__(self, shape, dtype):
if dtype is np.int32:
@tokestermw
tokestermw / wilson_lower_bound.py
Last active August 31, 2016 17:46
use probability distributions instead of ratios or counts
from __future__ import division
def average(x):
return sum(x) / len(x)
def wilson_lower_bound(count, total, z_score=1.96):
""" Implementation of Wilson Scores
@tokestermw
tokestermw / q.py
Created September 22, 2016 18:23 — forked from fheisler/q.py
Q-learning Tic-tac-toe
import random
class TicTacToe:
def __init__(self, playerX, playerO):
self.board = [' ']*9
self.playerX, self.playerO = playerX, playerO
self.playerX_turn = random.choice([True, False])
def play_game(self):
@tokestermw
tokestermw / rnn_viz_keras.py
Last active April 6, 2019 18:40
Recurrent Neural Network (RNN) visualizations using Keras.
from __future__ import print_function
from keras import backend as K
from keras.engine import Input, Model, InputSpec
from keras.layers import Dense, Activation, Dropout, Lambda
from keras.layers import Embedding, LSTM
from keras.optimizers import Adam
from keras.preprocessing import sequence
from keras.utils.data_utils import get_file
from keras.datasets import imdb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@tokestermw
tokestermw / restore_tf_models.py
Created February 21, 2017 21:09
Restoring frozen models are hard in TensorFlow.
"""
Play with saving .
Closest:
https://github.com/tensorflow/tensorflow/issues/616#issuecomment-205620223
"""
import numpy as np
import tensorflow as tf
from tensorflow.python.platform import gfile