Skip to content

Instantly share code, notes, and snippets.

View kashif's full-sized avatar

Kashif Rasul kashif

  • Berlin, Germany
  • 16:40 (UTC +02:00)
  • X @krasul
View GitHub Profile
@kashif
kashif / cifar10_resnet.py
Last active February 3, 2021 09:06
Keras Pre-activation Residual Network for CIFAR-10
from __future__ import print_function
from keras.datasets import cifar10
from keras.layers import merge, Input
from keras.layers.convolutional import Convolution2D, ZeroPadding2D, AveragePooling2D
from keras.layers.core import Dense, Activation, Flatten
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import np_utils
@kashif
kashif / cifar10_wide_resnet.py
Last active May 10, 2021 02:44
Keras Wide Residual Networks CIFAR-10
from __future__ import print_function
from keras.datasets import cifar10
from keras.layers import merge, Input
from keras.layers.convolutional import Convolution2D, ZeroPadding2D, AveragePooling2D
from keras.layers.core import Dense, Activation, Flatten, Dropout
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import np_utils
@kashif
kashif / cem.md
Last active November 7, 2023 12:56
Cross Entropy Method

Cross Entropy Method

How do we solve for the policy optimization problem which is to maximize the total reward given some parametrized policy?

Discounted future reward

To begin with, for an episode the total reward is the sum of all the rewards. If our environment is stochastic, we can never be sure if we will get the same rewards the next time we perform the same actions. Thus the more we go into the future the more the total future reward may diverge. So for that reason it is common to use the discounted future reward where the parameter discount is called the discount factor and is between 0 and 1.

A good strategy for an agent would be to always choose an action that maximizes the (discounted) future reward. In other words we want to maximize the expected reward per episode.

name: "autocolorize"
input: "data"
input_dim: 1
input_dim: 1
input_dim: 514
input_dim: 514
layer {
name: "data"
type: "Input"
top: "data"
@kashif
kashif / es.py
Last active June 5, 2017 12:07
Initial implementation of Evolution Strategies
import numpy as np
import gym
from gym.spaces import Discrete, Box
from gym.wrappers import Monitor
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
# ================================================================
# Policies
@kashif
kashif / fashion_mnist_cnn.py
Last active October 26, 2022 22:04
Fashion Mnist Benchmark
'''Trains a simple convnet on the Zalando MNIST dataset.
Gets to 81.03% test accuracy after 30 epochs
(there is still a lot of margin for parameter tuning).
3 seconds per epoch on a GeForce GTX 980 GPU with CuDNN 5.
'''
from __future__ import print_function
import numpy as np
from mnist import MNIST
@kashif
kashif / amsgrad.py
Last active May 13, 2019 14:21
Keras implementation of AMSGrad optimizer from "On the Convergence of Adam and Beyond" paper
class AMSgrad(Optimizer):
"""AMSGrad optimizer.
Default parameters follow those provided in the Adam paper.
# Arguments
lr: float >= 0. Learning rate.
beta_1: float, 0 < beta < 1. Generally close to 1.
beta_2: float, 0 < beta < 1. Generally close to 1.
epsilon: float >= 0. Fuzz factor.
@kashif
kashif / acc_sgd.py
Created March 16, 2018 11:32
AccSGD optimizer for keras
class AccSGD(Optimizer):
"""AccSGD optimizer.
Arguments:
lr (float): learning rate
kappa (float, optional): ratio of long to short step (default: 1000)
xi (float, optional): statistical advantage parameter (default: 10)
smallConst (float, optional): any value <=1 (default: 0.7)
# References
@kashif
kashif / input_fn.py
Last active March 2, 2019 16:42
TensorFlow 1.x Estimator input pipeline function to read images organised in their class folders
def input_fn(file_pattern, labels,
image_size=(224,224),
shuffle=False,
batch_size=64,
num_epochs=None,
buffer_size=4096,
prefetch_buffer_size=None):
table = tf.contrib.lookup.index_table_from_tensor(mapping=tf.constant(labels))
num_classes = len(labels)
@kashif
kashif / evonorm2d.py
Last active April 10, 2020 22:40
EvoNorm-S0 in PyTorch from https://arxiv.org/pdf/2004.02967.pdf
import torch
import torch.nn as nn
class EvoNorm2d(nn.Module):
__constants__ = ['num_features', 'eps', 'nonlinearity']
def __init__(self, num_features, eps=1e-5, nonlinearity=True):
super(EvoNorm2d, self).__init__()