1 regedit;
2 计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power;
3 CsEnabled,1改为0;
4 重启。
1 regedit;
2 计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power;
3 CsEnabled,1改为0;
4 重启。
深度学习可以使用商业收费的Matlab2018b中自带的Deep Learning Toolbox,也可以使用第三方MatConvNet(需要mex编译)。
同时,也可以使用开源免费框架,比如TensorFlow和PyTorch。后续发展了基于TensorFlow的Auto-Keras(自动深度学习)和基于Pytorch的fastai(编辑的代码量减少)。
在Win10/Anaconda(Python 3.6.4)环境下,本文选择了Pytorch。
安装
确定CUDA版本号或者鼠标右键NVIDIA控制面板--帮助--系统信息--组件--NVCUDA.DLL 9.1.84 driver(显卡GTX 1050 Ti),选择对应的Pytorch安装,
conda install pytorch torchvision -c pytorch #stable 1.0
接着安装fastai(Python 3.6+),
conda install -c fastai fastai
LEARNING PYTORCH WITH EXAMPLES (Pytorch v1.0)中文翻译
本教程通过自身包含的示例介绍PyTorch的基本概念。
PyTorch核心有两个主要特征:
1 一个可以在GPUs上运行的类似numpy的n维张量(tensor)
2 建立和训练神经网络时可以自动微分
我们将使用全连接的ReLU网络作为运行示例。 这个网络具有单个隐藏层,并且将通过梯度下降最小化网络输出与真实输出之间的欧几里德距离来训练网络来适合随机数据。
张量
热身:numpy
在介绍PyTorch之前,我们将首先使用numpy实现网络。
Numpy提供了一个n维数组对象和许多用于操作这些数组的函数。 Numpy是科学计算的通用框架; 它对计算图,深度学习或梯度都一无所知。 但是,通过使用numpy手动操作实现网络的前向和后向传播,我们可以轻松地使用numpy将双层网络适配到随机数据:
# -*- coding: utf-8 -*- # N is batch size, 输入数据的批量大小; D_in is input dimension, 输入数据的维度; # H is hidden dimension, 隐藏层维度; D_out is output dimension, 输出层维度. N, D_in, H, D_out = 64, 1000, 100, 10 # Create random input and output data 用随机数生成输入数据x、目标数据y x = np.random.randn(N, D_in) y = np.random.randn(N, D_out) # Randomly initialize weights 随机初始化权重参数 w1 = np.random.randn(D_in, H) w2 = np.random.randn(H, D_out) learning_rate = 1e-6 for t in range(500): # Forward pass: compute predicted y 向前传播 计算预测值y_pred h = x.dot(w1) #矩阵乘法 x:64*1000 w1:1000*100 h:64*100 h_relu = np.maximum(h, 0) #激活函数ReLU,非线性化 y_pred = h_relu.dot(w2) #h_relu:64*100 w2:100*10 y_pred:64*10 # Compute and print loss 计算和输出损失 loss = np.square(y_pred - y).sum() #误差的平方和 print(t, loss) # Backprop to compute gradients of w1 and w2 with respect to loss 反向传播,基于损失函数loss计算参数w1、w2的梯度 grad_y_pred = 2.0 * (y_pred - y) # 损失的梯度, grad_y_pred:64*10 grad_w2 = h_relu.T.dot(grad_y_pred) # 反向传播至w2,h_relu.T:100*64 grad_w2:100*10 grad_h_relu = grad_y_pred.dot(w2.T) # 反向传播至h_relu,w2.T:10*100 grad_h_relu:64*100 grad_h = grad_h_relu.copy() # 反向传播至h grad_h[h < 0] = 0 # grad_h:64*100 grad_w1 = x.T.dot(grad_h) # 反向传播至w1 x.T:1000*64 grad_w1:1000*100 # Update weights 更新权重 w1 -= learning_rate * grad_w1 w2 -= learning_rate * grad_w2
PyTorch:张量
Numpy是一个很棒的框架,但它不能利用GPUs来加速其数值计算。 对于现代深度神经网络,GPUs通常提供50倍或更高的加速,所以不幸的是,numpy对于现代深度学习来说还不够。
在这里,我们介绍最基本的PyTorch概念: 张量Tensor 。 PyTorch Tensor在概念上与numpy数组相同:Tensor是一个n维数组,PyTorch提供了许多用于在这些Tensors上运算的函数。 在幕后,Tensors可以跟踪计算图和渐变,但它们也可用作科学计算的通用工具。
与numpy不同,PyTorch Tensors可以利用GPUs加速其数值计算。 要在GPUs上运行PyTorch Tensor,只需将其转换为新的数据类型即可。
在这里,我们使用PyTorch Tensors将双层网络与随机数据相匹配。 像上面的numpy示例一样,我们需要手动实现通过网络的前向和后向传播:
# -*- coding: utf-8 -*- dtype = torch.float #定义tensor的数据类型 device = torch.device("cpu") # device = torch.device("cuda:0") # Uncomment this to run on GPU 使用GPU作为计算设备 # N is batch size; D_in is input dimension; N为输入数据的批量大小; D_in输入数据维度 # H is hidden dimension; D_out is output dimension. H 为隐含层维度; D_out输出层维度 N, D_in, H, D_out = 64, 1000, 100, 10 # Create random input and output data 用随机数生成输入数据x、目标数据y x = torch.randn(N, D_in, device=device, dtype=dtype) # 随机的输入数据 y = torch.randn(N, D_out, device=device, dtype=dtype) # 随机的输出数据 # Randomly initialize weights 用随机值初始化权重参数 w1 = torch.randn(D_in, H, device=device, dtype=dtype) # 随机初始化权重 w2 = torch.randn(H, D_out, device=device, dtype=dtype) learning_rate = 1e-6 for t in range(500): # Forward pass: compute predicted y 前向传播,计算预测值y_pred h = x.mm(w1) # 相当于numpy中的x.dot(w1) h_relu = h.clamp(min=0) # 相当于np.maximum(h, 0) y_pred = h_relu.mm(w2) # Compute and print loss 计算及打印损失值 loss = (y_pred - y).pow(2).sum().item() print(t, loss) # Backprop to compute gradients of w1 and w2 with respect to loss 反向传播,基于损失函数计算参数w1、w2的梯度 grad_y_pred = 2.0 * (y_pred - y) grad_w2 = h_relu.t().mm(grad_y_pred) grad_h_relu = grad_y_pred.mm(w2.t()) grad_h = grad_h_relu.clone() grad_h[h < 0] = 0 grad_w1 = x.t().mm(grad_h) # Update weights using gradient descent 使用梯度下降法更新权重参数 w1 -= learning_rate * grad_w1 w2 -= learning_rate * grad_w2
自动求导
PyTorch:张量Variables和自动求导autograd
在上面的例子中,我们不得不手动实现神经网络的前向和后向传播。 手动实现反向传递对于小型双层网络来说并不是什么大问题,但对于大型复杂网络来说,很快就会变得非常繁琐。
值得庆幸的是,我们可以使用自动微分来自动计算神经网络中的后向传播。 PyTorch中的autograd包提供了这个功能。 使用autograd时,网络的正向传播将定义计算图形 ; 图中的节点将是张量tensors,边将是从输入张量产生输出张量的函数。 通过此图反向传播,您可以轻松计算梯度。
这听起来很复杂,在实践中使用起来非常简单。 每个Tensor代表计算图中的节点。 如果x是具有x.requires_grad=True的Tensor,则x.grad是另一个Tensor,是相对于某个标量值保持x的梯度。
在这里,我们使用PyTorch Tensors和autograd来实现我们的双层网络; 现在我们不再需要手动实现通过网络的反向传递:
# -*- coding: utf-8 -*- import torch #定义tensor的数据类型 dtype = torch.float device = torch.device("cpu") # device = torch.device("cuda:0") # Uncomment this to run on GPU # N为输入数据的批量大小; D_in输入数据维度; # H 为隐含层维度; D_out输出层维度. N, D_in, H, D_out = 64, 1000, 100, 10 # 用随机数生成输入数据x、目标数据y的Tensor. # 这两个Tensors在反向传播时,无需进行梯度,故reqires_grad=False(缺省情况) x = torch.randn(N, D_in, device=device, dtype=dtype) y = torch.randn(N, D_out, device=device, dtype=dtype) # 用随机数生成权重参数w1、w2的Tensors # 这两个Tensors在进行反向传播时,需要计算梯度,故设置为requires_grad=True. w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True) w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True) learning_rate = 1e-6 for t in range(500): # 进行正向传播 #计算预测值y_pred. y_pred = x.mm(w1).clamp(min=0).mm(w2) #计算损失值. # 损失值为一个大小为(1,)的Tensor,即为标签。 # loss.item() 为损失值. loss = (y_pred - y).pow(2).sum() print(t, loss.item()) # Use autograd to compute the backward pass. This call will compute the # gradient of loss with respect to all Tensors with requires_grad=True. # After this call w1.grad and w2.grad will be Tensors holding the gradient # of the loss with respect to w1 and w2 respectively. # 用autograd计算反向传播,这里会根据所有设置了requires_grad=True的Tensor # 计算loss的梯度, w1.grad和w2.grad将会保存loss对于w1和w2的梯度 loss.backward() # Manually update weights using gradient descent. Wrap in torch.no_grad() # because weights have requires_grad=True, but we don't need to track this # in autograd. # An alternative way is to operate on weight.data and weight.grad.data. # Recall that tensor.data gives a tensor that shares the storage with # tensor, but doesn't track history. # You can also use torch.optim.SGD to achieve this. # 手动更新weight,需要用torch.no_grad(),因为weight有required_grad=True #但我们不需要在 autograd中跟踪这个操作 #torch.autograd.no_grad的作用是在上下文环境中切断梯度计算,在此模式下, #每一步计算结果中requires_grad都是False,即使input设置为quires_grad=True with torch.no_grad(): w1 -= learning_rate * w1.grad w2 -= learning_rate * w2.grad # Manually zero the gradients after updating weights 更新权重后,手工将梯度置零 w1.grad.zero_() w2.grad.zero_()
如果不用with torch.no_grad来更新权重参数,我们可以使用优化器来实现,具体可参考优化器(optim部分)。
PyTorch:定义新的autograd函数
在底层,每个原始autograd运算符实际上是两个在Tensors上操作的函数。 forward函数从输入Tensors计算输出张量。backward函数接收输出张量相对于某个标量值的梯度,并根据相同的标量值计算输入张量的梯度。
在PyTorch中,我们可以通过定义torch.autograd.Function的子类并实现forward和backward函数来轻松定义我们自己的autograd运算符。 然后我们可以使用我们的新autograd运算符,通过构造一个实例并像函数一样调用它,传递包含输入数据的Tensors。
在这个例子中,我们定义了自己的自定义自动求导功能,用于执行ReLU非线性映射,并使用它来实现我们的双层网络:
# -*- coding: utf-8 -*- import torch class MyReLU(torch.autograd.Function): """ We can implement our own custom autograd Functions by subclassing torch.autograd.Function and implementing the forward and backward passes which operate on Tensors. 通过继承torch.autograd.Function,我们能执行自定义的自动求导函数,而且执行正向和反向传播 """ @staticmethod def forward(ctx, input): """ In the forward pass we receive a Tensor containing the input and return a Tensor containing the output. ctx is a context object that can be used to stash information for backward computation. You can cache arbitrary objects for use in the backward pass using the ctx.save_for_backward method. 在正向传递中,我们收到一个包含输入和返回输出的张量, ctx是一个上下文对象,可用于存储反向计算的信息. 您可以使用ctx.save_for_backward方法缓存任意对象以用于反向传播。 """ ctx.save_for_backward(input) return input.clamp(min=0) @staticmethod def backward(ctx, grad_output): """ In the backward pass we receive a Tensor containing the gradient of the loss with respect to the output, and we need to compute the gradient of the loss with respect to the input. 在反向传播中,我们收到一个张量,其中包含相对于输出的损失梯度, 我们需要计算相关于输入的损失函数梯度。 """ input, = ctx.saved_tensors grad_input = grad_output.clone() grad_input[input < 0] = 0 return grad_input dtype = torch.float device = torch.device("cpu") # device = torch.device("cuda:0") # Uncomment this to run on GPU # N is batch size; D_in is input dimension; N为输入数据的批量大小; D_in输入数据维度 # H is hidden dimension; D_out is output dimension. H 为隐含层维度; D_out输出层维度 N, D_in, H, D_out = 64, 1000, 100, 10 # Create random Tensors to hold input and outputs. 用随机值生成输入数据x及目标数据y x = torch.randn(N, D_in, device=device, dtype=dtype) y = torch.randn(N, D_out, device=device, dtype=dtype) # Create random Tensors for weights. 初始化权重参数 w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True) w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True) learning_rate = 1e-6 for t in range(500): # To apply our Function, we use Function.apply method. We alias this as 'relu'. 把类MyReLU构造成一个实例relu,便于像函数一样调用。使用自己定义的自动求导方法 relu = MyReLU.apply # Forward pass: compute predicted y using operations; we compute # ReLU using our custom autograd operation. 向前传播,计算预测值y_pred,这里计算ReLU使用我们自定义的autograd y_pred = relu(x.mm(w1)).mm(w2) # Compute and print loss 计算及输出损失值 loss = (y_pred - y).pow(2).sum() print(t, loss.item()) # Use autograd to compute the backward pass. 使用autograd进行向后传播 loss.backward() # Update weights using gradient descent 使用梯度下降法更新权重参数 with torch.no_grad(): w1 -= learning_rate * w1.grad w2 -= learning_rate * w2.grad # Manually zero the gradients after updating weights 更新参数后,需要对梯度置为0 w1.grad.zero_() w2.grad.zero_()
Function与Module都可以对pytorch进行自定义拓展,使其满足网络的需求,但这两者还是有十分重要的不同:
1 Function一般只定义一个操作,因为其无法保存参数,因此适用于激活函数、pooling等操作;Module是保存了参数,因此适合于定义一层,如线性层,卷积层,也适用于定义一个网络。
2 Function需要定义三个方法:__init__, forward, backward(需要自己写求导公式);Module:只需定义__init__和forward,而backward的计算由自动求导机制构成。
3 可以不严谨的认为,Module是由一系列Function组成,因此其在forward的过程中,Function和Tensor组成了计算图,在backward时,只需调用Function的backward就得到结果,因此Module不需要再定义backward。
4 Module不仅包括了Function,还包括了对应的参数,以及其他函数与变量,这是Function所不具备的。
TensorFlow:静态图
PyTorch autograd看起来很像TensorFlow:在两个框架中我们定义了一个计算图,并使用自动微分来计算梯度。 两者之间最大的区别是TensorFlow的计算图是静态的 ,PyTorch使用动态计算图。
在TensorFlow中,我们定义计算图一次,然后一遍又一遍地执行相同的图,可能将不同的输入数据提供给图。 在PyTorch中,每个前向传播定义了一个新的计算图。
静态图很好,因为你可以预先优化图形; 例如,框架可能决定融合某些图形操作以提高效率,或者提出一种策略,用于在多个GPU或许多机器上分布图形。 如果您反复使用相同的图表,那么这个可能代价高昂的前期优化可以分摊,因为相同的图表会反复重新运行。
静态和动态图表不同的一个方面是控制流程。 对于某些模型,我们可能希望对每个数据点执行不同的计算; 例如,可以针对每个数据点针对不同数量的时间步长展开循环网络; 这种展开可以作为循环实现。 使用静态图形,循环结构需要是图形的一部分; 因此,TensorFlow提供了诸如tf.scan运算符,用于将循环嵌入到图中。 使用动态图形情况更简单:因为我们为每个示例动态构建图形,我们可以使用常规命令流控制来执行每个输入不同的计算。
NN模块
PyTorch:nn
计算图和autograd是用于定义复杂运算符和自动获取导数的非常强大的范例; 然而,对于大型神经网络,原始autograd可能有点太低级别。
在构建神经网络时,我们经常考虑将计算安排到层中 ,其中一些层具有可学习的参数 ,这些参数将在学习期间进行优化。
在TensorFlow中,像Keras , TensorFlow-Slim和TFLearn这样的软件包提供了对构建神经网络有用的原始计算图形的更高级别的抽象。
在PyTorch中, nn包服务于同样的目的。 nn包定义了一组模块 ,它们大致相当于神经网络层。 模块接收输入张量并计算输出张量,但也可以保持内部状态,例如包含可学习参数的张量。 nn包还定义了一组在训练神经网络时常用的有用损失函数。
当使用nn时,编写神经网络模型的过程可以大大简化。在这个例子中,我们使用nn包来实现我们的双层网络:
# -*- coding: utf-8 -*- import torch # N is batch size; D_in is input dimension; N为输入数据的批量大小; D_in输入数据维度 # H is hidden dimension; D_out is output dimension. H 为隐含层维度; D_out输出层维度 N, D_in, H, D_out = 64, 1000, 100, 10 # Create random Tensors to hold inputs and outputs 创建输入、目标数据的Tensor,随机生成训练数据 x = torch.randn(N, D_in) y = torch.randn(N, D_out) # Use the nn package to define our model as a sequence of layers. nn.Sequential # is a Module which contains other Modules, and applies them in sequence to # produce its output. Each Linear Module computes output from input using a # linear function, and holds internal Tensors for its weight and bias. #使用nn包来定义model作为layers 的序列. # nn.Sequential是一个Module,该Module包含其他Modules(如Linear, ReLU等) # Sequential Module会序列化的执行这些 Modules, 並且自动计算其output和grads. #注意因为是序列化执行的, 因此无需自定义 forward. 这是与 nn.Module 的区別之一. # 建立Sequential模型(顺序执行若干步骤) # nn.Sequential是一个包含其他Module的模块,按顺序调用它们来产生输出 # 每个Linear模块用线性方程来计算输出,为weight和bias保留中间Tensor model = torch.nn.Sequential( torch.nn.Linear(D_in, H), torch.nn.ReLU(), torch.nn.Linear(H, D_out), ) # The nn package also contains definitions of popular loss functions; in this # case we will use Mean Squared Error (MSE) as our loss function. #定义损失函数,这里用Mean Squared Error (MSE)作为损失函数 loss_fn = torch.nn.MSELoss(reduction='sum') # 使用均方误差 learning_rate = 1e-4 for t in range(500): # Forward pass: compute predicted y by passing x to the model. Module objects # override the __call__ operator so you can call them like functions. When # doing so you pass a Tensor of input data to the Module and it produces # a Tensor of output data. #前向传播,计算预测值 y_pred = model(x) # Compute and print loss. We pass Tensors containing the predicted and true # values of y, and the loss function returns a Tensor containing the # loss. # 计算及打印损失值 loss = loss_fn(y_pred, y) print(t, loss.item()) # Zero the gradients before running the backward pass. # 进行反向传播前,需要对梯度清零 model.zero_grad() # Backward pass: compute gradient of the loss with respect to all the learnable # parameters of the model. Internally, the parameters of each Module are stored # in Tensors with requires_grad=True, so this call will compute gradients for # all learnable parameters in the model. #反向传播,根据model参数计算损失函数的梯度 #每个model的 parameters 存放在含requires_grad=True标签的Tensors中 loss.backward() # Update the weights using gradient descent. Each parameter is a Tensor, so # we can access its gradients like we did before. # 利用梯度下降法更新权重参数 .每一个参数都是一个Tensor,并可获取他们的梯度 with torch.no_grad(): for param in model.parameters(): param -= learning_rate * param.grad # 手动更新参数
PyTorch:optim
到目前为止,我们通过手动改变持有可学习参数的torch.no_grad()更新模型的权重(使用torch.no_grad()或.data以避免在autograd中跟踪历史记录)。 对于像随机梯度下降这样的简单优化算法来说,这不是一个巨大的负担,但在实践中,我们经常使用更复杂的优化器如AdaGrad,RMSProp,Adam等来训练神经网络。
PyTorch中的optim包抽象出优化算法的思想,并提供常用优化算法的实现。
在这个例子中,我们将使用nn包像以前一样定义我们的模型,但我们将使用optim包提供的Adam算法优化模型:
# -*- coding: utf-8 -*- import torch # N is batch size; D_in is input dimension; # H is hidden dimension; D_out is output dimension. # N为输入数据的批量大小; D_in输入数据维度; # H 为隐含层维度; D_out输出层维度 N, D_in, H, D_out = 64, 1000, 100, 10 # Create random Tensors to hold inputs and outputs # 创建随机数据x和目标数据y x = torch.randn(N, D_in) y = torch.randn(N, D_out) # Use the nn package to define our model and loss function. #使用nn包定义model及损失函数 #建立Sequential模型(顺序执行若干步骤) model = torch.nn.Sequential( torch.nn.Linear(D_in, H), torch.nn.ReLU(), torch.nn.Linear(H, D_out), ) loss_fn = torch.nn.MSELoss(reduction='sum') # Use the optim package to define an Optimizer that will update the weights of # the model for us. Here we will use Adam; the optim package contains many other # optimization algoriths. The first argument to the Adam constructor tells the # optimizer which Tensors it should update. #使用optim包定义更新模型参数的优化器. 我们选择Adam优化器; optim package #还包含其他优化算法. Adam的第一个参数为需要更新的Tensors learning_rate = 1e-4 optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) for t in range(500): # Forward pass: compute predicted y by passing x to the model. # 计算预测值 y_pred = model(x) # Compute and print loss. # 计算打印损失值 loss = loss_fn(y_pred, y) print(t, loss.item()) # Before the backward pass, use the optimizer object to zero all of the # gradients for the variables it will update (which are the learnable # weights of the model). This is because by default, gradients are # accumulated in buffers( i.e, not overwritten) whenever .backward() # is called. Checkout docs of torch.autograd.backward for more details. # 反向传播前, 使用优化器对所有需要更新参数的梯度清零 #这是因为缺省情况下,梯度会存放在缓存里 optimizer.zero_grad() # Backward pass: compute gradient of the loss with respect to model # parameters # 反向传播 loss.backward() # Calling the step function on an Optimizer makes an update to its # parameters #更新参数 optimizer.step()
PyTorch:自定义nn模块
有时,您需要指定比现有模块序列更复杂的模型; 对于这些情况,您可以通过nn.Module并定义一个接收输入nn.Module并使用其他模块或Tensors上的其他autograd操作生成输出nn.Module的forward来定义您自己的模块。
在这个例子中,我们将我们的双层网络实现为自定义Module子类:
# -*- coding: utf-8 -*- import torch class TwoLayerNet(torch.nn.Module): def __init__(self, D_in, H, D_out): """ In the constructor we instantiate two nn.Linear modules and assign them as member variables. 通常我们將具有引用学习参数的层放在__init__函式中, 將不具有引用学习参数的操作放在forward中 """ super(TwoLayerNet, self).__init__() self.linear1 = torch.nn.Linear(D_in, H) self.linear2 = torch.nn.Linear(H, D_out) def forward(self, x): """ In the forward function we accept a Tensor of input data and we must return a Tensor of output data. We can use Modules defined in the constructor as well as arbitrary operators on Tensors. """ h_relu = self.linear1(x).clamp(min=0) y_pred = self.linear2(h_relu) return y_pred # N is batch size; D_in is input dimension; # H is hidden dimension; D_out is output dimension. # N为输入数据的批量大小; D_in输入数据维度; # H 为隐含层维度; D_out输出层维度. N, D_in, H, D_out = 64, 1000, 100, 10 # Create random Tensors to hold inputs and outputs x = torch.randn(N, D_in) y = torch.randn(N, D_out) # Construct our model by instantiating the class defined above # 实例化类,构建模型 # 使用自己建立的TwoLayerNet类来搭建网络 model = TwoLayerNet(D_in, H, D_out) # Construct our loss function and an Optimizer. The call to model.parameters() # in the SGD constructor will contain the learnable parameters of the two # nn.Linear modules which are members of the model. #构建损失函数及优化器. 其中model.parameters()包含学习参数 criterion = torch.nn.MSELoss(reduction='sum') optimizer = torch.optim.SGD(model.parameters(), lr=1e-4) for t in range(500): # Forward pass: Compute predicted y by passing x to the model # 传入x,计算预测值 y_pred = model(x) # Compute and print loss loss = criterion(y_pred, y) print(t, loss.item()) # Zero gradients, perform a backward pass, and update the weights. # 对梯度清零,执行反向传播并更新参数 optimizer.zero_grad() loss.backward() optimizer.step()
PyTorch:控制流+权重共享
作为动态图和权重共享的一个例子,我们实现了一个非常独特的模型:一个完全连接的ReLU网络,在每个前向传递中选择1到4之间的随机数并使用那么多隐藏层,重复使用相同的权重多次计算最里面的隐藏层。
对于这个模型,我们可以使用普通的Python流控制来实现循环,并且我们可以通过在定义前向传递时多次重复使用相同的模块来实现最内层之间的权重共享。
我们可以轻松地将此模型实现为Module子类:
# -*- coding: utf-8 -*- import random import torch class DynamicNet(torch.nn.Module): def __init__(self, D_in, H, D_out): """ In the constructor we construct three nn.Linear instances that we will use in the forward pass. 实现三个 nn.Linear 层 """ super(DynamicNet, self).__init__() self.input_linear = torch.nn.Linear(D_in, H) self.middle_linear = torch.nn.Linear(H, H) self.output_linear = torch.nn.Linear(H, D_out) def forward(self, x): """ For the forward pass of the model, we randomly choose either 0, 1, 2, or 3 and reuse the middle_linear Module that many times to compute hidden layer representations. Since each forward pass builds a dynamic computation graph, we can use normal Python control-flow operators like loops or conditional statements when defining the forward pass of the model. Here we also see that it is perfectly safe to reuse the same Module many times when defining a computational graph. This is a big improvement from Lua Torch, where each Module could be used only once. 在PyTorch中, 我们可以通过for循环来随机的选择中间层的层数, 使得每一次 执行forward函式时, 都有不同的中间层层数. 而这些中间层都来自于同一个Module例項, 因而具有共享的权重参数 """ h_relu = self.input_linear(x).clamp(min=0) for _ in range(random.randint(0, 3)): h_relu = self.middle_linear(h_relu).clamp(min=0) y_pred = self.output_linear(h_relu) return y_pred # N is batch size; D_in is input dimension; # H is hidden dimension; D_out is output dimension. # N为输入数据的批量大小; D_in输入数据维度; # H 为隐含层维度; D_out输出层维度. N, D_in, H, D_out = 64, 1000, 100, 10 # Create random Tensors to hold inputs and outputs x = torch.randn(N, D_in) y = torch.randn(N, D_out) # Construct our model by instantiating the class defined above model = DynamicNet(D_in, H, D_out) # Construct our loss function and an Optimizer. Training this strange model with # vanilla stochastic gradient descent is tough, so we use momentum criterion = torch.nn.MSELoss(reduction='sum') optimizer = torch.optim.SGD(model.parameters(), lr=1e-4, momentum=0.9) for t in range(500): # Forward pass: Compute predicted y by passing x to the model y_pred = model(x) # Compute and print loss loss = criterion(y_pred, y) print(t, loss.item()) # Zero gradients, perform a backward pass, and update the weights. optimizer.zero_grad() loss.backward() optimizer.step()
参考
CNN
pytorch-tutorials-examples-and-books/convolutional-neural-network(CNN) 卷积神经网络/MNIST-using-CNN.py
pytorch-tutorials-examples-and-books/convolutional-neural-network(CNN) 卷积神经网络/mycnn.py
pytorch-beginner/04-Convolutional Neural Network/convolution_network.py
pytorch-tutorial/tutorials/02-intermediate/convolutional_neural_network/main.py
How to input a matrix to CNN in pytorch
Input numpy ndarray instead of images in a CNN
How to load a list of numpy arrays to pytorch dataset loader?
The photogenerated charges in type-II composite are spatially separated but the redox abilities of electrons and holes are weakened, which is not favorable for H2 evolution requiring high redox potential of H2/H+. In contrary, Z-scheme composite can retain photogenerated charges with stronger redox capability separately on two semiconductors, and thus satisfies the requirements for hydrogen evolution.
Huang Z F, Song J, Wang X, et al. Switching charge transfer of C3N4/W18O49 from type-II to Z-scheme by interfacial band bending for highly efficient photocatalytic hydrogen evolution[J]. Nano Energy, 2017, 40: 308-316.
南无观世音菩萨。南无佛,南无法,南无僧。
与佛有因,与佛有缘。佛法相因,常乐我静。
朝念观世音,暮念观世音。念念从心起,念佛不离心。
天罗神,地罗神。人离难,难离身。一切灾殃化为尘。
南无摩诃般若波罗密。
参考:
硬件清单:
GPU 1080Ti原厂公版 6488*4
CPU i7-6850K(*)/i7-6900K/i7-6950K 2599*1
主板 华硕X99-E-WS 3849*1
内存 金士顿-骇客神条DDR4-16G-3200MHz 1299*4
硬盘 希捷-4TB-7200转 560*1
电源 EVGA-1600W 2399*1
散热器 猫头鹰NH-D15 699*1
机箱 美商海盗船780T黑色全塔机箱 1259*1
总价:42513
参考:
VASP吸收光谱的计算:
1 SCF计算介电常数,
2 运行optics.sh获得REAL.in和IMAG.in,再使用VASPKIT根据介电常数虚部获得吸收光谱数据。
参考:
官方下载phonopy-1.12.8.4.tar.gz,安装及测试文件下载。需要Python2.7.x。
1 .bashrc # python 2 export PATH="/THFS/home/longr/soft/anaconda2/bin:$PATH" export PYTHONPATH=/THFS/home/longr/soft/anaconda2/lib/python2.7/site-packages #phonopy export PATH=/THFS/home/longr/soft/phonopy-1.12.8.4/scripts:$PATH 2 python setup.py install --user
Force Calculators
1
phonopy -d --dim="2 2 2"
运行build.py;
2 提交job.sh;
3
phonopy -f {001..072}/vasprun.xml
生成FORCE_SETS;
4
mesh.conf,
ATOM_NAME = C N H Pb I DIM = 2 2 2 MP = 1 1 1
phonopy -p mesh.conf #生成total_dos.dat,遇到matplotlib报错正常,依旧生成dat文件。 python phonon_dos_plot.py #plot
5
band.conf,
ATOM_NAME = C N H Pb I DIM = 2 2 2 BAND = 0.5 0.5 0.5 0.0 0.0 0.0 0.5 0.5 0.0 0.0 0.5 0.0
phonopy -p band.conf #获得band.yaml phonopy-bandplot --gnuplot band.yaml > band.dat #将band.yaml转为origin dat数据
Total Time: 38.45 h
DFPT Force Constants
phonopy -d --dim="2 2 2" -c POSCAR-unitcell
将SPOSCAR为POSCAR,提交job.sh,
phonopy --fc vasprun.xml phonopy --dim="2 2 2" -c POSCAR-unitcell -p mesh.conf phonopy --dim="2 2 2" -c POSCAR-unitcell -p band.conf phonopy-bandplot --gnuplot band.yaml > band.dat
Total Time: 41.59 h
注意:
以下错误信息直接忽略,
QXcbConnection: Could not connect to display Aborted (core dumped)
参考:
使用 VASP 进行 Bader 电荷分析,可以得到原子周围的电子数,从而近似得到原子的化合价。
SCF计算,修改INCAR,
LAECHG =.TRUE.
以MAPbI3为例,POTCAR,
Element (and appendix) default cutoff ENMAX (eV) valency C C 400 4 N N 400 5 H H 250 1 Pb Pb_d 238 14 I I 176 7
计算完成后生成文件 AECCAR0、AECCAR1、AECCAR2,运行下面命令,chgsum.pl和bader下载,
./chgsum.pl AECCAR0 AECCAR2 ./bader CHGCAR -ref CHGCAR_sum
生成文件 ACF.dat、AVF.dat、BCF.dat。
ACF.dat包含每个原子的坐标,根据Bader分区关联的电荷,根据Bader分区的整体百分比以及到表面的最小距离。 如果使用伪电位,则应将该距离与核心区域的最大截止半径进行比较。ACF.dat文件中,第一列的数字对应的是POSCAR中的原子顺序。每个原子的电荷对应的是CHARGE那一列。注意,该列中的电荷是该原子价层电子中的电子总数。如果你想要知道这个原子是带正电或者负电荷,需要减去POTCAR中的电子数目。
# X Y Z CHARGE MIN DIST ATOMIC VOL -------------------------------------------------------------------------------- 1 5.944247 6.102335 6.027354 3.807541 0.484059 12.344952 2 0.846529 6.101499 0.155424 6.260392 0.712337 13.996899 3 5.724197 6.100695 4.954106 0.926981 0.314647 8.964801 4 5.659497 0.899036 0.360077 0.894653 0.245347 7.946148 5 5.658650 5.206143 0.362407 0.897170 0.244886 7.955935 6 1.176072 0.842462 5.881985 0.510168 0.127403 3.221114 7 1.175258 5.255247 5.883771 0.473976 0.092612 3.045765 8 1.116555 0.000045 1.171149 0.511190 0.162292 3.135071 9 3.587664 3.050927 2.895929 13.053734 1.369209 29.630250 10 2.510075 3.049954 5.979174 7.548695 1.607185 51.262563 11 2.578308 6.102101 3.194155 7.553212 1.592758 52.156026 12 6.558447 3.053620 2.497538 7.562288 1.523589 56.885138 -------------------------------------------------------------------------------- VACUUM CHARGE: 0.0000 VACUUM VOLUME: 0.0000 NUMBER OF ELECTRONS: 50.0000
BCF.dat包含每个Bader最大值的坐标,该体积内的电荷,最近的原子以及该原子的距离。
参考:
Bader charge analysis(bader 布居分析)
How to perform Bader Charge Analysis steps from VASP output?
Calculating charge transfer using the Bader analysis method in VASP