tensorflow课堂笔记(三)

损失函数

"""
神经元模型 f(∑xiwi + b),其中b为偏置项bias,f为激活函数activation function
激活函数 activation function
tf.nn.relu()
tf.nn.sigmoid()
tf.nn.tanh()
NN的复杂度
层数 = 隐藏层 + 1个输出层
总参数 = 总w + 总b
损失函数loss:预测值y与已经答案y_的差距
优化目标是让loss尽量小
均方误差mse:MSE(y_, y)
loss_mse = tf.reduce_mean(tf.square(y_-y))
"""
#coding utf-8
import tensorflow as tf
import numpy as np
BATCH_SIZE = 8
seed = 23455
COST = 1    #成本1
PROFIT = 9  #利润9

rdm = np.random.RandomState(seed) #种下随机数种子
X = rdm.rand(32, 2)               #0到1的32行2列的随机数列表
Y_ = [[x1+x2+(rdm.rand()/10.0-0.05)] for [x1, x2] in X]  #rdm.rand()/10.0-0.05产生0.05到-0.05的随机数

#1定义神经网络的输入,参数和输出,定义前向传播
x = tf.placeholder(tf.float32, shape=[None, 2])
y_ = tf.placeholder(tf.float32, shape=[None, 1])

w1 = tf.Variable(tf.random_normal([2, 1], stddev=1, seed=1))

y = tf.matmul(x, w1)

#2定义损失函数和反向传播方法
#loss_mse = tf.reduce_mean(tf.square(y-y_))
loss_mse = tf.reduce_sum(tf.where(tf.greater(y,y_),(y-y_)*COST,(y_-y)*PROFIT))
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss_mse)

#3训练神经网络
with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)

    #进行STEPS轮训练
    STEPS = 20000
    for i in range(STEPS):
        start = (i % BATCH_SIZE) % 32
        end = start + BATCH_SIZE
        sess.run(train_step, feed_dict={x:X[start:end], y_:Y_[start:end]})
        if i % 500 == 0:
            print("After %d training step(s), loss in all data is " % i)
            print(sess.run(w1),"\n")

    #打印训练结果
    print("\n")
    print("Final w1 is:\n")
    print(sess.run(w1),"\n")

"""
自定义损失函数
假如y<y_ 商品少了,损失了利润
假如y>y_ 商品多了,损失了成本
loss = tf.reduce_sum(tf.where(tf.greater(y,y_),COST(y-y_),PROFIT(y_-y)))
上式类似于?:
运行结果:
Final w1 is:

[[1.0430964 ]
 [0.98464024]]

交叉熵ce(cross entropy):表征两个概率分布之间的距离,距离小的更接近答案
H = -∑y_*logy
ce = -tf.reduce_mean(y_*.log(tf.cpil_by_value(y,1e-12,1.0)))
y<e-12为e-12,大于1.0时为1.0

n分类sotfmax(),满足概率分布
ce = tf.nn.sparse_sotfmax_cross_entropy_with_logits(logits=y,labels=tf.argmax(y_,1))
cem = tf.reduce_mean(ce)
"""

猜你喜欢

转载自blog.csdn.net/haohulala/article/details/83651528
今日推荐