Skip to content

Instantly share code, notes, and snippets.

View ikhlestov's full-sized avatar
🐍
Coding

Illarion ikhlestov

🐍
Coding
View GitHub Profile
@ikhlestov
ikhlestov / weights_initialization.py
Created September 12, 2017 17:18
pytorch: weights initialization
import torch
from torch.autograd import Variable
# new way with `init` module
w = torch.Tensor(3, 5)
torch.nn.init.normal(w)
# work for Variables also
w2 = Variable(w)
torch.nn.init.normal(w2)
# old styled direct access to tensors data attribute
@ikhlestov
ikhlestov / 03_merged_timeline_only_class_definition.py
Created March 23, 2017 13:32
03_Profiling Tensorflow with timeline(only class for merging timelines)
import json
class TimeLiner:
_timeline_dict = None
def update_timeline(self, chrome_trace):
# convert crome trace to python dict
chrome_trace_dict = json.loads(chrome_trace)
# for first run store full trace
@ikhlestov
ikhlestov / 01_simple_example.py
Last active September 9, 2020 08:05
01_Profiling Tensorflow with timeline
import tensorflow as tf
from tensorflow.python.client import timeline
a = tf.random_normal([2000, 5000])
b = tf.random_normal([5000, 1000])
res = tf.matmul(a, b)
with tf.Session() as sess:
# add additional options to trace the session execution
options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
@ikhlestov
ikhlestov / architecture_pattern.py
Created September 13, 2017 20:10
pytorch: architecture pattern
class ImagesDataset(torch.utils.data.Dataset):
pass
class Net(nn.Module):
pass
model = Net()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
scheduler = lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1)
criterion = torch.nn.MSELoss()
@ikhlestov
ikhlestov / custom_data_loader.py
Created September 13, 2017 19:41
pytorch: custom data loader
import torch
import torchvision as tv
class ImagesDataset(torch.utils.data.Dataset):
def __init__(self, df, transform=None,
loader=tv.datasets.folder.default_loader):
self.df = df
self.transform = transform
self.loader = loader
@ikhlestov
ikhlestov / pytorch_as_numpy.py
Last active February 19, 2020 06:47
pytorch: pytorch as numpy
import torch
import numpy as np
numpy_tensor = np.random.randn(10, 20)
# convert numpy array to pytorch array
pytorch_tensor = torch.Tensor(numpy_tensor)
# or another way
pytorch_tensor = torch.from_numpy(numpy_tensor)
@ikhlestov
ikhlestov / train_model_on_cuda.py
Created September 12, 2017 17:05
pytorch: train model on cuda
import torch
### tensor example
x_cpu = torch.randn(10, 20)
w_cpu = torch.randn(20, 10)
# direct transfer to the GPU
x_gpu = x_cpu.cuda()
w_gpu = w_cpu.cuda()
result_gpu = x_gpu @ w_gpu
# get back from GPU to CPU
@ikhlestov
ikhlestov / bells_and_whistles.py
Created September 13, 2017 19:08
pytorch: learning rate scheduler, train flag, random seed
# scheduler example
from torch.optim import lr_scheduler
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
scheduler = lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1)
for epoch in range(100):
scheduler.step()
train()
validate()
@ikhlestov
ikhlestov / mixed_models_definition.py
Created September 11, 2017 20:50
pytorch: mixed models definition
from torch import nn
class Model(nn.Module):
def __init__(self):
super().__init__()
self.feature_extractor = nn.Sequential(
nn.Conv2d(3, 12, kernel_size=3, padding=1, stride=1),
nn.Conv2d(12, 24, kernel_size=3, padding=1, stride=1),
)
@ikhlestov
ikhlestov / probability_task.py
Created September 20, 2017 00:16
probability_theory
from itertools import combinations, permutations
import numpy as np
def check_line(line):
start = False
for i in line:
if i != 0 and not start:
start = True
counter = 0
elif i == 0 and start: