博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TensorFlow不同交叉熵计算方式
阅读量:4187 次
发布时间:2019-05-26

本文共 1001 字,大约阅读时间需要 3 分钟。

import tensorflow as tf  #our NN's output  logits=tf.constant([[1.0,3.0,2.0],[3.0,2.0,1.0],[1.0,2.0,3.0]])  #step1:do softmax  y=tf.nn.softmax(logits)  #true label  y_=tf.constant([[0.0,1.0,0.0],[1.0,0.0,0.0],[0.0,0.0,1.0]])  #step2:do cross_entropy  cross_entropy = -tf.reduce_sum(y_*tf.log(y))  #do cross_entropy just one step  cross_entropy2 = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits = logits,labels=y_))#dont forget tf.reduce_sum()!!  lab = tf.constant([1,0,2])cross_entropy3 = tf.reduce_sum(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,labels=lab))with tf.Session() as sess:      softmax=sess.run(y)      c_e = sess.run(cross_entropy)      c_e2 = sess.run(cross_entropy2)      print("step1:softmax result=")      print(softmax)      print("step2:cross_entropy result=")      print(c_e)      print("Function(softmax_cross_entropy_with_logits) result=")      print(c_e2)      c_e3 = sess.run(cross_entropy3)    print("using sparse cross_entropy")    print(c_e3)

转载地址:http://tldoi.baihongyu.com/

你可能感兴趣的文章
CareerCup Divide n cakes to k different people
查看>>
CareerCup Randomly return a node in a binary tree
查看>>
CareerCup Given a sorted array which contains scores. Write a program to find occurrence
查看>>
CareerCup The number of pairs (x, y) of distinct elements with condition x + y <= Threshold
查看>>
Build a key-value data structure which can perform lookup and rangeLookup(key1, key2)
查看>>
整数划分问题---动态规划、递归
查看>>
Balanced Partition
查看>>
Number of 1s
查看>>
CareerCup Find all the conflicting appointments from a given list of n appointments.
查看>>
CareerCup Given an array having positive integers, find a subarray which adds to a given number
查看>>
CareerCup Generate all the possible substrings
查看>>
CareerCup Given an array A[], find (i, j) such that A[i] < A[j] and (j - i) is maximum.
查看>>
Brain Teaser 球变色
查看>>
(2)考试大纲---信息系统项目管理师考试系列
查看>>
(3)教材目录---信息系统项目管理师考试系列
查看>>
商城基础E-R模型图
查看>>
飞翔的小鸟--键盘事件案例
查看>>
一个sql函数group_concat详解
查看>>
根据地址返回坐标位置的百度地图api
查看>>
thinkcmf数据字典
查看>>