pytorch build neural networks is very simple and straightforward, here are two of their own commonly used build mode:
import torch import as nn
first:
class NN(): def __init__(self): super(NN,self).__init__() =( (30,40), (), (40,60), (), (60,10), () ) [0]..uniform_(-3e-3, 3e-3) [0].(-1,1) def forward(self,states): return (states)
This one writes the whole network in a Sequential, and the network parameter settings can be set individually after the network is built:[0]. .uniform_(-3e-3,3e-3), this is to set the weight of the first linear to be uniformly distributed between (-3e-3, 3e-3),bias is uniformly distributed between -1 and 1.
second:
class NN1(): def __init__(self): super(NN1,self).__init__() self.Linear1=(30,40) self..fill_(-0.1) #self..uniform_(-3e-3,3e-3) self..fill_(-0.1) self.layer1=(self.Linear1,()) self.Linear2=(40,60) self.layer2=(self.Linear2,()) self.Linear3=(60,10) self.layer3=(self.Linear3,()) def forward(self,states): return (states)
The network parameters can be set directly after the definition of the linear layer, such as here for the first linear layer is set like this: self...fill_(-0.1),self...fill_(-0.1).
You can see the effect of the parameter after defining it this way:
Net=NN() print("0:",[0]) print("weight:",type([0].weight)) print("weight:",type([0].)) print("bias",[0].) print('1:',[1]) #print("weight:",[1].) print('2:',[2]) print('3:',[3]) #print([-1]) Net1=NN1()
print(Net1.)
Output:
0: Linear (30 -> 40) weight: <class ''> weight: <class ''> bias -0.6287 -0.6573 -0.0452 0.9594 -0.7477 0.1363 -0.1594 -0.1586 0.0360 0.7375 0.2501 -0.1371 0.8359 -0.9684 -0.3886 0.7200 -0.3906 0.4911 0.8081 -0.5449 0.9872 0.2004 0.0969 -0.9712 0.0873 0.4562 -0.4857 -0.6013 0.1651 0.3315 -0.7033 -0.7440 0.6487 0.9802 -0.5977 0.3245 0.7563 0.5596 0.2303 -0.3836 [ of size 40] 1: ReLU () 2: Linear (40 -> 60) 3: Tanh () -0.1000 -0.1000 -0.1000 ... -0.1000 -0.1000 -0.1000 -0.1000 -0.1000 -0.1000 ... -0.1000 -0.1000 -0.1000 -0.1000 -0.1000 -0.1000 ... -0.1000 -0.1000 -0.1000 ... ⋱ ... -0.1000 -0.1000 -0.1000 ... -0.1000 -0.1000 -0.1000 -0.1000 -0.1000 -0.1000 ... -0.1000 -0.1000 -0.1000 -0.1000 -0.1000 -0.1000 ... -0.1000 -0.1000 -0.1000 [ of size 40x30] Process finished with exit code 0
Note here that the type of self. is parameter of the network. and self. is FloatTensor.
Above this article on pytorch in the fully connected neural network to build two modes in detail is all I share with you, I hope to give you a reference, and I hope you support me more.