pytorch tensor查找0_Pytorch简单教程

2019 年,ML 框架之争只剩两个实力玩家:PyTorch 和 TensorFlow。研究者大批涌向 PyTorch,而业界的首选仍然是 TensorFlow。

Pytorch和TensorFlow的区别:

  • TensorFlow是基于静态计算图的,静态计算图是先定义后运行,一次定义多次运行(Tensorflow 2.0也开始使用动态计算图)

  • PyTorch是基于动态图的,是在运行的过程中被定义的,在运行的时候构建,可以多次构建多次运行

PyTorch的特点:

  • 简单。PyTorch 与 numpy 类似,可以很容易地与 Python 生态系统融合。例如,向 PyTorch 模型的任意位置放入一个 pdb 断点,它都可以正常工作。而在 TensorFlow 中,调试模型需要一个激活的会话,最后会变得非常棘手。

  • 优秀的 API。比起 TensorFlow 的 API,多数研究者更喜欢 PyTorch 的 API。部分原因在于 PyTorch 的设计更加合理,还有一部分原因在于。TensorFlow 在将 API 转换多次之后已经自损元气。

  • 性能。尽管 PyTorch 的动态图留给优化的机会非常之少,但有不少非正式报告称 PyTorch 与 TensorFlow 一样快。目前还不清楚这是不是真的,但至少,TensorFlow 在这方面还没有取得决定性优势。

本文推荐一个简单的Pytorch的入门学习教程(来源于网络,如有问题请及时联系):

张量

  Pytorch中的Tensor和ndarray类似,区别在于ndarray不能再GPU上加速,而Tensor可以使用GPU加速。

构建一个未初始化3*3的矩阵

import torch
x = torch.empty(3,3)# tensor([[1.0469e-38, 5.9694e-39, 8.9082e-39],
# [1.0194e-38, 9.1837e-39, 4.6837e-39],
# [9.9184e-39, 9.0000e-39, 1.0561e-38]])

构建一个3*3的随机矩阵

x = torch.rand(3, 3)# tensor([[0.4289, 0.6872, 0.2781],
# [0.2129, 0.7520, 0.3994],
# [0.0995, 0.9080, 0.7868]])

dtype long的全零矩阵:

x = torch.zeros(5, 3, dtype=torch.long)# tensor([[0, 0, 0],
# [0, 0, 0],
# [0, 0, 0]])

把数据[5.5, 3]变成Tensor

x = torch.tensor([5.5, 3])# tensor([5.5000, 3.0000])

得到数组的shape

print(x.size())# torch.Size([2])

torch.Size 实际上是一个元组,因此它支持所有元组操作。

Operation操作

加法

import torch# ------------- 方法一 -------------#
x = torch.rand(2, 2) # 构建一个(2,2)的随机数组
y = torch.rand(2, 2) # 构建一个(2,2)的随机数组print(x + y)# ------------- 方法二 -------------#print(torch.add(x, y))# ------------- 方法三 -------------#
result = torch.empty(2, 2)
torch.add(x, y, out=result)print(result)# ------------- 方法四 -------------#
# 把x加到y上y.add_(x)print(y)# 所有的结果都等于
# tensor([[0.5464, 0.5692],
# [0.7211, 1.2168]])

Pytorch的索引和python一样,

调整shape

torch.view()  调整数组shape

torch.size()  查看数据shape

import torch
x = torch.randn(4, 4)
y = x.view(16)print(y.size()) # torch.Size([16])
z = x.view(-1, 8)print(z.size()) # torch.Size([2, 8])

如果我们的张量只有一个数值,可以使用.item()获取

import torch
x = torch.randn(1)print(x) # tensor([-0.8504])print(x.item()) # -0.8503872156143188

Numpy数组和Torch Tensor转换

将Torch张量转换为NumPy数组

ndarray.numpy():Torch Tensor-->ndarray

