梯度
矩阵梯度是对每个矩阵中元素求导,初始矩阵没有梯度,只有运算之后才能求导。
张量使用GPU
# # from __future__ import print_function
# import torch
# x = torch.empty(5, 3)
# print(x)
# y = torch.rand(5, 3)
# print(y)
# z = torch.zeros(5, 3, dtype=torch.long)
# print(z)
#
# u = torch.tensor([3.3, 4],dtype=torch.double)
# print(u)
# x = x.new_ones(5, 3)
# print(x)
# x = torch.randn_like(x, dtype=torch.float)
# print(x)
#
# print(x.size())
#
# print(x + y)
# print(torch.add(x, y))
#
#
# result = torch.empty(5, 3)
#
# print(result)
#
# torch.add(x, y, out=result)
# print(result)
# print(x.add(y))
# print(x)
# print('-----------------------------')
# # 取矩阵行
# print(x[:1])
# # 取列
# print(x[:, 2])
# # 一行两列
# x1 = torch.randn(2)
# print(x1)
# # 仅仅只有一个tensor元素
# # print(x1.item())
import torch
# autograd
# 创建一个张量,设置 requires_grad=True 来跟踪与它相关的计算
x = torch.ones(2, 2, requires_grad=True)
# print(x)
y = x + 2
# print(y)
# 每个张量都有一个 .grad_fn 属性保存着创建了张量的 Function 的引用
# 如果用户自己创建张量,则g rad_fn 是 None
# print(x.grad_fn)
# y 作为操作的结果被创建,grad_fn
# print(y.grad_fn)
# 针对 y 做更多的操作
z = y * y
print(z)
out = z.mean()
# print(out)
# print(z, out)
# 如果你想计算导数,你可以调用 Tensor.backward()
out.backward()
# d(out) / dx
print(x.grad)
# ------------------------------
# print('----------------------')
# a = torch.ones(2, 2)
# a = ((a * 3) / (a + 1))
# print(a)
# print(a.requires_grad)
# # 默认False
# # .requires_grad_( ... ) 会改变张量的 requires_grad 标记
#
# a.requires_grad_(True)
# # 设置 requires_grad=True 来跟踪与它相关的计算
# print(a.requires_grad)
#
# print(a * a)
# b = (a * a).sum()
# print(b)
# print(b.grad_fn)
# 如果你想计算导数,你可以调用 Tensor.backward()。如果 Tensor 是标量(
# 即它包含一个元素数据),则不需要指定任何参数backward(),
# 但是如果它有更多元素,则需要指定一个gradient 参数来指定张量的形状。
# 梯度
a = torch.tensor([-444.6791, 762.9810, -1690.0941], requires_grad=True)
b = a**2
out = b.mean().backward()
print(a.grad)
print(-444.6791*2/3)
# import torch
# import torch.nn as nn
# import torch.nn.functional as F
#
#
# class Net(nn.Module):
#
# def __init__(self):
# super(Net, self).__init__()
# self.conv1 = nn.Conv2d(1, 6, 5)
# self.conv2 = nn.Conv2d(6, 16, 5)
#
# self.fc1 = nn.Linear(16*5*5, 120)
# self.fc2 = nn.Linear(120, 84)
# self.fc3 = nn.Linear(84, 10)
#
# def forward(self, x):
# x = F.max_pool2d(F.relu(self.conv1(x)),(2,2))
#
# x = F.max_pool2d(F.relu(self.conv2(x),2))
# x = x.view(-1, self.num_flat_features(x))
# x = F.relu(self.fc1(x))
# x = F.relu(self.fc2(x))
# x = self.fc3(x)
# return x
#
# def num_flat_features(self, x):
# size = x.size()[1]
# num_features = 1
# for s in size:
# num_features *= s
# return num_features
#
#
# net = Net()
# print(net)
# params = list(net.parameters())
# print(len(params))
# print(params[0].size()) # conv1's .weight
# # 3X2 的矩阵中添加 2x3 矩阵
# # input = torch.randn(3, 2, 2, 3)
# input = torch.randn(1, 1, 32, 32)
# # print(input)
# out = net(input)
# print(out)
# import torch
# import torchvision
# import torchvision.transforms as transforms
#
# transform = transforms.Compose([transforms.ToTensor(),
# transforms.Normalize((0.5, 0.5, 0.5),(0.5, 0.5, 0.5))])
#
# print(transform)
#
# trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
#
# trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle = True, num_workers = 2)
#
# testset = torchvision.datasets.CIFAR10(root='./data', train = False,shuffle = False, num_workers = 2)
#
# classes = ('plane', 'car', 'bird', 'cat',
# 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
import torch
import numpy as np
# d = [[1, 2], [2,3]]
# print(d)
# t_d = torch.tensor(d)
# print(t_d)
#
# np_array = np.array(d)
# print(np_array)
# t_np_array = torch.from_numpy(np_array)
# print(t_np_array)
#
# shape = (2, 4,)
# rand_tensor = torch.rand(shape)
#
# print(f"----: \n{rand_tensor}\n")
#
# print(rand_tensor.shape)
# print(rand_tensor.dtype)
# print(rand_tensor.device)
# 使用GPU
tensor = torch.ones(4, 4)
if torch.cuda.is_available():
tensor = tensor.to('cuda')
else:
print('error')
tensor[:, 1] = 0 # 将第1列(从0开始)的数据全部赋值为0
print(tensor)
print(f"Shape of tensor: {tensor.shape}")
print(f"Datatype of tensor: {tensor.dtype}")
print(f"Device tensor is stored on: {tensor.device}")
# 拼接
t1 = torch.cat([tensor, tensor, tensor], dim=0)
print(t1)
# 张量与张量的矩阵乘法:
tensor = tensor @ tensor.T
print(tensor)
# 自动赋值运算
tensor.add_(5)
print(tensor)
#Tensor与Numpy的转化
t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"n: {n}")
# 由Numpy array数组转为张量
n = np.ones(5)
print(n)
t = torch.from_numpy(n)
print(t)