pytorch 变量、损失函数、激活函数分析

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 23 11:45:49 2019

@author: wang
"""

import torch
from torch.autograd import Variable

import torch.nn as nn
import torch.nn.functional as F

'''
变量 Variable
定义: pytorch中用于存储可训练数据的容器。
组成: Variable由可训练kernel的Tensor及其导数Tensor组成。
特性分析:
1. Variable创建
2. 变量梯度计算
'''

#%% 1 Variable 创建
x=Variable(torch.Tensor(2,2))
print("x variable: ",x)  # 输出 x.data
print ("x.data: ",x.data, ",x.grad: ",x.grad)

#%% 2 计算导数
x = Variable(torch.Tensor([[1,2],[3,4]]), requires_grad=True)
v_out = torch.mean(x*x)  # 输出一个标量
print(x.grad)  # 计算前没有梯度 None
v_out.backward(retain_graph=True) # 反向传播, 支持梯度叠加
print(x.grad)  # 计算前没有梯度 None
'''
tensor([[0.5000, 1.0000],
        [1.5000, 2.0000]])
'''
v_out.backward(retain_graph=True) # 反向传播, 支持梯度叠加
print(x.grad)  # 计算前没有梯度 None
'''
tensor([[1., 2.],
        [3., 4.]])
'''
x.grad = None  # 手动释放梯度
v_out.backward() # 反向传播, 支持梯度叠加


'''
损失函数
定义:
损失函数类型:
1. BCELoss 二分类损失函数
2. BCEWithLogitsLoss
3. 负对数似然损失函数 NLLLoss
4. 交叉熵损失函数 CrossEntropyLoss
5. L1损失函数
6. L2 损失函数
7. SmoothL1Loss 损失函数
'''
#%%1. 二分类损失函数
m = nn.Sigmoid()  # 计算sigmoid值
loss = nn.BCELoss()
x = torch.randn(3,requires_grad=True)  
target = torch.empty(3).random_(2)  # 输出 0, 1 值
output = loss(m(x), target)  # 输入为 0 - 1之间的值
output.backward()
print(x)
print(m(x))
print(target)
print(output)
print(x.grad)
x.grad = None  

#%% 2 BCEWithLogitsLoss 损失函数
loss = nn.BCEWithLogitsLoss()
x = torch.randn(3,requires_grad=True)
target = torch.empty(3).random_(2)
output = loss(x, target)
print(x)
print(m(x))
print(target)
print(output)
print(x.grad)
x.grad = None  

#%% 3 负对数似然损失函数
m = nn.LogSoftmax(dim=1)
loss = nn.NLLLoss()
# input is of size N x C = 3 x 5
x = torch.randn(3,5,requires_grad=True)
#each element in target has to have 0 <= value < C
target = torch.tensor([1,0,4])
output = loss(m(x), target)
print(output * 3)
print(m(x)[0,1] + m(x)[1,0] + m(x)[2,4])

#%% 4 交叉熵损失函数
loss = nn.CrossEntropyLoss()
# input is of size N x C = 3 x 5
x = torch.randn(3,5,requires_grad=True)
#each element in target has to have 0 <= value < C
target = torch.empty(3, dtype=torch.long).random_(5)
output = loss(x, target)
print(output)

#%% 5 L1损失函数
loss = nn.L1Loss()
x = torch.randn(1, 2, requires_grad=True)#tensor([[-0.0625, -2.1603]], requires_grad=True)
target = torch.randn(1, 2)#tensor([[0.6789, 0.9831]])
output = loss(x, target)#tensor(1.9424, grad_fn=<L1LossBackward>)
print(output)

#%% 6 L2损失函数
loss = nn.MSELoss()
x = torch.randn(1, 2, requires_grad=True)#tensor([[-1.4445, -2.4888]], requires_grad=True)
target = torch.randn(1, 2)#tensor([[ 0.7117, -0.1200]])
output = loss(x, target)#tensor(5.1303, grad_fn=<MseLossBackward>)
print(output)

#%% 7 SmoothL1Loss 损失函数
loss = nn.SmoothL1Loss()
x = torch.randn(1, 2, requires_grad=True)#tensor([[-1.4445, -2.4888]], requires_grad=True)
target = torch.randn(1, 2)#tensor([[ 0.7117, -0.1200]])
output = loss(x, target)#tensor(5.1303, grad_fn=<MseLossBackward>)
print(output)


'''
激活函数研究
'''

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值