# ----------------------------
#! 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计算的更多相关文章

  1. (转载)人脸识别中Softmax-based Loss的演化史

    人脸识别中Softmax-based Loss的演化史  旷视科技 近期,人脸识别研究领域的主要进展之一集中在了 Softmax Loss 的改进之上:在本文中,旷视研究院(上海)(MEGVII Re ...

  2. C++ 类的实例中 内存分配详解

    一个类,有成员变量:静态与非静态之分:而成员函数有三种:静态的.非静态的.虚的. 那么这些个东西在内存中到底是如何分配的呢? 以一个例子来说明: #include"iostream.h&qu ...

  3. c++类模板template中的typename使用方法-超级棒

    转载:https://blog.csdn.net/vanturman/article/details/80269081 如有问题请联系我删除: 目录 起因 typename的常见用法 typename ...

  4. 怎样在caffe中添加layer以及caffe中triplet loss layer的实现

    关于triplet loss的原理.目标函数和梯度推导在上一篇博客中已经讲过了.详细见:triplet loss原理以及梯度推导.这篇博文主要是讲caffe下实现triplet loss.编程菜鸟.假 ...

  5. 浅谈人脸识别中的loss 损失函数

    浅谈人脸识别中的loss 损失函数 2019-04-17 17:57:33 liguiyuan112 阅读数 641更多 分类专栏: AI 人脸识别   版权声明:本文为博主原创文章,遵循CC 4.0 ...

  6. 第16/24周 SQL Server 2014中的基数计算

    大家好,欢迎回到性能调优培训.上个星期我们讨论在SQL Server里基数计算过程里的一些问题.今天我们继续详细谈下,SQL Server 2014里引入的新基数计算. 新基数计算 SQL Serve ...

  7. GLSL 中的光照计算

    理论知识转载地址:http://blog.csdn.net/ym19860303/article/details/25545933 1.Lambert模型(漫反射) 环境光: Iambdiff = K ...

  8. 管道设计CAD系统中重量重心计算

    管道设计CAD系统中重量重心计算 eryar@163.com Abstract. 管道设计CAD系统中都有涉及到重量重心计算的功能,这个功能得到的重心数据主要用于托盘式造船时方便根据重心设置吊装配件. ...

  9. 【CDN+】 Spark入门---Handoop 中的MapReduce计算模型

    前言 项目中运用了Spark进行Kafka集群下面的数据消费,本文作为一个Spark入门文章/笔记,介绍下Spark基本概念以及MapReduce模型 Spark的基本概念: 官网: http://s ...

随机推荐

  1. python常用函数 L

    lstrip(str) 删除字符串左边的字符,支持传入参数. 例子: ljust(int) 格式化字符串,左对齐,支持传入填充值. 例子: loads(json) 将json字符串转换为dict. 例 ...

  2. 从__name__=='__main__'说到__builtin__

    一.__name__ 我们在写好代码进行自测的时候一般会先写这样一行代码: # inter_method if __name__ == '__main__': 为什么呢,可能并不是所有人都考虑过,这个 ...

  3. java 字符串的截取、转换、分割

    1.截取 package java07; /* 字符串的截取方法: public String substring(int index):截取从参数位置一直到字符串末尾,返回新字符串 public S ...

  4. 【python实例】要求输出字符串中最少一个最多八个的所有字符串组合(连续)

    """ 题目:字符串str="ABCDEFGHIJK",要求输出最少一个最多八个的所有组合(向后连续字母) 输出如下: A [0::] AB ABC ...

  5. office2016pro专业版命令行激活

    1.首先查看Office2016安装目录在哪里,如果是默认安装,没有修改路径,是在C:\Program Files\Microsoft Office\Office16目录下,具体路径还得自行查看,我的 ...

  6. 太可怕了!黑客是如何攻击劫持安卓用户的DNS?

    最近发现的针对Android设备的广泛路由器的DNS劫持恶意软件现在已升级为针对iOS设备以及桌面用户的功能. 被称为RoamingMantis的恶意软件最初发现在上个月劫持了互联网路由器,以散布旨在 ...

  7. springBoot相关(一)

    2.0新特性: 编程语言: Java8+.Kotlin 底层框架:Spring Framwork 5.0.x 全新特性: web Flux web Flux: 函数编程:java 8 Lambda 响 ...

  8. The main Method

    The main Method You can call static methods without having any objects. For example, you never const ...

  9. .NET面试题集锦②

    一.前言部分 文中的问题及答案多收集整理自网络,不保证100%准确,还望斟酌采纳. 1.实现产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复. ]; ArrayList my ...

  10. mysql delete from left 多表条件删除

    DELETE n from news n LEFT JOIN news2 d ON n.id=d.id WHERE d.id IS null