import torch
a = torch.ones(5)print(a) # tensor([1., 1., 1., 1., 1.])# torch tensor-->ndarray
b = a.numpy()print(b, type(b)) # [1. 1. 1. 1. 1.]

将NumPy数组转换为Torch张量

torch.from_numpy(ndarray):ndarray--Torch Tensor

import torchimport numpy as np
a = np.ones(5) # [1. 1. 1. 1. 1.]
b = torch.from_numpy(a)print(b) # tensor([1., 1., 1., 1., 1.], dtype=torch.float64)

Autograd自动微分

自动微分Autograd用于自动计算复杂函数的梯度,用于神经网络的优化,

如果设置torch.tensor_1(requires_grad=True),那么会追踪所有对该张量tensor_1的所有操作。

import torch# 创建一个张量并设置 requires_grad=True 用来追踪他的计算历史
x = torch.ones(2, 2, requires_grad=True)print(x)# tensor([[1., 1.],
# [1., 1.]], requires_grad=True)

当Tensor完成一个计算过程,每个张量都会自动生成一个.grad_fn属性

# 对张量进行计算操作,grad_fn已经被自动生成了。
y = x + 2print(y)# tensor([[3., 3.],
# [3., 3.]], grad_fn=)print(y.grad_fn)# # 对y进行一个乘法操作
z = y * y * 3
out = z.mean()print(z)# tensor([[27., 27.],
# [27., 27.]], grad_fn=)print(out)# tensor(27., grad_fn=)

.requires_grad_(...) 可以改变张量的requires_grad属性。 

import torch
a = torch.randn(2, 2)
a = ((a * 3) / (a - 1))print(a.requires_grad) # 默认是requires_grad = Falsea.requires_grad_(True)print(a.requires_grad) # True
b = (a * a).sum()print(b.grad_fn) #

梯度

回顾到上面

import torch# 创建一个张量并设置 requires_grad=True 用来追踪他的计算历史
x = torch.ones(2, 2, requires_grad=True)print(x)# tensor([[1., 1.],
# [1., 1.]], requires_grad=True)# 对张量进行计算操作,grad_fn已经被自动生成了。
y = x + 2print(y)# tensor([[3., 3.],
# [3., 3.]], grad_fn=)print(y.grad_fn)# # 对y进行一个乘法操作
z = y * y * 3
out = z.mean()print(z)# tensor([[27., 27.],
# [27., 27.]], grad_fn=)print(out)# tensor(27., grad_fn=)
print(out)  # tensor(27., grad_fn=)print("*"*50)
out.backward()# 打印梯度print(x.grad)# tensor([[4.5000, 4.5000],
# [4.5000, 4.5000]])
import torch
x = torch.randn(3, requires_grad=True)
y = x * 2while y.data.norm() < 1000:
y = y * 2print(y) # tensor([-920.6895, -115.7301, -867.6995], grad_fn=)
gradients = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)# 把gradients代入y的反向传播中y.backward(gradients)# 计算梯度print(x.grad) # tensor([ 51.2000, 512.0000, 0.0512])

为了防止跟踪历史记录,可以将代码块包装在with torch.no_grad():中。在评估模型时特别有用,因为模型的可训练参数的属性可能具有requires_grad = True,但是我们不需要梯度计算。

print(x.requires_grad)      # Trueprint((x ** 2).requires_grad)   # True
with torch.no_grad():print((x ** 2).requires_grad) # False

神经网络

神经网络是基于自动梯度 (autograd)来定义一些模型。一个 nn.Module 包括层和一个 forward(input) 它会返回输出(output)。

一个典型的神经网络训练过程包括以下几点:

  1. 定义一个包含可训练参数的神经网络

  2. 迭代整个输入

  3. 通过神经网络处理输入

  4. 计算损失(loss)

  5. 反向传播梯度到神经网络的参数

  6. 更新网络的参数,典型的用一个简单的更新方法:weight = weight - learning_rate *gradient

