标签:square nts stat form nes simple res nump .sh
import torch
import numpy as np
from torch.autograd import Variable
x = torch.FloatTensor(2,3)
print(x)
x.zero_()
print(x)
np.random.seed(123)
np_array = np.random.random((2,3))
print(torch.FloatTensor(np_array))
print(torch.from_numpy(np_array))
torch.manual_seed(123)
print(torch.randn(2,3))
print(torch.eye(3))
print(torch.ones(2,3))
print(torch.zeros(2,3))
print(torch.arange(0,3))
x = torch.FloatTensor(3,4)
print(x.size())
print(x.type())
x = torch.rand(3,2)
print(x)
y = x.cuda()
print(y)
z = y.cpu()
print(z)
print(z.numpy())
x = torch.rand(3,5).cuda()
y = torch.rand(5,4).cuda()
print(torch.mm(x,y))
print(x.new(1,2).zero_())
from timeit import timeit
x = torch.rand(1000,64)
y = torch.rand(64,32)
number = 10000
def square():
z = torch.mm(x,y)
print(‘CPU: {}ms‘.format(timeit(square,number = number)1000))
x,y = x.cuda(),y.cuda()
print(‘GPU: {}ms‘.format(timeit(square,number = number)1000))
x = torch.arange(0,5)
print(torch.sum(x))
print(torch.sum(torch.exp(x)))
print(torch.mean(x))
x = torch.rand(3,2)
print(x)
print(x[1,:])
x = Variable(torch.arange(0,4),requires_grad = True)
y = torch.sum(x**2)
y.backward()
print(x)
print(y)
print(x.grad)
x = torch.rand(3,5)
y = torch.rand(5,4)
xv = Variable(x)
yv = Variable(y)
print(torch.mm(x,y))
print(torch.mm(xv,yv))
x = Variable(torch.arange(0,4),requires_grad = True)
torch.sum(x ** 2).backward()
print(x.grad)
torch.sum(x ** 2).backward()
print(x.grad)
x.grad.data.zero_()
torch.sum(x ** 2).backward()
print(x.grad)
net = torch.nn.Sequential(
torch.nn.Linear(28*28,256),
torch.nn.Sigmoid(),
torch.nn.Linear(256,10)
)
print(net.state_dict().keys())
print(net.state_dict())
torch.save(net.state_dict(),‘test.t7‘)
net.load_state_dict(torch.load(‘test.t7‘))
class MyNetwork(torch.nn.Module):
def init(self):
super().init()
self.layer1 = torch.nn.Linear(28*28,256),
self.layer2 = torch.nn.Sigmoid(),
self.layer3 = torch.nn.Linear(256,10)
def forward(self,input_val):
h = input_val
h = self.layer1(h)
h = self.layer2(h)
h = self.layer3(h)
return h
net = MyNetwork()
CMU Deep Learning 2018 by Bhiksha Raj 学习记录(1)
标签:square nts stat form nes simple res nump .sh
原文地址:https://www.cnblogs.com/ecoflex/p/8870121.html