当前位置:网站首页>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)边栏推荐
猜你喜欢

LeetCode高频题128. 最长连续序列,经常被互联网大厂面试考到

Embedded classic communication protocol

Leveraging the blue ocean of household appliances consumption with "digital channels", the dealer online system enables enterprises to further their business

Codeforces Round #807 (Div. 2) A-C题解

2022 software testing skills robotframework + selenium library + Jenkins web Keyword Driven Automation practical tutorial

JS what situations can't use json Parse, json.stringify deep copy and a better deep copy method

uniapp 总结篇 (小程序)

Interviewer: are you sure redis is a single threaded process?

Machine learning how to achieve epidemic visualization -- epidemic data analysis and prediction practice

54:第五章:开发admin管理服务:7:人脸入库流程;人脸登录流程;浏览器开启视频调试模式(以便能够在本机的不安全域名的情况下,也能去开启摄像头);
随机推荐
BGP federal experiment
Cloud native enthusiast weekly: the evolution of Prometheus architecture
对话Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
Forget the root password
GBase 8c 配置设置函数
软件测试面试题:你所熟悉的软件测试类型有哪些?
【数据库数据恢复】SQL Server数据库磁盘空间不足的数据恢复案例
GBase 8c 服务器信号函数
Gbase 8C annotation information function
微信小程序图片根据屏幕比例缩放
Hcip 13th day notes
Flex布局学习完成PC端
GBase 8c 事务ID和快照(三)
软件测试面试题:测试计划工作的目的是什么?测试计划工作的内容都包括什么?其中哪些是最重要的?
损失函数-交叉熵的原理及实现
一种比读写锁更快的锁,还不赶紧认识一下
Establishment of elk log analysis system
Codeforces Round #810 (Div. 2)A~C题解
go 学习02 基础知识
LeetCode 第 302 场周赛