损失函数
预测值(y)与已知答案(y_)的差距。
神经网络的优化目标就是找到某套参数使得计算出来的结果y与已知标准答案y_无限接近。也就是他们的差距loss值无限小。
主流loss有三种计算方法:
N
N
优化目标:
l
o
s
s
最小
→
{
均方误差
m
s
e
(
M
e
a
n
S
q
u
a
r
d
E
r
r
o
r
)
自定义
交叉熵
c
e
(
C
r
o
s
s
E
n
t
r
o
p
y
)
NN优化目标:loss最小→ \begin{cases} 均方误差mse(Mean Squard Error) \\自定义 \\ 交叉熵ce(Cross Entropy)\end{cases}
NN优化目标:loss最小→⎩
⎨
⎧均方误差mse(MeanSquardError)自定义交叉熵ce(CrossEntropy)
均方误差
m
s
e
:
M
S
E
(
y
−
,
y
)
=
∑
i
=
1
n
(
y
−
y
−
)
2
n
mse:MSE(y_-,y)=\frac{\sum^{n}_{i=1}(y-y_-)^2}{n}
mse:MSE(y−,y)=n∑i=1n(y−y−)2
loss_mse=tf.reduce_mean(tf.square(y_-y))
预测酸奶日销量y,x1、x2是影响日销量的因素。
建模前,一个预先采集的数据有:每日x1、x2和销量y_(即已知答案,最佳情况:产量=销量),拟造数据集X,Y;y_=x1,x2 噪声:-0.02~+0.05 拟合预测销售量。
import tensorflow as tf
import numpy as np
SEED = 23455
rdm = np.random.RandomState(seed=SEED) # 生成[0,1)之间的随机数
x = rdm.rand(32, 2)
y_ = [[x1 + x2 + (rdm.rand() / 10.0 - 0.05)] for (x1, x2) in x] # 生成噪声[0,1)/10=[0,0.1); [0,0.1)-0.05=[-0.05,0.05)
x = tf.cast(x, dtype=tf.float32)
w1 = tf.Variable(tf.random.normal([2, 1], stddev=1, seed=1))#随机初始化参数w1,为两行一列
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)#偏导
w1.assign_sub(lr * grads)
if epoch % 500 == 0:
print("%d次训练之后,参数w1是:" % (epoch))
print(w1.numpy(), "\n")
print("最后的参数w1是: ", w1.numpy())
0次训练之后,参数w1是:
[[-0.8096241]
[ 1.4855157]]
500次训练之后,参数w1是:
[[-0.21934733]
[ 1.6984866 ]]
1000次训练之后,参数w1是:
[[0.0893971]
[1.673225 ]]
1500次训练之后,参数w1是:
[[0.28368822]
[1.5853055 ]]
2000次训练之后,参数w1是:
[[0.423243 ]
[1.4906037]]
2500次训练之后,参数w1是:
[[0.531055 ]
[1.4053345]]
3000次训练之后,参数w1是:
[[0.61725086]
[1.332841 ]]
3500次训练之后,参数w1是:
[[0.687201 ]
[1.2725208]]
4000次训练之后,参数w1是:
[[0.7443262]
[1.2227542]]
4500次训练之后,参数w1是:
[[0.7910986]
[1.1818361]]
5000次训练之后,参数w1是:
[[0.82943517]
[1.1482395 ]]
5500次训练之后,参数w1是:
[[0.860872 ]
[1.1206709]]
6000次训练之后,参数w1是:
[[0.88665503]
[1.098054 ]]
6500次训练之后,参数w1是:
[[0.90780276]
[1.0795006 ]]
7000次训练之后,参数w1是:
[[0.92514884]
[1.0642821 ]]
7500次训练之后,参数w1是:
[[0.93937725]
[1.0517985 ]]
8000次训练之后,参数w1是:
[[0.951048]
[1.041559]]
8500次训练之后,参数w1是:
[[0.96062106]
[1.0331597 ]]
9000次训练之后,参数w1是:
[[0.9684733]
[1.0262702]]
9500次训练之后,参数w1是:
[[0.97491425]
[1.0206193 ]]
10000次训练之后,参数w1是:
[[0.9801975]
[1.0159837]]
10500次训练之后,参数w1是:
[[0.9845312]
[1.0121814]]
11000次训练之后,参数w1是:
[[0.9880858]
[1.0090628]]
11500次训练之后,参数w1是:
[[0.99100184]
[1.0065047 ]]
12000次训练之后,参数w1是:
[[0.9933934]
[1.0044063]]
12500次训练之后,参数w1是:
[[0.9953551]
[1.0026854]]
13000次训练之后,参数w1是:
[[0.99696386]
[1.0012728 ]]
13500次训练之后,参数w1是:
[[0.9982835]
[1.0001147]]
14000次训练之后,参数w1是:
[[0.9993659]
[0.999166 ]]
14500次训练之后,参数w1是:
[[1.0002553 ]
[0.99838644]]
最后的参数w1是: [[1.0009792]
[0.9977485]]