Skip to content

Instantly share code, notes, and snippets.

View ikhlestov's full-sized avatar
🐍
Coding

Illarion ikhlestov

🐍
Coding
View GitHub Profile
@ikhlestov
ikhlestov / from_tensors_to_variables.py
Created September 11, 2017 20:08
pytorch: from tensors to variables
import torch
from torch.autograd import Variable
# define an inputs
x_tensor = torch.randn(10, 20)
y_tensor = torch.randn(10, 5)
x = Variable(x_tensor, requires_grad=False)
y = Variable(y_tensor, requires_grad=False)
# define some weights
w = Variable(torch.randn(20, 5), requires_grad=True)
@ikhlestov
ikhlestov / simple_layer_with_optimizer.py
Last active September 11, 2017 20:14
pytorch: simple layer with optimizer
import torch
from torch.autograd import Variable
import torch.nn.functional as F
x = Variable(torch.randn(10, 20), requires_grad=False)
y = Variable(torch.randn(10, 3), requires_grad=False)
# define some weights
w1 = Variable(torch.randn(20, 5), requires_grad=True)
w2 = Variable(torch.randn(5, 3), requires_grad=True)
@ikhlestov
ikhlestov / tensorflow_while_loop.py
Created September 11, 2017 20:29
pytorch: tensorflow while loop
import tensorflow as tf
first_counter = tf.constant(0)
second_counter = tf.constant(10)
some_value = tf.Variable(15)
# condition should handle all args:
def cond(first_counter, second_counter, *args):
@ikhlestov
ikhlestov / pytorch_while_loop.py
Created September 11, 2017 20:30
pytorch: pytorch while loop
import torch
first_counter = torch.Tensor([0])
second_counter = torch.Tensor([10])
some_value = torch.Tensor(15)
while (first_counter < second_counter)[0]:
first_counter += 2
second_counter += 1
@ikhlestov
ikhlestov / sequenstial_models_definition.py
Created September 11, 2017 20:47
pytorch: sequential models definition
from collections import OrderedDict
import torch.nn as nn
# Example of using Sequential
model = nn.Sequential(
nn.Conv2d(1, 20, 5),
nn.ReLU(),
nn.Conv2d(20, 64, 5),
@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 / self_defined_layers.py
Created September 12, 2017 16:58
pytorch: self defined layers
import torch
class MyFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input):
ctx.save_for_backward(input)
output = torch.sign(input)
return output
@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 / cuda_device_allocation.py
Created September 12, 2017 17:06
pytorch: cuda device allocation
import torch
# check is cuda enabled
torch.cuda.is_available()
# set required device
torch.cuda.set_device(0)
# work with some required cuda device
with torch.cuda.device(1):
@ikhlestov
ikhlestov / cuda_wrapper.py
Created September 12, 2017 17:08
pytorch: CUDA wrapper
class Trainer:
def __init__(self, model, use_cuda=False, gpu_idx=0):
self.use_cuda = use_cuda
self.gpu_idx = gpu_idx
self.model = self.to_gpu(model)
def to_gpu(self, tensor):
if self.use_cuda:
return tensor.cuda(self.gpu_idx)
else: