针对类别的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 ...
随机推荐
- sed使用---转义字符
https://blog.csdn.net/wangcg123/article/details/50667883 单引号里面,s表示替换,三根斜线中间是替换的样式,特殊字符需要使用反斜线”\”进行转义 ...
- lik模糊e查询语句,索引使用效果详解
一.like查询与索引 在oracle里的一个超级大的表中,我们的where条件的列有建索引的话,会走索引唯一扫描INDEX UNIQUE SCAN.如select * from table wher ...
- OkHttp源码剥离导入到eclipse中
1.里面有两个类关于Android版本的我稍微修改过了,没有用的. 2.可以直接导入eclipse中,maven里面的jar包可能还有不需要的冗余,可以自己去剔除. https://github.co ...
- bzoj4007 & loj2111 [JLOI2015]战争调度 复杂度分析+树上背包
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4007 https://loj.ac/problem/2111 题解 同 [NOI2006]网络 ...
- vue+element-ui 实现分页(根据el-table内容变换的分页)
官方例子 官方提示: 设置layout,表示需要显示的内容,用逗号分隔,布局元素会依次显示.prev表示上一页,next为下一页,pager表示页码列表,除此以外还提供了jumper和total,si ...
- 【leetcode】1032. Stream of Characters
题目如下: Implement the StreamChecker class as follows: StreamChecker(words): Constructor, init the data ...
- list去重方法
list去重 方法1:利用循环去重 s= [1,1,2,2,2,3,4,4] res= [] for i in s: if i not in res: res.append(i) print(res) ...
- 英语单词retrieve
retrieve 来源——报错信息 [root@centos65 ~]# yum whatprovides */lsb_release Loaded plugins: fastestmirror, s ...
- Angular JS - 7 - Angular JS 常用指令2
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举
HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...