我们先来定义一个网络,处理输入,调用backword

import torchimport torch.nn as nnimport torch.nn.functional as Fclass Net(nn.Module):def __init__(self):
super(Net, self).__init__()# 1 input image channel, 6 output channels, 3x3 square convolution# kernel
self.conv1 = nn.Conv2d(1, 6, 3)
self.conv2 = nn.Conv2d(6, 16, 3)# an affine operation: y = Wx + b
self.fc1 = nn.Linear(16 * 6 * 6, 120) # 6*6 from image dimension
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)def forward(self, x):# (2, 2)大小的最大池化层
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 xdef num_flat_features(self, x):print(x.size()) # torch.Size([1, 16, 6, 6])
size = x.size()[1:] # 除batch维度外的所有维度print(size) # torch.Size([16, 6, 6])
num_features = 1for s in size:
num_features *= sreturn num_features
net = Net()print(net) # 打印模型结构
# Net(
# (conv1): Conv2d(1, 6, kernel_size=(3, 3), stride=(1, 1))
# (conv2): Conv2d(6, 16, kernel_size=(3, 3), stride=(1, 1))
# (fc1): Linear(in_features=576, out_features=120, bias=True)
# (fc2): Linear(in_features=120, out_features=84, bias=True)
# (fc3): Linear(in_features=84, out_features=10, bias=True))

torch.nn只支持批输入,格式:sSamples * nChannels * Height * Width(样本数*通道数*高*宽)

如果我们只有一个样本,只需使用 ``input.unsqueeze(0)`` 来添加其它的维数

一个模型可训练的参数可以通过调用 net.parameters() 返回:

params = list(net.parameters())print(len(params))       # 10print(params[0].size())  # 第一个卷积层的权重 torch.Size([6, 1, 3, 3])

让我们尝试随机生成一个 32x32 的输入

input = torch.randn(1, 1, 32, 32)
out = net(input)print(out)# tensor([[ 0.1464, 0.0453, 0.0269, 0.0078, 0.1960, -0.1795, 0.1265,
# -0.0742, -0.0649, 0.0592]], grad_fn=)

把所有参数梯度缓存器置零,用随机的梯度来反向传播

# 把所有参数梯度缓存器置零net.zero_grad()# 用随机的梯度来反向传播
out.backward(torch.randn(1, 10))

损失函数

计算均方误差 $loss=nn.MSELoss(模型预测值-目标)

output = net(input)     # torch.Size([1, 10])
target = torch.randn(10) # 随便取一个target
target = target.view(1, -1) # 让target和output的shape一样
criterion = nn.MSELoss()
loss = criterion(output, target)print(loss) # tensor(0.8695, grad_fn=)

现在,如果你跟随损失到反向传播路径,可以使用它的 .grad_fn 属性,你将会看到一个这样的计算图:

input -> conv2d -> relu -> maxpool2d -> conv2d -> relu -> maxpool2d -> view -> linear -> relu -> linear -> relu -> linear -> MSELoss -> loss

所以,当我们调用 loss.backward(),整个图都会微分,而且所有的在图中的requires_grad=True 的张量将会让他们的 grad 张量累计梯度。

为了演示,我们将跟随以下步骤来反向传播。

print(loss.grad_fn)  # MSELoss
# print(loss.grad_fn.next_functions[0][0]) # Linear
# print(loss.grad_fn.next_functions[0][0].next_functions[0][0]) # ReLU
#

反向传播

为了实现反向传播损失,我们所有需要做的事情仅仅是使用 loss.backward()。你需要清空现存的梯度,要不然将会和现存的梯度累计到一起。

现在我们调用 loss.backward() ,然后看一下 con1 的偏置项在反向传播之前和之后的变化。

