In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how the logical regression in the Pytorch foundation is, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Logical regression basically says:
For simple logical regression, we can use a neural network to express:
So you can see that the input is two inputs, so the corresponding weights are also two, and the weight matrix is 2 to 1.
Export to o=w1*x1+w2*x2
For the three samples, you can see the following formula:
So convert it to vector product:
So, for the three parameters, you can get the gradient vector:
Input processing: num_inputs=2# input the number of num_examples=1000# samples true_w= [2Mae Murray 3.4] # w parameter matrix true_b=4.2 # b offset parameter features=torch.tensor (np.random.normal (0score 1, (num_examples,num_inputs)), dtype=torch.float) # to build a basic sample set: where the distribution sampling of 0 is adopted, and the input sample set is 2 to 1000 Labels=true_w [0] * features [:, 0] * features [:, 0] + true_w [1] * features [:, 1] + true_b# tag set, that is, the last y, can be multiplied directly by formula accumulation; labels+=torch.tensor (np.random.normal (0Med 0.01), dtype=torch.float) # adds a disturbance term batch_size=10dataset=Data.TensorDataset (features,labels) # uses the Data function to build a dataset Data_iter=Data.DataLoader (dataset,batch_size,shuffle=True) # splits the constructed dataset, referring to batch_size according to the number
For input, you can refer to the above, and for your own dataset, you can directly use Data.TensorDataset to construct.
After that, we specify the structure and initial parameters of the network, and load dataset directly to learn the parameters.
Network structure:
There are usually two ways to define a network structure:
1. Define using class inheritance
two。 Defined by using the function to pass parameters and accumulate layer by layer
If you define it through class inheritance:
Class LinearNet (nn.Module): def _ init__ (self, n_feature): super (LinearNet, self). _ _ init__ () self.linear = nn.Linear (n_feature, 1) # forward definition of forward propagation def forward (self, x): y = self.linear (x) return ynet = LinearNet (num_inputs)
You can see that the LinearNet class inherits from the nn.Module class
If you define it by adding it layer by layer:
It is usually defined using the Sequential function:
# net = nn.Sequential (nn.Linear (num_inputs, 1) # other layers can be passed in here) # net = nn.Sequential () net.add_module ('linear', nn.Linear (num_inputs, 1)) # net.add_module. # three from collections import OrderedDictnet = nn.Sequential ([(' linear', nn.Linear (num_inputs)) 1)) #. ]))
You can see the three most commonly used methods
In fact, there are many ways to write the network structure, and you can also use the network constructor defined by yourself to return a net, which is also a common operation.
After building the network, you need to initialize the model parameters, that is, to give a default value to all the wline b in the network:
Init.normal_ (net [0] .weight, mean=0,std=0.01) init.constant_ (net [0] .bias, val=0)
Each element of the weight parameter is initialized to a random sampling normal distribution with a mean of 0 and a standard deviation of 0.01, and the deviation is initialized to zero.
Then we define a loss function. For linear regression, we can use MSEloss.
For the optimizer, we use learning rate=0.03,SGD gradient descent algorithm to optimize.
Loss=nn.MSELoss () optimizer=optim.SGD (net.parameters (), lr=0.003)
For the adjustment of learning rate, we can also make dynamic adjustment, such as hierarchical adjustment, dynamic adjustment:
Optimizer = optim.SGD ([# if you do not specify a learning rate for a parameter Use the outermost default learning rate {'params': net.subnet1.parameters ()}, # lr=0.03 {' params': net.subnet2.parameters (), 'lr': 0.01}] Lr=0.03) # adjust the learning rate for param_group in optimizer.param_groups: param_group ['lr'] * = 0.1times the previous training stage: num_epochs = 3for epoch in range (1, num_epochs + 1): for X, y in data_iter: output = net (X) l = loss (output, y.view (- 1) 1) optimizer.zero_grad () # gradient zeroing Equivalent to net.zero_grad () l.backward () optimizer.step () print ('epoch% d, loss:% f'% (epoch, l.item ()) dense = net [0] print (true_w, dense.weight) print (true_b, dense.bias)
So you can see that in the end, you can compare it by looking at the parameters.
It is worth noting that after each round of training, remember to clear the residual gradient of the optimizer to prevent accumulation.
On the basis of Pytorch logical regression is how to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.