针对类别的5中softmax_cross_entropy loss计算
# ----------------------------
#! Copyright(C) 2019
# All right reserved.
# 文件名称:xxx.py
# 摘 要:五种方式实现类别加权交叉熵
# 当前版本:1.0
# 作 者:
# 完成日期:2019-x-x
# ----------------------------- """
标签为1的类别权重变为其他类别的10倍
""" def weight_classes_cross_entropy_python():
import numpy as np def softmax(x):
sum_raw = np.sum(np.exp(x), axis=-1)
x1 = np.ones(np.shape(x))
for i in range(np.shape(x)[0]):
x1[i] = np.exp(x[i]) / sum_raw[i]
return x1 logits = np.array([[12, 3, 2], [3, 10, 1], [1, 2, 5], [4, 6.5, 1.2], [3, 6, 1]])
labels = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 0, 0], [0, 1, 0]]) # 每一行只有一个1
coe = [1, 10, 1, 1, 10]
logits_softmax = softmax(logits)
cross_entropy_vector = np.sum(-labels * np.log(logits_softmax), axis=1)
cross_entropy = np.mean(cross_entropy_vector * coe) print('weight_classes_cross_entropy_python计算结果:%5.4f' % cross_entropy) def weight_classes_cross_entropy_tf_losess():
import tensorflow as tf
labels = tf.Variable(initial_value=tf.constant([0, 1, 2, 0, 1]), dtype=tf.int32)
logits = tf.Variable(initial_value=tf.constant([[12, 3, 2], [3, 10, 1], [1, 2, 5], [4, 6.5, 1.2], [3, 6, 1]]), dtype=tf.float32)
coe = tf.where(tf.equal(labels, 1), tf.multiply(10, tf.ones_like(labels)), tf.ones_like(labels))
cross_entropy = tf.losses.sparse_softmax_cross_entropy(
logits=logits, labels=labels, weights=coe)
with tf.Session()as sess:
sess.run(tf.global_variables_initializer())
cross_entropy_value = sess.run(cross_entropy)
print('weight_classes_cross_entropy_python_tf_losess计算结果:%5.4f' % cross_entropy_value) def weight_classes_cross_entropy_tf_nn_sparse():
import tensorflow as tf
labels = tf.Variable(initial_value=tf.constant([0, 1, 2, 0, 1]), dtype=tf.int32)
logits = tf.Variable(initial_value=tf.constant([[12, 3, 2], [3, 10, 1], [1, 2, 5], [4, 6.5, 1.2], [3, 6, 1]]), dtype=tf.float32)
coe = tf.where(tf.equal(labels, 1), tf.multiply(tf.constant(10, dtype=tf.float32), tf.ones_like(labels, dtype=tf.float32)), tf.ones_like(labels, dtype=tf.float32))
cross_entropy_vector = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits) cross_entropy = tf.reduce_mean(tf.multiply(coe, cross_entropy_vector))
with tf.Session()as sess:
sess.run(tf.global_variables_initializer())
cross_entropy_value = sess.run(cross_entropy) print('weight_classes_cross_entropy_python_tf_nn_sparse计算结果:%5.4f' % cross_entropy_value) def weight_classes_cross_entropy_tf_nn():
import tensorflow as tf
onehot_labels = tf.Variable(initial_value=tf.constant([[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 0, 0], [0, 1, 0]]), dtype=tf.int32)
labels = tf.arg_max(onehot_labels, 1)
logits = tf.Variable(initial_value=tf.constant([[12, 3, 2], [3, 10, 1], [1, 2, 5], [4, 6.5, 1.2], [3, 6, 1]]), dtype=tf.float32)
coe = tf.where(tf.equal(labels, 1), tf.multiply(tf.constant(10, dtype=tf.float32), tf.ones_like(labels, dtype=tf.float32)), tf.ones_like(labels, dtype=tf.float32))
cross_entropy_vector = tf.nn.softmax_cross_entropy_with_logits_v2(labels=onehot_labels, logits=logits) cross_entropy = tf.reduce_mean(tf.multiply(coe, cross_entropy_vector))
with tf.Session()as sess:
sess.run(tf.global_variables_initializer())
cross_entropy_value = sess.run(cross_entropy)
print('weight_classes_cross_entropy_python_tf_nn计算结果:%5.4f' % cross_entropy_value) def weight_classes_cross_entropy_tf():
import tensorflow as tf
onehot_labels = tf.Variable(initial_value=tf.constant([[1.0, 0, 0], [0, 1, 0], [0, 0, 1], [1, 0, 0], [0, 1, 0]]), dtype=tf.float32)
labels = tf.arg_max(onehot_labels, 1)
logits = tf.Variable(initial_value=tf.constant([[12, 3, 2], [3, 10, 1], [1, 2, 5], [4, 6.5, 1.2], [3, 6, 1]]), dtype=tf.float32)
labels_softmax = tf.nn.softmax(logits)
cross_entropy_vector = -tf.reduce_sum(tf.multiply(onehot_labels, tf.log(labels_softmax)), axis=1)
coe = tf.where(tf.equal(labels, 1), tf.multiply(tf.constant(10, dtype=tf.float32), tf.ones_like(labels, dtype=tf.float32)), tf.ones_like(labels, dtype=tf.float32))
cross_entropy = tf.reduce_mean(tf.multiply(coe, cross_entropy_vector))
with tf.Session()as sess:
sess.run(tf.global_variables_initializer())
cross_entropy_value = sess.run(cross_entropy)
print('weight_classes_cross_entropy_tf计算结果:%5.4f' % cross_entropy_value) if __name__ == '__main__':
# weight_classes_cross_entropy_python()
# weight_classes_cross_entropy_tf_losess()
# weight_classes_cross_entropy_tf_nn_sparse()
# weight_classes_cross_entropy_tf_nn()
# weight_classes_cross_entropy_tf()
针对类别的5中softmax_cross_entropy loss计算的更多相关文章
- (转载)人脸识别中Softmax-based Loss的演化史
人脸识别中Softmax-based Loss的演化史 旷视科技 近期,人脸识别研究领域的主要进展之一集中在了 Softmax Loss 的改进之上:在本文中,旷视研究院(上海)(MEGVII Re ...
- C++ 类的实例中 内存分配详解
一个类,有成员变量:静态与非静态之分:而成员函数有三种:静态的.非静态的.虚的. 那么这些个东西在内存中到底是如何分配的呢? 以一个例子来说明: #include"iostream.h&qu ...
- c++类模板template中的typename使用方法-超级棒
转载:https://blog.csdn.net/vanturman/article/details/80269081 如有问题请联系我删除: 目录 起因 typename的常见用法 typename ...
- 怎样在caffe中添加layer以及caffe中triplet loss layer的实现
关于triplet loss的原理.目标函数和梯度推导在上一篇博客中已经讲过了.详细见:triplet loss原理以及梯度推导.这篇博文主要是讲caffe下实现triplet loss.编程菜鸟.假 ...
- 浅谈人脸识别中的loss 损失函数
浅谈人脸识别中的loss 损失函数 2019-04-17 17:57:33 liguiyuan112 阅读数 641更多 分类专栏: AI 人脸识别 版权声明:本文为博主原创文章,遵循CC 4.0 ...
- 第16/24周 SQL Server 2014中的基数计算
大家好,欢迎回到性能调优培训.上个星期我们讨论在SQL Server里基数计算过程里的一些问题.今天我们继续详细谈下,SQL Server 2014里引入的新基数计算. 新基数计算 SQL Serve ...
- GLSL 中的光照计算
理论知识转载地址:http://blog.csdn.net/ym19860303/article/details/25545933 1.Lambert模型(漫反射) 环境光: Iambdiff = K ...
- 管道设计CAD系统中重量重心计算
管道设计CAD系统中重量重心计算 eryar@163.com Abstract. 管道设计CAD系统中都有涉及到重量重心计算的功能,这个功能得到的重心数据主要用于托盘式造船时方便根据重心设置吊装配件. ...
- 【CDN+】 Spark入门---Handoop 中的MapReduce计算模型
前言 项目中运用了Spark进行Kafka集群下面的数据消费,本文作为一个Spark入门文章/笔记,介绍下Spark基本概念以及MapReduce模型 Spark的基本概念: 官网: http://s ...
随机推荐
- hdu1210Eddy's 洗牌问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1210 Eddy是个ACMer,他不仅喜欢做ACM题,而且对于纸牌也有一定的研究,他在无聊时研究发现,如 ...
- rabbitmq路由规则
信道如何共用???? 几台机器共用一个channel 如何做到?
- 7.使用dom4j实现增删改查
1.导入dim4j提供的jar包 (1)dom4j,是一个组织,针对xml解析,提供解析器dom4j (2)dom4j不是javase的一部分(jaxp是的) (3)使用dom4j步骤 - 下载并导入 ...
- 在项目中使用 Maven 私服
#在项目中使用 Maven 私服 在 Maven settings.xml 中添加 Nexus 认证信息(servers 节点下): <server> <id>nexus-re ...
- .NET Core 构建跨平台的桌面应用
1.运行环境 开发工具:Visual Studio 2017 JDK版本:.NET Core 2.0 项目管理工具:nuget 2.GITHUB地址 https://github.com/nbfujx ...
- win 10配置安装iis
站长喜欢本地配置iss调试网站后发布到网上,但是前提是系统得配置好iis.随着Win10的出现,越来越多的人装上了Win10, 但是小编最近发现很多旧版本windows系统用户在升级到windows ...
- Android中微信抢红包插件原理解析和开发实现
一.前言 自从去年中微信添加抢红包的功能,微信的电商之旅算是正式开始正式火爆起来.但是作为Android开发者来说,我们在抢红包的同时意识到了很多问题,就是手动去抢红包的速度慢了,当然这些有很多原因导 ...
- BZOJ 4484: [Jsoi2015]最小表示(拓扑排序+bitset)
传送门 解题思路 \(bitset\)维护连通性,给每个点开个\(bitset\),第\(i\)位为\(1\)则表示与第\(i\)位联通.算答案时显然要枚举每条边,而枚举边的顺序需要贪心,一个点先到达 ...
- [20190727NOIP模拟测试9]单(single) 题解(树上dp)
啊啊啊啊啊啊啊啊考场上差一点就A掉了5555 千里之堤溃于蚁穴……鬼知道最后一步那么显然的柿子我为什么没考虑用上…… 观察数据范围可知,出题人期望我们想出一个$O(n)$的做法 当然也有可能是$O(n ...
- APIO2010 特别行动队 & 斜率优化DP算法笔记
做完此题之后 自己应该算是真正理解了斜率优化DP 根据状态转移方程$f[i]=max(f[j]+ax^2+bx+c),x=sum[i]-sum[j]$ 可以变形为 $f[i]=max((a*sum[j ...