当前位置: 首页 > java >正文

杂项知识点

杂项

  • 1 激活函数
    • 1.1 sigmoid
    • 1.2 tanh
    • 1.3 Relu
    • 1.4 leakRelu

1 激活函数

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

1.1 sigmoid


import torch
import numpy as np
import matplotlib.pyplot as plt
# sigmoid + tanh + Relu + leakRelu
## 1 sigmoid
### 1.1 代码复现sigmoid
#(1)函数
def sigmoid(x):return 1./(1+torch.exp(-x))def sigmoid_diff(x):t = sigmoid(x)return t*(1-t)
#(2))检测
x = torch.arange(3)
print(sigmoid(x))
print(sigmoid_diff(x))
print("#"*10)## 1.2 用torch检测sigmoid的导数
for i in range(3):x = torch.tensor([i],dtype=torch.float32,requires_grad=True)y = sigmoid(x)y.backward()print(x.grad.item(),end=", ")'''
tensor([0.5000, 0.7311, 0.8808])
tensor([0.2500, 0.1966, 0.1050])
##########
0.25, 0.1966119408607483, 0.10499356687068939
'''

1.2 tanh

import torch
import numpy as np
import matplotlib.pyplot as plt
## 2 thah
### 2.1 代码复现thah
#(1)函数
def thah(x):return 1.*(torch.exp(x)-torch.exp(-x))/(torch.exp(x)+torch.exp(-x))def thah_diff(x):t = torch.exp(2*x)return 4*t/(1+t)**2
#(2))检测
x = torch.arange(3)
print(thah(x))
print(thah_diff(x))
print("#"*10)## 2.2 用torch检测thah的导数
for i in range(3):x = torch.tensor([i],dtype=torch.float32,requires_grad=True)y = thah(x)y.backward()print(x.grad.item(),end=", ")
'''
tensor([0.0000, 0.7616, 0.9640])
tensor([1.0000, 0.4200, 0.0707])
##########
1.0,0.41997432708740234,0.07065081596374512
'''

1.3 Relu

import torch
import numpy as np
import matplotlib.pyplot as plt
## 3 Relu
### 3.1 代码复现Relu
#(1)函数
def Relu(x):return torch.maximum(torch.tensor(0),x)def Relu_diff(x):return torch.where(x>0,1,0)
#(2))检测
x = torch.arange(-2,4)print(Relu(x))
print(Relu_diff(x))
print("#"*10)# 3.2 用torch检测Relu的导数
for i in range(-2,4):x = torch.tensor([i],dtype=torch.float32,requires_grad=True)y = Relu(x)y.backward()print(x.grad.item(),end=", ")
'''
tensor([0, 0, 0, 1, 2, 3])
tensor([0, 0, 0, 1, 1, 1])
##########
0.0, 0.0, 0.5, 1.0, 1.0, 1.0, 
'''

1.4 leakRelu

import torch
import numpy as np
import matplotlib.pyplot as plt
## 4 LeakRelu
### 4.1 代码复现LeakRelu
#(1)函数
def LeakRelu(x,a=0.01):return torch.maximum(torch.tensor(a),x)def LeakRelu_diff(x):return torch.where(x>0,1,0.01)
#(2))检测
x = torch.arange(3)
print(LeakRelu(x))
print(LeakRelu_diff(x))
print("#"*10)## 4.2 用torch检测LeakRelu的导数
for i in range(3):x = torch.tensor([i],dtype=torch.float32,requires_grad=True)y = LeakRelu(x)y.backward()print(x.grad.item(),end=", ")'''
tensor([0.0100, 1.0000, 2.0000])
tensor([0.0100, 1.0000, 1.0000])
##########
0.0, 1.0, 1.0, '''
http://www.xdnf.cn/news/1821.html

相关文章:

  • 基于python代码的通过爬虫方式实现快手发布视频(2025年4月)
  • 模式识别的局限和确认偏误消除偏见
  • LeetCode 每日一题 2799. 统计完全子数组的数目
  • 项目笔记2:post请求是什么,还有什么请求
  • Uni-App 多端电子合同开源项目介绍
  • 单精度浮点运算/定点运算下 MATLAB (VS) VIVADO
  • Excalidraw工具分享
  • 速成GO访问sql,个人笔记
  • CodeMeter Runtime 安装失败排查与解决指南
  • 蓝牙调试助手APP波形图版
  • 软件工程效率优化:一个分层解耦与熵减驱动的系统框架
  • java配置
  • mysql知识总结 索引篇
  • Flutter Dart中的类 对象
  • 05-GPIO原理
  • 「零配置陷阱」:现代全栈工具链的复杂度管控实践
  • java多线程(7.0)
  • 发放优惠券
  • Java常用API详解
  • 通过VIN车辆识别代码查询_精准版API,获取车辆精准参数
  • 并发编程【深度解剖】
  • Android学习总结之Glide篇(缓存和生命周期)
  • 数据结构与算法(十二):图的应用-最小生成树-Prim/Kruskal
  • 人工智能---当机器人遇到大模型会产生火花吗?
  • 【C++】STL之deque
  • CPU 虚拟化机制——受限直接执行 (LDE)
  • 悟空统计在SEO优化中的核心作用:外链质量评估
  • SpringBoot入门实战(第八篇:项目接口-订单管理)完结篇
  • 高功率激光输出稳定性不足?OAS 光学软件来攻克
  • ap无法上线问题定位(交换机发包没有剥掉pvid tag)