深度学习入门笔记(6)——variable
在tensor进入网络进行前后传播和自求导前,需要用variable进行激活,variable不会改变矩阵的数据类型,在属性上细分为,data,grad,grad_fn,其中,data就是字面意思,用于读取tensor的数值;grad表示反向传播梯度;grad_fn返回获取variable所用手段(加减乘除),废话不多说,我们直接看代码,不足之处还请批评指正!
(1)定义:
x = torch.randn(3)
x = torch.autograd.Variable(x, requires_grad=True)
tensor([-0.2628, -1.7023, -0.8074], requires_grad=True)
Process finished with exit code 0
(2)属性:
# 定义一个tensor
matrix_1 = torch.Tensor([[2, 2], [1, 1]])
matrix_2 = torch.ones((2, 2))
matrix_3 = torch.Tensor([[1, 1], [2, 2]])
# variable激活矩阵
variable_1 = torch.autograd.Variable(matrix_1)
variable_2 = torch.autograd.Variable(matrix_2)
variable_3 = torch.autograd.Variable(matrix_3)
# 矩阵运算
matrix = variable_3 + variable_2 + variable_1
# 反向传播
matrix.backward()
print(variable_1.grad)
print(variable_2.grad)
print(variable_3.grad)
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
Process finished with exit code 1
这里是报错的原因及改正方案,我们改为requires_grad=True后重新运行
RuntimeError: grad can be implicitly created only for scalar outputs
Process finished with exit code 1
这里还有一个报错,解决方案在这,我们写入torch.ones_like(matrix)后重新运行,就会得到输出
tensor([[1., 1.],
[1., 1.]])
tensor([[1., 1.],
[1., 1.]])
tensor([[1., 1.],
[1., 1.]])
Process finished with exit code 0
这里其实涉及标量转换,实质上是标量求导,但variable也可以进行矩阵求导,我们可以看下面一段代码:
x = torch.randn(3)
x = torch.autograd.Variable(x, requires_grad=True)
y = x*2
print(y)
y.backward(torch.FloatTensor([1,2,3]))
print(x.grad)
运行结果
tensor([-1.5543, -1.5927, -3.1763], grad_fn=<MulBackward0>)
tensor([2., 4., 6.])
Process finished with exit code 0