net.zero_grad()     # 将所有参数的梯度缓冲区归零print(‘conv1.bias.grad 反向传播之前‘)print(net.conv1.bias.grad)# tensor([0., 0., 0., 0., 0., 0.])
loss.backward()print(‘conv1.bias.grad 反向传播之后‘)print(net.conv1.bias.grad)# tensor([-0.0118, 0.0125, -0.0085, -0.0225, 0.0125, 0.0235])

随机梯度下降,更新神经网络参数:

基于python实现

weight = weight - learning_rate * gradient
learning_rate = 0.01for f in net.parameters():
f.data.sub_(f.grad.data * learning_rate)

使用torch.optim实现,torch.optim中包含SGD, Nesterov-SGD, Adam, RMSProp, 等优化器

import torch.optim as optim# create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01)# in your training loop:
optimizer.zero_grad() # zero the gradient buffers
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step() # Does the update

图像分类器

torch有一个叫做totchvision 的包,支持加载类似Imagenet,CIFAR10,MNIST 等公共数据集的数据加载模块 torchvision.datasets

支持加载图像数据数据转换模块 torch.utils.data.DataLoader。

本节我们使用CIFAR10数据集,它包含十个类别:‘airplane’, ‘automobile’, ‘bird’, ‘cat’, ‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’。CIFAR-10 中的图像尺寸为33232,也就是RGB的3层颜色通道,每层通道内的尺寸为32*32。

0b8e34f1345e219156436441886dfb25.png

训练一个图像分类器

我们将按次序的做如下几步:

  1. 使用torchvision加载并且归一化CIFAR10的训练和测试数据集

  2. 定义一个卷积神经网络

  3. 定义一个损失函数

  4. 在训练样本数据上训练网络

  5. 在测试样本数据上测试网络

torchvision 数据集的输出是范围在[0,1]之间的 PILImage,我们将他们转换成归一化范围为[-1,1]之间的张量 Tensors。

import torchimport torchvisionimport torchvision.transforms as transforms
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])# 下载训练数据集
trainset = torchvision.datasets.CIFAR10(root=‘./data‘, train=True,
download=True, transform=transform)# 下载测试数据集
testset = torchvision.datasets.CIFAR10(root=‘./data‘, train=False,
download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
shuffle=True, num_workers=2)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
shuffle=False, num_workers=2)
classes = (‘plane‘, ‘car‘, ‘bird‘, ‘cat‘, ‘deer‘, ‘dog‘, ‘frog‘, ‘horse‘, ‘ship‘, ‘truck‘)

让我们来展示其中的一些训练图片

import matplotlib.pyplot as pltimport numpy as np# 展示图片def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()# 获取一些随机的训练图片
dataiter = iter(trainloader)
images, labels = dataiter.next()
imshow(torchvision.utils.make_grid(images)) # show images
# 打印 labelsprint(‘ ‘.join(‘%5s‘ % classes[labels[j]] for j in range(4)))# cat plane ship frog

定义一个卷积神经网络 在这之前先 从神经网络章节 复制神经网络,并修改它为3通道的图片(在此之前它被定义为1通道)

import torch.nn as nnimport torch.nn.functional as Fclass Net(nn.Module):def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
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 = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)return x
net = Net()

定义一个损失函数和优化器 让我们使用交叉熵Cross-Entropy 作损失函数,优化器使用SGD

import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

我们只需要在数据迭代器上将数据循环传给网络和优化器 就可以。

for epoch in range(2):  # 多次循环数据集
running_loss = 0.0for i, data in enumerate(trainloader, 0):# 获取输入
inputs, labels = data# 把参数梯度归零 optimizer.zero_grad()# 前向传播(forward) + 反向传播(backward) + 优化器(optimize)
outputs = net(inputs) # 前向传播
loss = criterion(outputs, labels)
loss.backward() # 反向传播
optimizer.step() # 优化器
running_loss += loss.item()if i % 2000 == 1999: # 每2000个小batch打印一次print(‘[%d, %5d] loss: %.3f‘ %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0print(‘Finished Training‘)# [1, 2000] loss: 2.187
# [1, 4000] loss: 1.852
# [1, 6000] loss: 1.672
# [1, 8000] loss: 1.566
# [1, 10000] loss: 1.490
# [1, 12000] loss: 1.461
# [2, 2000] loss: 1.389
# [2, 4000] loss: 1.364
# [2, 6000] loss: 1.343
# [2, 8000] loss: 1.318
# [2, 10000] loss: 1.282
# [2, 12000] loss: 1.286
# Finished Training

