This file contains hidden or 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 | |
# 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) |
This file contains hidden or 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 | |
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) |
This file contains hidden or 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 | |
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): |
This file contains hidden or 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 | |
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 |
This file contains hidden or 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 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), |
This file contains hidden or 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 hidden or 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 | |
class MyFunction(torch.autograd.Function): | |
@staticmethod | |
def forward(ctx, input): | |
ctx.save_for_backward(input) | |
output = torch.sign(input) | |
return output |
This file contains hidden or 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 hidden or 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 | |
# 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): |
This file contains hidden or 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 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: |