What can a deep learning library do?
import torch
import time
print(torch.__version__)
print(torch.cuda.is_available())
a = torch.randn(10000, 1000) # 随机生成服从正态分布10000行x1000列的张量
b = torch.randn(1000, 2000)
t0 = time.time()
c = torch.matmul(a, b)
t1 = time.time()
print(a.device, t1 - t0, c.norm(2)) # cpu 0.41573262214660645 tensor(140925.6406)
# device = torch.device('cuda')
# a = a.to(device)
# b = b.to(device)
a = a.cuda()
b = b.cuda()
t0 = time.time()
c = torch.matmul(a, b)
t2 = time.time()
print(a.device, t2 - t0, c.norm(2))
t0 = time.time()
c = torch.matmul(a, b)
t2 = time.time()
print(a.device, t2 - t0, c.norm(2)) # cuda:0 0.006535530090332031 tensor(141469.8906, device='cuda:0')
1. What is the difference between torch.rand() and torch.randn()?
torch.randn(*sizes, out=None)
randn is to randomly generate data that obeys the normal distribution, and the return value is a tensor.
parameter:
sizes (int...) - 整数序列,定义了输出张量的形状
out (Tensor, optinal) - 结果张量
torch.rand(*sizes, out=None)
rand is randomly generated data that obeys a uniform distribution, and the return value is a tensor.
parameter:
sizes (int...) - 整数序列,定义了输出张量的形状
out (Tensor, optinal) - 结果张量
2. pytorch norm function-torch.norm
torch.norm(input, p=‘fro’, dim=None, keepdim=False, out=None, dtype=None)
Returns the matrix norm or vector norm of the given tensor
3. Is there a difference between .cuda() and .to(device)
in pytorch . Cuda() is equivalent to .to(device)
running screenshot:
Find the partial derivatives of y to abc:
import torch
from torch import autograd
x = torch.tensor(1.)
a = torch.tensor(1., requires_grad=True) # requires_grad=True 是告诉pytorch我们是对a b c求导
b = torch.tensor(2., requires_grad=True)
c = torch.tensor(3., requires_grad=True)
y = a**2 * x + b * x + c
print('\nbefore: ', a.grad, b.grad, c.grad)
grads = autograd.grad(y, [a, b, c])
print('\nafter: ', grads[0], grads[1], grads[2])
Example usage of torch.autograd.grad() function in Pytorch
autograd.grad(outputs, inputs, grad_outputs=None, retain_graph=None,
create_graph=False, only_inputs=True, allow_unused=False)
outputs: the dependent variable for derivation (function that requires derivation)
inputs: the independent variable to be derived
grad_outputs: If outputs is a scalar, then grad_outputs=None, that is, you don’t need to write; if outputs is a vector, then this parameter must be written, if you don’t write it, an error will be reported