杂项知识点

[复制链接]
发表于 2025-9-22 04:44:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
1 激活函数

常用的激活函数包罗sigmoid + tanh + Relu + leakRelu
1.1 sigmoid

  1. import torch
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. # sigmoid + tanh + Relu + leakRelu
  5. ## 1 sigmoid
  6. ### 1.1 代码复现sigmoid
  7. #(1)函数
  8. def sigmoid(x):
  9.     return 1./(1+torch.exp(-x))
  10. def sigmoid_diff(x):
  11.     t = sigmoid(x)
  12.     return t*(1-t)
  13. #(2))检测
  14. x = torch.arange(3)
  15. print(sigmoid(x))
  16. print(sigmoid_diff(x))
  17. print("#"*10)
  18. ## 1.2 用torch检测sigmoid的导数
  19. for i in range(3):
  20.     x = torch.tensor([i],dtype=torch.float32,requires_grad=True)
  21.     y = sigmoid(x)
  22.     y.backward()
  23.     print(x.grad.item(),end=", ")
  24. '''
  25. tensor([0.5000, 0.7311, 0.8808])
  26. tensor([0.2500, 0.1966, 0.1050])
  27. ##########
  28. 0.25, 0.1966119408607483, 0.10499356687068939
  29. '''
复制代码
1.2 tanh

  1. import torch
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. ## 2 thah
  5. ### 2.1 代码复现thah
  6. #(1)函数
  7. def thah(x):
  8.     return 1.*(torch.exp(x)-torch.exp(-x))/(torch.exp(x)+torch.exp(-x))
  9. def thah_diff(x):
  10.     t = torch.exp(2*x)
  11.     return 4*t/(1+t)**2
  12. #(2))检测
  13. x = torch.arange(3)
  14. print(thah(x))
  15. print(thah_diff(x))
  16. print("#"*10)
  17. ## 2.2 用torch检测thah的导数
  18. for i in range(3):
  19.     x = torch.tensor([i],dtype=torch.float32,requires_grad=True)
  20.     y = thah(x)
  21.     y.backward()
  22.     print(x.grad.item(),end=", ")
  23. '''
  24. tensor([0.0000, 0.7616, 0.9640])
  25. tensor([1.0000, 0.4200, 0.0707])
  26. ##########
  27. 1.0,0.41997432708740234,0.07065081596374512
  28. '''
复制代码
1.3 Relu

  1. import torch
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. ## 3 Relu
  5. ### 3.1 代码复现Relu
  6. #(1)函数
  7. def Relu(x):
  8.     return torch.maximum(torch.tensor(0),x)
  9. def Relu_diff(x):
  10.     return torch.where(x>0,1,0)
  11. #(2))检测
  12. x = torch.arange(-2,4)
  13. print(Relu(x))
  14. print(Relu_diff(x))
  15. print("#"*10)
  16. # 3.2 用torch检测Relu的导数
  17. for i in range(-2,4):
  18.     x = torch.tensor([i],dtype=torch.float32,requires_grad=True)
  19.     y = Relu(x)
  20.     y.backward()
  21.     print(x.grad.item(),end=", ")
  22. '''
  23. tensor([0, 0, 0, 1, 2, 3])
  24. tensor([0, 0, 0, 1, 1, 1])
  25. ##########
  26. 0.0, 0.0, 0.5, 1.0, 1.0, 1.0,
  27. '''
复制代码
1.4 leakRelu

  1. import torch
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. ## 4 LeakRelu
  5. ### 4.1 代码复现LeakRelu
  6. #(1)函数
  7. def LeakRelu(x,a=0.01):
  8.     return torch.maximum(torch.tensor(a),x)
  9. def LeakRelu_diff(x):
  10.     return torch.where(x>0,1,0.01)
  11. #(2))检测
  12. x = torch.arange(3)
  13. print(LeakRelu(x))
  14. print(LeakRelu_diff(x))
  15. print("#"*10)
  16. ## 4.2 用torch检测LeakRelu的导数
  17. for i in range(3):
  18.     x = torch.tensor([i],dtype=torch.float32,requires_grad=True)
  19.     y = LeakRelu(x)
  20.     y.backward()
  21.     print(x.grad.item(),end=", ")
  22. '''
  23. tensor([0.0100, 1.0000, 2.0000])
  24. tensor([0.0100, 1.0000, 1.0000])
  25. ##########
  26. 0.0, 1.0, 1.0,
  27. '''
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表