在测试集上测试网络 我们已经通过训练数据集对网络进行了2次训练,但是我们需要检查网络是否已经学到了东西。

我们将用神经网络的输出作为预测的类标来检查网络的预测性能,用样本的真实类标来校对。如果预测是正确的,我们将样本添加到正确预测的列表里。

好的,第一步,让我们从测试集中显示一张图像来熟悉它。

GroundTruth: cat ship ship plane

c1d27103d657e9b513400947c6d24159.png

测试

输出是预测与十个类的近似程度,与某一个类的近似程度越高,网络就越认为图像是属于这一类别。所以让我们打印其中最相似类别类标:

outputs = net(images)
_, predicted = torch.max(outputs, 1)print(‘Predicted: ‘, ‘ ‘.join(‘%5s‘ % classes[predicted[j]] for j in range(4)))# Predicted: cat ship car ship
# GroundTruth: cat ship ship plane

预测对了两个,让我们看看网络在整个数据集上的表现。

correct = 0
total = 0
with torch.no_grad():for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()print(‘Accuracy of the network on the 10000 test images: %d %%‘ % (100 * correct / total))# Accuracy of the network on the 10000 test images: 54 %

正确率有54%,看来网络学到了东西。随机预测出为10类中的哪一类:

class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
with torch.no_grad():for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs, 1)
c = (predicted == labels).squeeze()for i in range(4):
label = labels[i]
class_correct[label] += c[i].item()
class_total[label] += 1for i in range(10):print(‘Accuracy of %5s : %2d %%‘ % (
classes[i], 100 * class_correct[i] / class_total[i]))# Accuracy of plane : 57 %
# Accuracy of car : 73 %
# Accuracy of bird : 49 %
# Accuracy of cat : 54 %
# Accuracy of deer : 18 %
# Accuracy of dog : 20 %
# Accuracy of frog : 58 %
# Accuracy of horse : 74 %
# Accuracy of ship : 70 %
# Accuracy of truck : 66 %

在GPU上跑这些神经网络?

在GPU上训练,我么要将神经网络转到GPU上。前提条件是CUDA可以用,让我们首先定义下我们的设备为第一个可见的cuda设备。

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")# Assume that we are on a CUDA machine, then this should print a CUDA device:print(device)# cuda:0

接着这些方法会递归地遍历所有模块,并将它们的参数和缓冲器转换为CUDA张量。

net.to(device)

记住你也必须在每一个步骤向GPU发送输入和目标:

inputs, labels = inputs.to(device), labels.to(device)

CUDA张量

使用该.to方法可以将张量移动到任何设备上。只有在有CUDA的情况下我们才能运行这个函数

# 我们将使用“torch.device”对象来移动GPU中的张量if torch.cuda.is_available():
device = torch.device("cuda") # CUDA设备对象
y = torch.ones_like(x, device=device) # 直接在GPU上创建张量
x = x.to(device) # 或者只使用 ``.to("cuda")
z = x + yprint(z)print(z.to("cpu", torch.double)) # " ".to()还可以更改数据类型# tensor([0.7032], device=‘cuda:0‘)
# tensor([0.7032], dtype=torch.float64)

数据并行处理

本章节教大家如何使用DataParallel来使用多GPU。

我们把模型放入GPU中

 device = torch.device("cuda:0")
model.to(device)

将所有张量复制到GPU

mytensor = my_tensor.to(device)

