Skip to content

Instantly share code, notes, and snippets.

View epignatelli's full-sized avatar
🍀

Eduardo Pignatelli epignatelli

🍀
View GitHub Profile
"""
This gist is an extract from:
https://github.com/epignatelli/reinforcement-learning-an-introduction
"""
def bellman_expectation(self, state, probs, discount):
"""
Makes a one step lookahead and applies the bellman expectation equation to the state self.state_value[state]
Args:
state (Tuple[int, int]): the x, y indices that define the address on the value table
"""
This gist is an extract from:
https://github.com/epignatelli/reinforcement-learning-an-introduction
"""
import matplotlib
matplotlib.use("Agg")
import numpy as np
import matplotlib.pyplot as plt
@epignatelli
epignatelli / torch.utils.data.dataset.py
Last active March 24, 2023 15:30
pytorch Subset to return an instance of the parent Dataset, to be able to access the same attribute
"""
An implementation of the pytorch Subset that returns an instance of the original dataset with a reduced number of items.
This has two benefits:
- It allows to stil access the attributes of the Dataset class, such as methods, or properties.
- You can use the usual python index notation with slices to chunk the dataset, rather than creating a list of indices
"""
class Dataset(object):
def __init__(self, iterable):
self.items = iterable
@epignatelli
epignatelli / torch.cuda.is_capable.py
Last active April 5, 2020 12:52
torch.cuda.is_capable
import torch
def is_supported():
CUDA_VERSION = torch._C._cuda_getCompiledVersion()
supported = True
for d in range(torch.cuda.device_count()):
capability = torch.cuda.get_device_capability(d)
major = capability[0]
minor = capability[1]
supported &= major > 3 # too old
@epignatelli
epignatelli / hashlib_timeit.py
Last active April 2, 2020 12:40
Testing the performance of different hashing algorithms for hashing a numpy array
import hashlib
funcs = [
hashlib.blake2b,
hashlib.blake2s,
hashlib.md5,
hashlib.sha1,
hashlib.sha224,
hashlib.sha256,
hashlib.sha256,
@epignatelli
epignatelli / profile.py
Last active April 3, 2020 13:12
Python profiling decorator
def profiled(func):
def decorated(*args, **kwargs):
pr = cProfile.Profile()
pr.enable()
result = func(*args, **kwargs)
pr.disable()
pr.print_stats(sort="tottime")
return result
@epignatelli
epignatelli / SubsetProxy.py
Created October 29, 2019 19:23
A torch.utils.data.Subset that still retains the access to the parent Dataset properties and functions
from torch.utils.data import Subset
from sklearn.model_selection import train_test_split
class SubsetProxy(Subset):
def __init__(self, dataset, indices):
super().__init__(dataset, indices)
def __getattr__(self, name):
try:
@epignatelli
epignatelli / out_shape.py
Created September 18, 2019 18:08
output shapes of the more common convolutions and pooling operations in deep learning
import math
def conv1d_out_shape(input_size, kernel_size, stride=1, padding=0, dilation=1):
output_size = ((input_size + (2 * padding[0]) - dilation[0] * (kernel_size[0] - 1) - 1) / stride[0]) + 1
return math.floor(output_size)
def transpose_conv1d_out_shape(input_size, kernel_size, stride=1, padding=0, dilation=1):
output_size = ((input_size - 1) * stride) - (2 * padding) + (dilation * (kernel_size - 1))
@epignatelli
epignatelli / private.py
Created September 10, 2019 15:32
Python @Private function decorator
import sys, functools
def private(member):
@functools.wraps(member)
def wrapper(*function_args):
myself = member.__name__
caller = sys._getframe(1).f_code.co_name
if (not caller in dir(function_args[0]) and not caller is myself):
raise PermissionError(f"Cannot call private function {caller}.{myself}")
return member(*function_args)