Skip to content

Instantly share code, notes, and snippets.

View ikhlestov's full-sized avatar
🐍
Coding

Illarion ikhlestov

🐍
Coding
View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / 02_example_with_placeholders_and_for_loop.py
Created March 23, 2017 12:48
02_Profiling Tensorflow with timeline
import os
import tempfile
import tensorflow as tf
from tensorflow.contrib.layers import fully_connected as fc
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.python.client import timeline
batch_size = 100