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
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
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
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
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 | |
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 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 | |
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 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 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 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 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 |