2.4损失函数
损失函数(loss):预测值(y)与已知答案(y_)的差距
nn优化目标:loss最小->-mse
-自定义
-ce(cross entropy)
均方误差mse:MSE(y_,y)=E^n~i=1(y-y_)^2/n
loss_mse = tf.reduce_mean(tf.square(y_-y))
import tensorflow as tf
import numpy as np SEED = 23455 rdm = np.random.RandomState(seed=SEED)
x = rdm.rand(32,2)
y_ = [[x1 + x2 + (rdm.rand()/10.0 - 0.05)] for (x1,x2) in x] # 生成【0,1】/10-0.05的噪声
x = tf.cast(x,dtype = tf.float32) w1 = tf.Variable(tf.random.normal([2,1],stddev=1, seed = 1)) # 创建一个2行一列的参数矩阵 epoch = 15000
lr = 0.002 for epoch in range(epoch):
with tf.GradientTape() as tape:
y = tf.matmul(x,w1)
loss_mse = tf.reduce_mean(tf.square(y_-y)) grads = tape.gradient(loss_mse,w1) # loss_mse对w1求导
w1.assign_sub(lr*grads) # 在原本w1上减去lr(学习率)*求导结果 if epoch % 500 == 0:
print('After %d training steps,w1 is'%(epoch))
print(w1.numpy(),"\n")
print("Final w1 is:",w1.numpy())

结果:

After 0 training steps,w1 is
[[-0.8096241]
[ 1.4855157]]

After 500 training steps,w1 is
[[-0.21934733]
[ 1.6984866 ]]

After 1000 training steps,w1 is
[[0.0893971]
[1.673225 ]]

After 1500 training steps,w1 is
[[0.28368822]
[1.5853055 ]]

........

........

After 14000 training steps,w1 is
[[0.9993659]
[0.999166 ]]

After 14500 training steps,w1 is
[[1.0002553 ]
[0.99838644]]

Final w1 is: [[1.0009792]
[0.9977485]]

自定义损失函数
如预测商品销量,预测多了,损失成本,预测少了损失利润
若利润!=成本,则mse产生的loss无法利益最大化
自定义损失函数 loss(y_-y)=Ef(y_-y) f(y_-y)={profit*(y_-y) ,y<y_ 预测少了,损失利润
{cost*(y_-y) ,y>y_ 预测多了,损失成本 写出函数:
loss_zdy = tf.reduce_sum(tf.where(tf.greater(y_,y),(profit*(y_-y),cost*(y-y_) ))) 假设商品成本1元,利润99元,则预测后的参数偏大,预测销量较高,反之成本为99利润为1则参数小,销售预测较小
import tensorflow as tf
import numpy as np profit = 1
cost = 99
SEED = 23455
rdm = np.random.RandomState(seed=SEED)
x = rdm.rand(32,2)
x = tf.cast(x,tf.float32)
y_ = [[x1+x2 + rdm.rand()/10.0-0.05] for x1,x2 in x]
w1 = tf.Variable(tf.random.normal([2,1],stddev=1,seed=1)) epoch = 10000
lr = 0.002 for epoch in range(epoch):
with tf.GradientTape() as tape:
y = tf.matmul(x,w1)
loss_zdy = tf.reduce_sum(tf.where(tf.greater(y_,y),(y_-y)*profit,(y-y_)*cost)) grads = tape.gradient(loss_zdy,w1)
w1.assign_sub(lr*grads) if epoch % 500 == 0:
print("after %d epoch w1 is:"%epoch)
print(w1.numpy(),'\n')
print('--------------')
print('final w1 is',w1.numpy()) # 当成本=1,利润=99模型的两个参数[[1.1231122][1.0713713]] 均大于1模型在往销量多的预测
# 当成本=99,利润=1模型的两个参数[[0.95219666][0.909771 ]] 均小于1模型在往销量少的预测
交叉熵损失函数CE(cross entropy),表示两个概率分布之间的距离
H(y_,y)= -Ey_*lny
如:二分类中标准答案y_=(1,0),预测y1=(0.6,0.4),y2=(0.8,0.2)
哪个更接近标准答案?
H1((1,0),(0.6,0.4))=-(1*ln0.6 + 0*ln0.4) =0.511
H2((1,0),(0.8,0.2))=0.223
因为h1>H2,所以y2预测更准
tf中交叉熵的计算公式:
tf.losses.categorical_crossentropy(y_,y)
import tensorflow as tf
loss_ce1 = tf.losses.categorical_crossentropy([1,0],[0.6,0.4])
loss_ce2 = tf.losses.categorical_crossentropy([1,0],[0.8,0.2])
print("loss_ce1",loss_ce1)
print("loss_ce2",loss_ce2)
#loss_ce1 tf.Tensor(0.5108256, shape=(), dtype=float32)
#loss_ce2 tf.Tensor(0.22314353, shape=(), dtype=float32)
# 结果loss_ce2数值更小更接近
softmax与交叉熵结合
输出先过softmax,再计算y_和y的交叉损失函数
tf.nn.softmax_cross_entroy_with_logits(y_,y)

最新文章

  1. myql 查询树形表结果:说说、说说的述评、评论的回复
  2. eclipse的几个快捷键
  3. MongoDB与Mysql常用命令解释
  4. 判定元素正在插入到DOM树——DOMNodeInsertedIntoDocument
  5. Codeforces Gym 100463D Evil DFS
  6. Vi和Vim的区别及联系
  7. jQuery遍历对象、数组、集合实例
  8. sql server2008 搭建链接服务器成功后查询时报Cannot obtain the schema rowset &quot;DBSCHEMA_TABLES_INFO&quot; for OLE DB provider &quot;SQLNCLI10&quot; for linked server &quot;XXXXX&quot;. 的解决方法
  9. C# 面向对象 , 继承
  10. javascript获取页面文档内容
  11. IEEE浮点数float、double的存储结构
  12. JFinal开发8个常见问题
  13. 编译 MVC View
  14. js打印html指定元素,解决动态获取的图片无法打印问题
  15. PHP use闭包函数
  16. linux 下将tomcat注册成服务并开机启动
  17. 14-01 Java matches类,Pattern类,matcher类
  18. 详解tween.js 中文使用指南
  19. 20145320周岐浩 web安全基础实践
  20. jquery完成界面无刷新加载登陆注册

热门文章

  1. 复变函数-MINDMAPS-continuous updating
  2. vue中使用mixins
  3. 1.2Go环境搭建之Mac
  4. Asp.net MVC验证那些事(1)-- 介绍和验证规则使用----[转]--[并修改了部分内容]
  5. PAT-1064 Complete Binary Search Tree(完全二叉树)
  6. ngnix随笔一
  7. 如何在npm发布轮子
  8. [翔哥高手无敌之路]0-002.如何提取apk中的信息?
  9. Python数据分析:pandas玩转Excel (二)
  10. 解决google play上架App设置隐私政策声明问题