Skip to content

Instantly share code, notes, and snippets.

View dsantiago's full-sized avatar

Diogo Santiago dsantiago

View GitHub Profile
@dsantiago
dsantiago / Dict2Prop.py
Created April 20, 2021 18:10
Convert dict to cal propoeries with "."
class dotaccess(dict):
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
ml_eng = {
'name': 'Diogo Santiago',
'link': 'http://github.com/dsantiago/'
}
@dsantiago
dsantiago / KerasBar.py
Created April 15, 2021 22:12
Add Keras like progress bar to Pytorch-Lightning
class KerasProgressBar(pl.callbacks.progress.ProgressBarBase):
def on_train_start(self, trainer, pl_module):
super().on_train_start(trainer, pl_module)
pbar = tf.keras.utils.Progbar(trainer.num_training_batches)
self.keras_bar = pbar
def on_train_epoch_start(self, trainer, pl_module):
super().on_train_epoch_start(trainer, pl_module)
print(f'Epoch {trainer.current_epoch+1}/{trainer.max_epochs}')
import numpy as np
import torch
crit = torch.nn.BCELoss()
preds = torch.tensor([[1, 0.2, 0.3, 1, 1], [1, 0.1, 0.7, 1, 1]]).float() # n classes
targets = torch.tensor([[1, 1, 1, 0, 0], [1, 0, 1, 1, 1]]).float() # n classes 0 or 1
print(preds, targets)
imgs = np.arange(36).reshape(9, 2, 2)
print(imgs)
"""
array([[[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7]],
@dsantiago
dsantiago / manual_feedforwards.py
Last active April 5, 2021 01:36
Manual Feedforwards
def myloss(y, y_pred):
return ((y - y_pred) ** 2).mean()
np.random.seed(42)
x = np.arange(1, 21)
np.random.shuffle(x)
y = x * 2 - 1
x = torch.FloatTensor(x).view(-1, 1)
y = torch.FloatTensor(y).view(-1, 1)
@dsantiago
dsantiago / ConvCalculation.md
Last active April 20, 2021 18:26
Convolution Formula

Conv:

o = output
p = padding
k = kernel_size
s = stride
d = dilation

o = [i + 2*p - k - (k-1)*(d-1)]/s + 1