Skip to content

Instantly share code, notes, and snippets.

import glob
import os
from pprint import pprint
import re
import shutil
RESULT_FILE_REGEXES = (
"^result.json$",
"^progress.csv$",
import os
import sys
from shutil import copyfile
import numpy as np
import pandas as pd
def get_result_path(trial_dir):
return os.path.join(trial_dir, "progress.csv")
@hartikainen
hartikainen / replay_pool_timing.py
Created January 13, 2019 03:56
Time RL replay pool sampling with numpy pool vs. array pool
from collections import defaultdict
import time
import matplotlib.pyplot as plt
import numpy as np
def create_fields(observation_shape, action_shape):
fields = {
'observations': observation_shape,
@hartikainen
hartikainen / picklable_keras_model.py
Created December 20, 2018 02:33
Extend keras Model class to make it picklable.
import tempfile
import tensorflow as tf
class PicklableKerasModel(tf.keras.Model):
def __getstate__(self):
with tempfile.NamedTemporaryFile(suffix='.hdf5', delete=True) as fd:
tf.keras.models.save_model(self, fd.name, overwrite=True)
model_str = fd.read()
@hartikainen
hartikainen / latent_space_policy_training_example.ipynb
Last active November 6, 2018 21:06
Simple latent space policy training example
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
class LearnableConditionalRealNVP(object):
def __init__(self, input_shape, output_shape):
self._input_shape = input_shape
self._output_size = np.prod(output_shape)
@hartikainen
hartikainen / float_lookup_layer
Last active September 25, 2018 02:30
FloatLookupLayer that can used to replace keras estimator when doing simple debugging without models.
class FloatLookupLayer(tf.keras.layers.Layer):
def __init__(self, all_observation_pairs, **kwargs):
self.all_observation_pairs = all_observation_pairs
x = np.reshape(
self.all_observation_pairs,
(self.all_observation_pairs.shape[0],
np.prod(self.all_observation_pairs.shape[1:3])))
x = tf.reduce_join(tf.as_string(x), separator='|', axis=-1)
x = tf.concat((x, ['ERROR']), axis=0)
# Base softlearning container that contains all softlearning requirements,
# but not the actual softlearning repo. Could be used for example when developing
# softlearning, in which case you would mount softlearning repo in to the container
# as a volume, and thus be able to modify code on the host, yet run things inside
# the container. You are encouraged to use docker-compose (docker-compose.dev.yml),
# which should allow you to setup your environment with a single one command.
FROM nvidia/cuda:9.0-runtime-ubuntu16.04
ARG MJKEY
@hartikainen
hartikainen / split_data.py
Last active May 29, 2017 04:17
Split a dataset (X, y) where X is shape (N, D_in) and y is shape (N, D_out) into train, validation, and test sets. Also, possibly create a small dataset for development purposes.
import numpy as np
from sklearn.model_selection import train_test_split
def split_data(X, y, train_prop, val_prop, test_prop, N_dev=0, verbose=True):
""" Split the dataset (X, y) into train, validation, and test sets.
Also, possibly create a small dataset for development purposes. Note that
the proportions should sum to 1.
Arguments:
X -- Feature matrix, shape of (N, D_in)