Skip to content

Instantly share code, notes, and snippets.

Code to test:
x = torch.randint(2, (1000, 1000))
torch_times = {}
torch_tmp = []
for dim in range(2):
for _ in range(10):
t0 = time.perf_counter()
tu, ti = torch.unique(x, return_inverse=True, dim=dim)
import time
import torch
import torch.nn as nn
device = 'cuda:0'
batch_size = 10
channels = 64
h, w = 128, 128
import torch
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
output = F.log_softmax(torch.randn(1, 3, 24, 24), 1)
target = torch.zeros(1, 24, 24, dtype=torch.long)
target[0, 4:12, 4:12] = 1
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
use_adam = False
class MyModel(nn.Module):
def __init__(self):
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torchvision.utils import make_grid
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
class MyDataset(Dataset):
def __init__(self, data, target):
self.data = data
self.target = target
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import torch.nn.functional as F
import matplotlib.pyplot as plt
import numpy as np
#Load packages
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision.datasets as dsets
from torch.autograd import Variable
import torch.nn.functional as F
torch.manual_seed(2809)
import torch
import torch.nn as nn
from torch.autograd import Variable
# batch_size = 2
x1 = Variable(torch.ones(2, 1))
w1 = Variable(torch.ones(1, 1), requires_grad=True)
y1 = Variable(torch.ones(2, 1) * 2)
import torch
import numpy as np
# numpy
a = np.random.rand(10, 20)
tmp0 = np.split(a, indices_or_sections=5, axis=0) # split into 5 sections
for t in tmp0:
print(t.shape)
np.split(a, indices_or_sections=7, axis=0) # error, since no equal division