Machine learning pytorch platform code study notes (2) - Variable

 When Variable calculates, it silently builds a huge system behind the background curtain step by step, called the computational graph, computational graph. It will connect all the computational steps (nodes), and finally when the error is reversed, once The ability to calculate the modification range (gradient) in all variables, and tensor does not have this ability


1. We define a Variable:
 
 
 
 
import torch
from torch.autograd import Variable # torch 中 Variable 模块

# Mr Egg
tensor = torch.FloatTensor([[1,2],[3,4]])
# Put the eggs in the basket, requires_grad is to participate in the error back propagation, do you want to calculate the gradient
variable = Variable(tensor, requires_grad=True)

print(tensor)
"""
 1  2
 3  4
[torch.FloatTensor of size 2x2]
"""

print(variable)
"""
Variable containing:
 1  2
 3  4
[torch.FloatTensor of size 2x2]
"""

2.Variable calculation, gradient

square and mean

 
 
t_out = torch.mean(tensor*tensor)       # x^2
v_out = torch.mean(variable*variable)   # x^2
print(t_out)
#7.5

print(v_out)    
#Variable containing:
# 7.5000
#[torch.FloatTensor of size 1]

Only variables can be backpropagated

v_out.backward() # Simulate the error backward propagation of v_out

# Variable is a part of the calculation graph, which can be used to transfer errors.
# v_out = 1/4 * sum(variable*variable) This is the v_out calculation step in the calculation graph
# The gradient for v_out is, d(v_out)/d(variable) = 1/4*2*variable = variable/2

print(variable.grad) # Gradient of initial Variable
'''
 0.5000  1.0000
 1.5000  2.0000
'''
3. Get the data in the Variable

Note the difference:

print(variable) # Variable form, in addition to data also contains structure
"""
Variable containing:
 1  2
 3  4
[torch.FloatTensor of size 2x2]
"""

print(variable.data)    # tensor 形式
"""
 1  2
 3  4
[torch.FloatTensor of size 2x2]
"""

print(variable.data.numpy())    # numpy 形式
"""
[[ 1.  2.]
 [ 3.  4.]]
"""


Reference link: https://morvanzhou.github.io/tutorials/machine-learning/torch/2-02-variable/


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325438610&siteId=291194637