This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
NewerOlder