Skip to content

Instantly share code, notes, and snippets.

View jojonki's full-sized avatar

Junki Ohmura jojonki

View GitHub Profile
@jojonki
jojonki / gym-CartPole-v0-test.py
Created January 2, 2017 14:33
CartPole-v0 which is OpenAI's gym.
# -*- coding: utf-8 -*-
import gym
from gym import wrappers
env = gym.make("CartPole-v0")
env = wrappers.Monitor(env, './cartpole-experiment-1')
for i_episode in range(20):
observation = env.reset()
for t in range(100):
@jojonki
jojonki / FrozenLake-V0-QLearning.py
Last active May 19, 2022 17:47
FrozenLake-v0 with Q learning
# -*- coding: utf-8 -*-
# ref: https://gym.openai.com/evaluations/eval_1lfzNKEHS9GA7nNWE73w
import numpy as np
import gym
from gym import wrappers
# Q learning params
ALPHA = 0.1 # learning rate
GAMMA = 0.99 # reward discount
# -*- coding: utf-8 -*-
# ref: http://taotao54321.hatenablog.com/entry/2016/11/08/180245
import numpy as np
import gym
from gym import wrappers
# Q learning params
ALPHA = 0.1 # learning rate
GAMMA = 0.99 # reward discount
@jojonki
jojonki / pg-pong.py
Last active January 6, 2017 08:47 — 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 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
@jojonki
jojonki / variational_autoencoder.py
Created May 5, 2017 15:34
fix vae's x shape; not using batch
'''This script demonstrates how to build a variational autoencoder with Keras.
Reference: "Auto-Encoding Variational Bayes" https://arxiv.org/abs/1312.6114
'''
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from keras.layers import Input, Dense, Lambda, Layer
from keras.models import Model
from keras import backend as K
@jojonki
jojonki / CustomTrainingParams.py
Created November 10, 2017 22:16
Add custom trainable parameters in PyTorch
import random
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch import optim
import torch.nn.functional as F
seq_len = 10
n_class = 5
class SimpleNet(nn.Module):
@jojonki
jojonki / return_indices_by_value.py
Last active November 14, 2017 22:07
return indices by value
# https://stackoverflow.com/questions/7851077/how-to-return-index-of-a-sorted-list
s = [2, 3, 1, 4, 5]
sorted(range(len(s)), key=lambda k: s[k])
# [2, 0, 1, 3, 4]
sorted(range(len(s)), key=lambda k: s[k], reverse=True)
# [4, 3, 1, 0, 2]
@jojonki
jojonki / heatmap.ipynb
Last active November 21, 2017 21:54
Word alignment heatmap in matplotlib
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jojonki
jojonki / auto_load_extenton.ipynb-dummy
Last active December 18, 2017 21:40
Load libraries dynamically in IPython
%load_ext autoreload
%autoreload 2
@jojonki
jojonki / python_memory_print.py
Created November 24, 2017 03:25
print memory usage in python
# https://discuss.pytorch.org/t/how-pytorch-releases-variable-garbage/7277/2
def memReport():
for obj in gc.get_objects():
if torch.is_tensor(obj) or (hasattr(obj, 'data') and torch.is_tensor(obj.data)):
print(type(obj), obj.size())
def cpuStats():
print(sys.version)
print(psutil.cpu_percent())
print(psutil.virtual_memory()) # physical memory usage