在多 GPU 中执行前向、方向操作是非常自然的。尽管如此,PyTorch 默认只会使用一个 GPU。因此我们要使用DataParallel让模型在多个GPU上并行运行。

输入和参数

import torchimport torch.nn as nnfrom torch.utils.data import Dataset, DataLoader
input_size = 5
output_size = 2
batch_size = 30
data_size = 100# 设备
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

制造一个随机的数据集

class RandomDataset(Dataset):def __init__(self, size, length):
self.len = length
self.data = torch.randn(length, size)def __getitem__(self, index):return self.data[index]def __len__(self):return self.len
rand_loader = DataLoader(dataset=RandomDataset(input_size, data_size),
batch_size=batch_size, shuffle=True)

搭建一个简单的模型,我们的模型仅获取输入,执行线性运算并给出输出,

class Model(nn.Module):def __init__(self, input_size, output_size):
super(Model, self).__init__()
self.fc = nn.Linear(input_size, output_size)def forward(self, input):
output = self.fc(input)print("\tIn Model: input size", input.size(), "output size", output.size())return output

创建模型和数据并行

我们先要检查模型是否有多个GPU,如果有我们再使用nn.DataParallel,然后我们可以把模型放在GPU上model.to(device)

model = Model(input_size, output_size)if torch.cuda.device_count() > 1:print("我们有", torch.cuda.device_count(), "个GPUs!")# dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
model = nn.DataParallel(model)
model.to(device)# 我们有2个GPU

运行模型,现在我们可以看到输入和输出张量的大小了

for data in rand_loader:
input = data.to(device)
output = model(input)print("Outside: input size", input.size(), "output_size", output.size())

输出

        In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2])
In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])

如果我们有2个GPU我们可以看到以下结果

# on 2 GPUs
Let‘s use 2 GPUs!
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2])
In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])

数据并行自动拆分了你的数据并且将任务单发送到多个 GPU 上。当每一个模型都完成自己的任务之后,DataParallel 收集并且合并这些结果,然后再返回给你。

其他学习资料:

http://pytorch123.com/

PyTroc官方文档:https://pytorch.org/

PyTroch中文文档:https://pytorch-cn.readthedocs.io/zh/latest/

PyTroch中文网:https://www.pytorchtutorial.com

简单易上手的PyTorch中文文档:https://github.com/fendouai/pytorch1.0-cn

未来地图:物联--数联--智联

【物联】

  • 物联数据建模—时空序列分析

  •  第一章:感知数据治理

  •  第二章:时空序列建模

  •  第三章 单元时间序列

  • 第四章:多维时间序列

  • 物联网数据分析体系

  • 物联数据建模探讨

  • GeoMAN:基于multi-level attention机制的传感器时间序列预测模型

【数联】

  • 什么是数据科学?

  • 大数据与数据科学课程体系

  • 这样搞定数据科学?

  • 自学福利!数据科学课程体系框架

  • 如何成为一名数据科学家?

【智联】

  • 三个角度理解知识图谱

  • 图数据挖掘VS知识图谱挖掘

  • 下个拐点:图神经网络

  • 详细的知识图谱构建流程

  • 基于知识图谱推理的关系推演

  • 基于知识图谱的智能问答

18c26cbe0e4539bf151ae0ab7802375b.png

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/567514.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

白盒测试-条件覆盖-短路陷阱

条件覆盖 ​要求设计足够多的测试用例&#xff0c;使得每一个判定的​每一个条件的每一个取值&#xff08;真或假&#xff09;至少各执行一次。 对于示例来说​&#xff0c;可以设计&#xff1a; {x4,y4,z11} 条件取值&#xff1a;TFTF {x2,y6,z9} 条件取值&#xff1a;F…

抖音2020研究报告_抖音音乐发布2020抖音音乐生态数据报告

