# coding:utf-8
# 预测多或者预测少的影响一样
# 导入模块,生成数据集
import tensorflow as tf
import numpy as np
# 一次喂入神经网络8组数据,数值不可以过大
BATCH_SIZE = 8
SEED = 23455
# 基于seed产生随机数
rdm = np.random.RandomState(SEED)
# 随机数返回32行2列的矩阵 表示32组 体积和重量 作为输入数据集
X = rdm.rand(32, 2)
Y_ = [[x1+x2+(rdm.rand()/10.0-0.05)] for (x1, x2) in X]
print("X:\n", X)
print("Y:\n", Y_)
# 定义神经网络的输入,参数和输出,定义前向传播过程
x = tf.placeholder(tf.float32, shape=(None, 2))
y_ = tf.placeholder(tf.float32, shape=(None, 1))
# w1为2行1列
w1 = tf.Variable(tf.random_normal([2, 1], stddev=1, seed=1))
y = tf.matmul(x, w1)
# 定义损失函数及反向传播方法
# 定义损失函数为MSE,反向传播方法为梯度下降
loss_mse = tf.reduce_mean(tf.square(y_-y))
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss_mse)
# 其他优化方法
# train_step = tf.train.GMomentumOptimizer(0.001, 0.9).minimize(loss_mse)
# train_step = tf.train.AdamOptimizer(0.001).minimize(loss_mse)
# 生成会话,训练STEPS轮
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
# 训练模型20000轮
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]})
# 没500轮打印一次loss值
if i % 1000 == 0:
total_loss = sess.run(loss_mse, feed_dict={x: X, y_: Y_})
print("After %d training step(s), loss on all data is %g" %(i, total_loss))
print(sess.run(w1),"\n")
print("Final w1 is: \n", sess.run(w1))
有上述代码可知,本例中神经网络预测模型为y = w1x1 + w2x2,损失函数采用均方误差。通过使损失函数值(loss)不断降低,神经网络模型得到最终参数w1 = 0.98,w2 = 1.02,销量预测结果为y = 0.98x1 + 1.02x2。由于在生成数据集时,标准答案为y = x1 + x2,因此,销量预测结果和标准答案已经非常接近,说明该神经网络预测酸奶日销量正确
# 第一种情况:酸奶成本1元,酸奶利润9元
# 预测少了损失大,故不要预测少,故生成的模型会多预测一些
# 导入模块,生成数据集
import tensorflow as tf
import numpy as np
# 一次喂入神经网络8组数据,数值不可以过大
BATCH_SIZE = 8
SEED = 23455
COST = 1
PROFIT = 9
# 基于seed产生随机数
rdm = np.random.RandomState(SEED)
# 随机数返回32行2列的矩阵 表示32组 体积和重量 作为输入数据集
X = rdm.rand(32, 2)
Y_ = [[x1+x2+(rdm.rand()/10.0-0.05)] for (x1, x2) in X]
# 定义神经网络的输入,参数和输出,定义前向传播过程
x = tf.placeholder(tf.float32, shape=(None, 2))
y_ = tf.placeholder(tf.float32, shape=(None, 1))
# w1为2行1列
w1 = tf.Variable(tf.random_normal([2, 1], stddev=1, seed=1))
y = tf.matmul(x, w1)
# 定义损失函数及反向传播方法
# 定义损失函数使得预测少了的损失大,于是模型应该偏向多的放心预测
loss = tf.reduce_sum(tf.where(tf.greater(y, y_), COST*(y-y_), PROFIT*(y_-y)))
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
# 其他优化方法
# train_step = tf.train.GMomentumOptimizer(0.001, 0.9).minimize(loss)
# train_step = tf.train.AdamOptimizer(0.001).minimize(loss)
# 生成会话,训练STEPS轮
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
# 训练模型20000轮
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]})
# 没500轮打印一次loss值
if i % 1000 == 0:
total_loss = sess.run(loss, feed_dict={x: X, y_: Y_})
print("After %d training step(s), loss on all data is %g" %(i, total_loss))
print(sess.run(w1),"\n")
print("Final w1 is: \n", sess.run(w1))
由代码执行结果可知,神经网络最终参数为w1=1.03,w2=1.05,销量预测结果为y = 1.03x1 + 1.05x2。由此可见,采用自定义损失函数预测的结果大于采用均方误差的结果,更符合实际需求
# 第二种情况:酸奶成本9元,酸奶利润1元
# 预测多了损失大,故不要预测多,故生成的模型会少预测一些
# 导入模块,生成数据集
import tensorflow as tf
import numpy as np
# 一次喂入神经网络8组数据,数值不可以过大
BATCH_SIZE = 8
SEED = 23455
COST = 9
PROFIT = 1
# 基于seed产生随机数
rdm = np.random.RandomState(SEED)
# 随机数返回32行2列的矩阵 表示32组 体积和重量 作为输入数据集
X = rdm.rand(32, 2)
Y_ = [[x1+x2+(rdm.rand()/10.0-0.05)] for (x1, x2) in X]
# 定义神经网络的输入,参数和输出,定义前向传播过程
x = tf.placeholder(tf.float32, shape=(None, 2))
y_ = tf.placeholder(tf.float32, shape=(None, 1))
# w1为2行1列
w1 = tf.Variable(tf.random_normal([2, 1], stddev=1, seed=1))
y = tf.matmul(x, w1)
# 定义损失函数及反向传播方法
# 重新定义损失函数使得预测多了的损失大,于是模型应该偏向少的方向预测
loss = tf.reduce_sum(tf.where(tf.greater(y, y_), COST*(y-y_), PROFIT*(y_-y)))
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
# 其他优化方法
# train_step = tf.train.GMomentumOptimizer(0.001, 0.9).minimize(loss)
# train_step = tf.train.AdamOptimizer(0.001).minimize(loss)
# 生成会话,训练STEPS轮
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
# 训练模型20000轮
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]})
# 没500轮打印一次loss值
if i % 1000 == 0:
total_loss = sess.run(loss, feed_dict={x: X, y_: Y_})
print("After %d training step(s), loss on all data is %g" %(i, total_loss))
print(sess.run(w1),"\n")
print("Final w1 is: \n", sess.run(w1))
由执行结果可知,神经网络最终参数为w1 = 0.96,w2 = 0.97,销量预测结果为y = 0.96+x1 + 0.7*x2。
因此,采用自定义损失函数预测的结果小于采用均方误差预测得结果,更符合实际需求
ce = -tf.reduce_mean(y_*tf.clip_by_value(y, le-12, 1.0)))
H(1, 0), (0.6, 0.4) = -(1 * log0.6 + 0log0.4) ≈ -(-0.222 + 0) = 0.222
H(1, 0), (0.8, 0.2) = -(1 * log0.8 + 0log0.2) ≈ -(-0.097 + 0) = 0.097
ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = y, labels = tf.argmax(y_, 1))
cem = tf.reduce_mean(ce)