当前位置:网站首页>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语言——分支语句和循环语句

DRF generating serialization class code

域名重定向工具 —— SwitchHosts 实用教程

APP special test: traffic test

从0到1:图文投票小程序设计与研发笔记

还在纠结报表工具的选型么?来看看这个

chrome copies the base64 data of an image

System availability: 3 9s, 4 9s in SRE's mouth... What is it?

C#大型互联网平台管理框架源码:基于ASP.NET MVC+EF6+Bootstrap开发,支持多数据库

毫秒级!千万人脸库快速比对,上亿商品图片检索,背后的极速检索用了什么神器?
随机推荐
论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》
Mini Program Graduation Works WeChat Food Recipe Mini Program Graduation Design Finished Product (8) Graduation Design Thesis Template
qt-faststart 安装使用
Deep Learning Course2 Week 2 Optimization Algorithms Exercises
Create virtual environments with virtualenv and Virtualenvwrapper virtual environment management tools
PHP算法之有效的括号
How to use pywinauto and pyautogui to link the anime lady and sister please go home
Deep learning Course2 first week Practical aspects of Deep Learning exercises
y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)
加载字体时避免隐藏文本
计算两点之间的中点
PostgreSQL Basics--Common Commands
选择合适的 DevOps 工具,从理解 DevOps 开始
Postman batch test interface detailed tutorial
TCP 可靠吗?为什么?
drf生成序列化类代码
E - Integer Sequence Fair
论文理解【RL - Exp Replay】—— Experience Replay with Likelihood-free Importance Weights
下载安装 vscode(含汉化、插件的推荐和安装)
数据增强--学习笔记(图像类,cnn)