近几年&#xff0c;随着原创音乐产业规模逐年递增及数字音乐4.0时代觉醒&#xff0c;拓宽了原创音乐创作与宣发渠道&#xff0c;真正实现了大众娱乐生活从“听音乐”到“看音乐”到“玩音乐”的多重共振。11月5日&#xff0c;抖音音乐正式发布《2020抖音音乐生态数据报告》。报…

mysql主备切换 自动_核电生产管理信息系统EAM完成首次备用环境切换演练

安全是核电事业稳步发展的基石&#xff0c;中国核电始终将安全放在第一位&#xff0c;在系统设计及日常管理中&#xff0c;注重安全备用及安全冗余的考虑。核电生产管理信息系统作为核电现场业务的主要承载工具&#xff0c;在核电日常生产运行中发挥着越来越重要的作用。近日&a…

外星人台式电脑_戴尔 XPS 和外星人大更新,一边是生产力,一边是游戏

十代酷睿的到来&#xff0c;也催促着终端厂商更迭着旗下的产品线&#xff0c;而今年特殊的情况更让不少居家办公的人意识到生产工具和生产力的可贵。在学生们即将返校&#xff0c;白领们陆续回到办公室的当口&#xff0c;戴尔对旗下产品进行了大更新&#xff0c;全新的 XPS15 和…

string 中的offset_【Java基础】String常量的长度有限制吗?

备注&#xff1a;JDK版本&#xff1a;1.8.0最近同事问了我一个问题&#xff0c;String常量有长度限制吗&#xff1f;为什么会问起这个问题呢&#xff0c;因为同事在开发中确实遇到了IDEA编译器编译过程中提示字符串常量过长的异常。异常的出现也就证明了String常量是有长度限制…

param[:]=param-lr*param.grad/batch_size的理解

lr*param.grad/batch_size 结果最终为一个标量&#xff08;具体数值&#xff09; param是一个列表 param[:]param-lr*param.grad/batch_size会把列表中的所有元素分别运算&#xff1a; param-lr*param.grad/batch_size import numpy as np wnp.random.normal(scale0.01,siz…

anaconda3安装_Anaconda3软件安装教程

01获取方式Anaconda3 下载地址链接&#xff1a;https://pan.baidu.com/s/1lJJavzMGxjFvxYNyJ_nPAQ提取码&#xff1a;dc5c安装过程中有问题可咨询微信&#xff1a;d73707919702插件详细安装步骤1.找到下载好压缩文件包单击鼠解压到Anaconda32.待到解压完成后双击打开解压后的文…

pip命令提示unknow or unsupported command install解决方法

python的pip一直可以用 再次使用时突然发现有错误&#xff1a; 经百度一查&#xff0c;是因为安装了loadrunner&#xff0c;导致了系统无法识别到底应该用哪个pip。 解决方案&#xff1a; 第一种&#xff1a;删除strawberry&#xff0c;但是这样明显是会影响到其他软件的使用…

360浏览器登录_浏览器发展历史介绍及当今主流浏览器的详细对比

作为访问internet的工具&#xff0c;浏览器已经成为我们日常生活中必不可少的上网工具了&#xff0c;它能让你加入全球的网络&#xff0c;通过一个窗口就能够连接世界。当你用浏览器时有没有想过浏览器的发展历史&#xff1f;面对市场上繁多的浏览器你又为什么偏爱你现在用的&a…

backward理解

backward&#xff1a;自动求梯度。计算小批量随机梯度。 当模型和损失函数形式较为简单时&#xff0c;上面的误差最小化问题的解可以直接用公式表达出来。这类解 叫作解析解&#xff08;analytical solution&#xff09;。本节使用的线性回归和平方误差刚好属于这个范畴。然而…

excel在线_功能强大的纯前端 Excel 在线表格: Luckysheet

【导语】&#xff1a;Luckysheet是一款类似excel的在线表格&#xff0c;纯前端&#xff0c;功能强大、配置简单、完全开源&#xff0c;几行代码就能展现出一个功能完备的在线表格。简介Luckysheet是一款类似excel的纯前端在线表格&#xff0c;只需要引入js和css文件即可使用。L…

