4

【TensorFlow4】损失函数

 3 years ago
source link: https://www.guofei.site/2018/10/01/tf4.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【TensorFlow4】损失函数

2018年10月01日

Author: Guofei

文章归类: 2-3-神经网络与TF ,文章编号: 284


版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2018/10/01/tf4.html

Edit

损失函数

1.交叉熵

H(p,q)=−∑p(x)logq(x)H(p,q)=−∑p(x)log⁡q(x)

cross_entropy=-tf.reduce_mean(y_*tf.log(tf.clip_by_value(y,1e-10,1.0)))
# y_代表正确结果,y代表预测结果
# tf.clip_by_value可以把张量中的数值限制在某个范围内,这里为了防止log0报错
# reduce_mean:大概是求均值?

TensorFlow对softmax+crossentropy进行了封装

# cross_entropy=tf.nn.softmax_cross_entropy_with_logits(y,y_)

#  输入的 shape 应当是 (number of examples, num_classes)
logits = tf.transpose(Z3) # 注意,这里不是 y_hat
labels = tf.transpose(Y)

# 输出的(num_classes,),你还要使用一次 tf.reduce_mean
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=labels))

2.MSE

MSE(y,y′)=∑(y−y′)2nMSE(y,y′)=∑(y−y′)2n

mse=tf.reduce_mean(tf.square(y_-y))

3.自定义

例如,预测销量时,多预测一个损失1元,少预测1个损失10元。
Loss(y,y′)=∑f(yi,y′i)Loss(y,y′)=∑f(yi,yi′),
其中,f(x,y)={a(x−y)b(y−x)x>yx≤yf(x,y)={a(x−y)x>yb(y−x)x≤y

loss=tf.reduce_sum(tf.select(tf.greater(v1,v2),(v1-v2)*a,(v2-v1)*b))

正则化项

el_param1=tf.constant(1,dtype=tf.float32)
el_param2=tf.constant(1,dtype=tf.float32)
l1_term=el_param1*tf.reduce_sum(tf.abs(A))
l2_term=el_param2*tf.reduce_sum(tf.square(A))

loss=tf.reduce_sum(tf.square(y-y_hat))+l1_term+l2_term
tf.reduce_sum(tf.square(y-y_hat))\
+tf.contrib.layers.l1_regularizer(1.0)(A)\
+tf.contrib.layers.l2_regularizer(1.0)(A)

参考文献

TF官网-tf.nn
《人工神经网络原理》马锐,机械工业出版社
白话深度学习与TensorFlow,高扬,机械工业出版社
《TensorFlow实战Google深度学习框架》,郑泽宇,中国工信出版集团


您的支持将鼓励我继续创作!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK