当前位置:网站首页>Principle and implementation of cross entropy
Principle and implementation of cross entropy
2022-07-28 02:06:00 【Falling gold】
principle
To be lazy , No hurry, no change
Realization
# After testing ,reduction Add it or not, the result is the same , But for insurance , Or add
ce_loss = F.cross_entropy(logits, labels, reduction="none")
print(ce_loss)python Realization
softmax
import numpy as np
x=np.array([[0.3, 0.6, 0.9, 1], [0.6, 0.4, 0.9, 0.5], [0.6, 0.2, 0.9, 0.5]]) # input data
W=np.random.randn(4,3) # Generate weights randomly
def softmax(x,W):
z=np.dot(x,W) # Get the vector of prediction probability
return np.exp(z) / np.sum(np.exp(z))
a=softmax(x,W)
print(a)
a0_result=np.argmax(a[0]) # On the sample a0, take a[0] The maximum probability in is taken as the classification result Be careful a[0].sum=1
a1_result=np.argmax(a[1])
a2_result=np.argmax(a[2])
print(a0_result)Cross entropy
stay softmax On the basis of :
import torch
labels=np.array([1,0,2])
def cross_entropy(labels,log_pt):
pt = torch.exp(log_pt) # adopt softmax Score after function
labels = labels.view(-1, 1) # Add one more dimension , For the use of gather Function to prepare
pt = pt.gather(1, labels) # Pick the one corresponding to the real value softmax Scoring , It can also be realized by using hot coding
#dim=1 Return by line
ce_loss = -torch.log(pt)
return ce_loss
ce_loss=cross_entropy(torch.tensor(labels),torch.tensor(a))
print(ce_loss)边栏推荐
- Common modules of ros2 launch files
- What are the important applications of MES system in manufacturing enterprises
- GBase 8c 配置设置函数
- 软件测试面试题:为什要在一个团队中开展测试工作?
- focal loss原理及实现
- 自定义类型:结构体,枚举,联合
- JS what situations can't use json Parse, json.stringify deep copy and a better deep copy method
- Cloud native enthusiast weekly: the evolution of Prometheus architecture
- Enterprise operation and maintenance practice - using aliyun container image service to pull and build images of overseas GCR and quay warehouses
- Domain Driven Design -- Terminology
猜你喜欢
![[interview: concurrent article 28:volatile] orderliness](/img/8d/c4c2ca08d8883e997709d208b7395b.png)
[interview: concurrent article 28:volatile] orderliness

C# 使用Abp仓储访问数据库时报错记录集

Digital economy is the core of future economic development

Uniapp summary (applet)

自定义类型:结构体,枚举,联合

测试/开发程序员的级别“陷阱“,级别不是衡量单维度的能力......

##ELK日志分析系统搭建##

二叉树的遍历和性质

Completely delete MySQL in Linux system

N32l43x FLASH read \ write \ erase operation summary
随机推荐
实时数仓:美团的实时数仓建设实践
Hcip 13th day notes
Lightweight project management system
go 学习01
二叉树的遍历和性质
什么是方法,什么是方法论:了解自我精进提升的底层逻辑
GBase 8c 事务ID和快照(一)
微信小程序图片根据屏幕比例缩放
ros2 launch文件常用模块
Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步
Interviewer: are you sure redis is a single threaded process?
Gbase 8C transaction ID and snapshot (IV)
Uniapp summary (applet)
shell正则和元字符
GBase 8c 事务ID和快照(四)
mongodb/mongoTemplate.upsert批量插入更新数据的实现
如何评估研发人员效能?软件工程师报告帮你看见每个人的贡献
53:第五章:开发admin管理服务:6:开发【admin管理员退出登录,接口】;(一个点:我们想要修改一个采用了某种编码方式的值时,新的值最好也按照这种编码方式编码后,再去修改;)
Gbase 8C backup control function (IV)
Leetcode high frequency question 128. the longest continuous sequence, which is often tested in interviews with Internet companies