STL-queue.back()队尾误区

queue.back()指向最新插入queue中的值&#xff0c;而非队尾元素&#xff0c; 如&#xff1a;queue.pop()多次&#xff0c;并不会影响queue.back()的值。 STL 英文back()解释&#xff1a; reference& back(); const_reference& back() const; Access last element …

u8 和 char如何转化_EXCEL小知识——如何快速实现文本与数值的互相转化

我是前言嗨&#xff0c;大家好&#xff0c;消失了一个多月&#xff0c;我胡汉三&#xff0c;又回来啦~今天给大家带来的&#xff0c;是如何实现文本与数值之间的 “ 快速 ” 转换&#xff01;众所周知&#xff0c;在一些制造类公司&#xff0c;公司的运营离不开一些系统软件的辅…

navicat er图没有连线_迁徙图?流向图?城市关系强度图?

文章首发于公众号「码上GIS」&#xff0c;欢迎关注。文中流向图和城市关系强度图的 ArcMap 10.5 Mxd 工程和数据可在公众号后台回复「190708」和「190709」获取不记得是从哪年开始&#xff0c;每年春运期间&#xff0c;百度都会发布个春运大数据&#xff0c;其中最让人印象深刻…

linux删除文件_Linux中删除特殊名称文件的多种方式

今日分享&#xff1a;我们在肉体的疾病方面花了不少钱&#xff0c;精神的病害方面却没有花什么&#xff0c;现在已经到了时候&#xff0c;我们应该有不平凡的学校。--《瓦尔登湖》前言我们都知道&#xff0c;在linux删除一个文件可以使用rm命令&#xff0c;但是有一些特殊名称的…

Python中的lambda和apply结合使用

1、 lambda lambda原型为&#xff1a;lambda 参数:操作(参数) lambda函数也叫匿名函数&#xff0c;即没有具体名称的函数&#xff0c;它允许快速定义单行函数&#xff0c;可以用在任何需要函数的地方。这区别于def定义的函数。 lambda与def的区别&#xff1a; 1&#xff09;…

软件开发报价模板_定制开发小程序和行业通用(模板)小程序的利弊分析

最近很多掌客多客户来咨询&#xff0c;纠结到底是定制开发小程序还是买个模板通用小程序好&#xff0c;其实在回答这个问题之前&#xff0c;我们先要搞明白什么是定制开发小程序&#xff0c;什么是模板通用小程序&#xff0c;最后再问问自己的搞小程序的目的是什么&#xff1f;…

有十五个数按由大到小顺序存放在一个数组中_「图形化编程」前导知识-数组(一)...

今天我们来学习一个新的概念-数组。这节课将通过一个小程序讲解数组的基本概念-数组的长度和下标定义数组指的是有序元素的集合&#xff0c;数组中的每个元素具有相同的类型&#xff0c;按照顺序排列的形式组织在一起。我们可以把数组想象为一个抽屉柜&#xff0c;每个抽屉只能…

octave错误-error: ‘squareThisNumber‘ undefined near line 1 column 1

.m文件名称也应为大写&#xff1a;squareThisNumber.m 问题2&#xff1a; parse error near line 1 of file C:\Users\asus\squareThisNumber.m syntax error >>> {\rtf1\ansi\ansicpg936\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset134 \cb\ce\cc\e5;}} 解决方案…

python矩阵中找满足条件的元素_Python 找到列表中满足某些条件的元素方法

Python 找到列表中满足某些条件的元素方法 更新时间&#xff1a;2018年06月26日 11:20:17 作者&#xff1a;CS_network 今天小编就为大家分享一篇Python 找到列表中满足某些条件的元素方法&#xff0c;具有很好的参考价值&#xff0c;希望对大家有所帮助。一起跟随小编过来看看…