Skip to content

Instantly share code, notes, and snippets.

View astoeffelbauer's full-sized avatar

Andreas Stöffelbauer astoeffelbauer

View GitHub Profile
class QLearningAgent(acme.Actor):
def __init__(self, env_specs=None, step_size=0.1):
# Black Jack dimensions
self.Q = np.zeros((32,11,2,2))
# set step size
self.step_size = step_size
class SarsaAgent(acme.Actor):
def __init__(self, env_specs=None, epsilon=0.1, step_size=0.1):
# in Black Jack, we have the following dimensions
self.Q = np.zeros((32,11,2,2))
# epsilon for policy and step_size for TD learning
self.epsilon = epsilon
self.step_size = step_size
# first initialize env and agent
# env = ...
agent = RandomAgent()
# repeat for a number of episodes
for episode in range(10):
# make first observation
timestep = env.reset()
agent.observe_first(timestep)
class RandomAgent(acme.Actor):
"""A random agent for the Black Jack environment."""
def __init__(self):
# init action values, will not be updated by random agent
self.Q = np.zeros((32,11,2,2))
# specify the behavior policy
self.behavior_policy = lambda q_values: np.random.choice(2)
@astoeffelbauer
astoeffelbauer / pytorch_sequential.py
Last active February 2, 2020 17:44
PyTorch sequential
# PyTorch nn.Sequential
model = nn.Sequential(
nn.Embedding(num_embeddings=20000, embedding_dim=50),
nn.AvgPool1d(kernel_size=50),
nn.Flatten(start_dim=1),
nn.Linear(in_features=42, out_features=1),
nn.Sigmoid()
)
@astoeffelbauer
astoeffelbauer / pytorch_subclassing.py
Last active February 2, 2020 17:28
PyTorch subclassing
# PyTorch nn.Module Subclassing
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.embedding_layer = nn.Embedding(num_embeddings=20000,
embedding_dim=50)
self.pooling_layer = nn.AvgPool1d(kernel_size=50)
self.fc_layer = nn.Linear(in_features=42, out_features=1)
@astoeffelbauer
astoeffelbauer / keras_functional_api.py
Created January 24, 2020 18:47
Keras functional API
inputs = tf.keras.layers.Input(shape=(42,))
x = tf.keras.layers.Embedding(input_dim=20000,
output_dimension=50,
input_length=42,
mask_zero=True)(inputs)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(128, activation='relu')(x)
outputs = tf.keras.layers.Dense(1, activation='sigmoid')(x)
model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
@astoeffelbauer
astoeffelbauer / keras_subclassing_api.py
Last active February 3, 2020 18:36
Keras subclassing API
class Subclass_Model(tf.keras.Model):
def __init__(self):
super(Subclass_Model, self).__init__()
self.embedding_layer = tf.keras.layers.Embedding(input_dim=20000,
output_dimension=50,
input_length=42,
mask_zero=True)
self.flatten_layer = tf.keras.layers.Flatten()
self.fc1_layer = tf.keras.layers.Dense(128, activation='relu')
@astoeffelbauer
astoeffelbauer / keras_sequential_api.py
Last active January 24, 2020 18:45
Keras sequential API
model = tf.keras.Sequential([
tf.keras.layers.Embedding(input_dim=20000,
output_dimension=50,
input_length=42,
mask_zero=True),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')])
@astoeffelbauer
astoeffelbauer / pytorch_simple_trainloop.py
Created January 24, 2020 17:42
A simple training loop in PyTorch
#define the loss fn and optimizer
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
#initialize empty list to track batch losses
batch_losses = []
#train the neural network for 5 epochs
for epoch in range(5):