针对类别的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 ...
随机推荐
- How To Find Out Attachments By File Type In Outlook?
ext: (extension extension) Take the attachments of zip files and of txt files for example, just ente ...
- 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom(Tarjan)
一道tarjan的模板水题 在这里还是着重解释一下tarjan的代码 #include<iostream> #include<cstdio> #include<algor ...
- rabbitmq 客户端崩溃退出
1.创建1个队列 和 创建另1个独占队列 名称相同 即崩溃退出 2..rabbitmq是为了实现实时消息推送的吗?
- Java使用文件通道复制文件
两种文件通道复制文件方式的性能比较 import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IO ...
- springboot redis操作
redis五大类型用法 Redis五大类型:字符串(String).哈希/散列/字典(Hash).列表(List).集合(Set).有序集合(sorted set)五种Controller:@Reso ...
- springboot支持webSocket和stomp实现消息订阅通知示例
先导入支持websocket的jar包,这里用Gradle构建的项目: dependencies { compile('org.springframework.boot:spring-boot-sta ...
- vue-葵花宝典
router-view 中包含 router-view 这种情况就可以 使用嵌套路由了 变化的视图中包含变化的视图 代码层面 router-view 中 包含router-view 路由childre ...
- json序列化反序列
json只能处理简单的数据类型:字典 列表等... 文件只能存字符串和二进制 序列化:把内存的对象变为字符串 反序列化:将字符串变回为内存对象
- javaScript的关键字与保留字
JavaScript 关键字: break case catch continue default delete do else finally for function if in instance ...
- SQLServer 链接服务器及同义词
链接服务器 1. openrowse exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc ...