当前位置:网站首页>JAX-based activation function, softmax function and cross entropy function
JAX-based activation function, softmax function and cross entropy function
2022-08-01 23:10:00 【Albert Darren】
1.tanh激活函数公式
tanh ( x ) = e x − e − x e x + e − x \tanh \left( x \right) =\frac{e^x-e^{-x}}{e^x+e^{-x}} \notag tanh(x)=ex+e−xex−e−x
2.基于JAX实现tanh激活函数
import jax.numpy as jnp
from jax import random
def tanh(x):
"""tanh function"""
return (jnp.exp(x)-jnp.exp(-x))/(jnp.exp(x)+jnp.exp(-x))
# Set the pseudorandom number seed
rng=random.PRNGKey(0)
# Standard normal sampling yields the input vector
x=random.normal(rng,shape=(4,1))
# 调用内置tanh函数实现
print(jnp.tanh(x))
# 调用自定义tanh函数实现
print(tanh(x))
3.softmax函数公式
s i = e x i ∑ i = 0 j e x i s_i=\frac{e^{x_i}}{\sum\limits _{i=0}^{j}e^{x_i} } \notag si=i=0∑jexiexi
其中 x i x_i xi表示第 i i ireal-valued output of a neuron
4.基于JAX实现softmax函数
import jax.numpy as jnp
import jax.nn as nn
def softmax(x,axis=-1):
"""softmax function"""
unnormalized=jnp.exp(x)
return unnormalized/jnp.sum(unnormalized)
# 定义数组
arr=jnp.arange(-2,4)
# 调用自定义softmax函数
print(softmax(arr))
# 调用jax自带softmax函数
print(nn.softmax(arr))
5.Cross-entropy function formula
H ( p , q ) = − ∑ i = 1 n p ( x i ) log ( q ( x i ) ) H\left( p,q \right) =-\sum_{i=1}^n{p\left( x_i \right) \log \left( q\left( x_i \right) \right)} \notag H(p,q)=−i=1∑np(xi)log(q(xi))
其中 p ( x ) p(x) p(x)represents the true probability distribution, q ( x ) q(x) q(x)represents the predicted probability distribution
6.基于JAXImplement the cross-entropy function
import jax.numpy as jnp
def cross_entropy(y_true,y_pred,eps=1e-7):
"""cross entropy function :param y_true:真实标签 :param y_pred:Neural network predicts labels :param eps:Default minimal positive number,The logarithm is guaranteed to be true0,增强logFunction Numerical Stability :return:交叉熵,保留到小数点后4位 """
y_true=jnp.array(y_true)
y_pred=jnp.array(y_pred)
res=-jnp.sum(y_true*jnp.log(y_pred+eps),axis=-1)
return jnp.round(res,4)
# 预测概率分布
y_pred=[0.1,0.05,0.6,0.0,0.05,0.1,0.0,0.1,0.0,0.0]
# 真实概率分布
y_true=[0,0,1,0,0,0,0,0,0,0]
# 交叉熵为0.5108
print(cross_entropy(y_true,y_pred))
边栏推荐
猜你喜欢

C语言——分支语句和循环语句

PDF转Word有那么难吗?做一个文件转换器,都解决了

Wechat Gymnasium Reservation Mini Program Graduation Design Finished Work Mini Program Graduation Design Finished Product (2) Mini Program Function

npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)

img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)

(Translation) How the contrasting color of the button guides the user's actions

E - Integer Sequence Fair

sys_kill系统调用

How to add a game character to a UE4 scene

域名重定向工具 —— SwitchHosts 实用教程
随机推荐
PostgreSQL Basics--Common Commands
Avoid , ,
, and tagsHow do programmers solve online problems gracefully?
Check if point is inside rectangle
C#大型互联网平台管理框架源码:基于ASP.NET MVC+EF6+Bootstrap开发,支持多数据库
Wechat Gymnasium Reservation Mini Program Graduation Design Finished Work Mini Program Graduation Design Finished Product (2) Mini Program Function
excel change cell size
浅析多服务在分布式系统下多事务通信处理机制方案
From 0 to 100: Notes on the Development of Enrollment Registration Mini Programs
npm npm
drf生成序列化类代码
论文理解【RL - Exp Replay】—— Experience Replay with Likelihood-free Importance Weights
【好书推荐】第一本无人驾驶技术书
【C补充】链表专题 - 单向链表
y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)
leetcode刷题
Chapter 19 Tips and Traps: Common Goofs for Novices
The monthly salary of the test post is 5-9k, how to increase the salary to 25k?
请问什么是 CICD
ROS2初级知识(8):Launching启动多节点