对于身份证问题,我们在上一篇成功地设计出了合适的模型并用代码实现,但是在模型训练过程中,我们发现训练时间很长,误差减少很慢。针对训练时间长和误差减少很慢,我们通常行之有效的方法主要有3种:
我们可以把隐藏层1的节点数增加到32个,只需要把w1定义成形状为[4,32]的变量,同时把w2定义为形态为[32,2]的变量,具体代码如下:
# Author:北京
# QQ:838262020
# time:2021/4/21
import tensorflow as tf
import random
random.seed()
x = tf.placeholder(tf.float32)
yTrain = tf.placeholder(tf.float32)
# w1 = tf.Variable(tf.random_normal([4, 8], mean=0.5, stddev=0.1), dtype=tf.float32)
w1 = tf.Variable(tf.random_normal([4, 32], mean=0.5, stddev=0.1), dtype=tf.float32)
b1 = tf.Variable(0, dtype=tf.float32)
xr = tf.reshape(x, [1, 4])
n1 = tf.nn.tanh(tf.matmul(xr, w1) + b1)
# w2 = tf.Variable(tf.random_normal([8, 2], mean=0.5, stddev=0.1), dtype=tf.float32)
w2 = tf.Variable(tf.random_normal([32, 2], mean=0.5, stddev=0.1), dtype=tf.float32)
b2 = tf.Variable(0, dtype=tf.float32)
n2 = tf.matmul(n1, w2) + b2
y = tf.nn.softmax(tf.reshape(n2, [2]))
loss = tf.reduce_mean(tf.square(y - yTrain))
optimizer = tf.train.RMSPropOptimizer(0.01)
train = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
lossSum = 0.0
for i in range(50000):
# 随机产生[0,9]的4位整数,模拟身份证后4位
xDataRandom = [int(random.random() * 10), int(random.random() * 10), int(random.random() * 10),
int(random.random() * 10)]
# 判断倒数第2位数字奇数或者偶数来模型对应的性别男女
if xDataRandom[2] % 2 == 0:
yTrainDataRandom = [0, 1]
else:
yTrainDataRandom = [1, 0]
result = sess.run([train, x, yTrain, y, loss], feed_dict={
x: xDataRandom, yTrain: yTrainDataRandom})
lossSum = lossSum + float(result[len(result) - 1])
print("i:%d,loss:%10.10f,avgLoss:%10.10f" % (i, float(result[len(result) - 1]), lossSum / (i + 1)))
一般来说,增加隐藏层的层数比增加某个隐藏层中的神经节点的数量效果会好一点,但是每一层上的激活函数的选择需要动一些脑筋。具体代码如下:
# Author:北京
# QQ:838262020
# time:2021/4/21
import tensorflow as tf
import random
random.seed()
x = tf.placeholder(tf.float32)
yTrain = tf.placeholder(tf.float32)
# w1 = tf.Variable(tf.random_normal([4, 8], mean=0.5, stddev=0.1), dtype=tf.float32)
w1 = tf.Variable(tf.random_normal([4, 32], mean=0.5, stddev=0.1), dtype=tf.float32)
b1 = tf.Variable(0, dtype=tf.float32)
xr = tf.reshape(x, [1, 4])
n1 = tf.nn.tanh(tf.matmul(xr, w1) + b1)
# w2 = tf.Variable(tf.random_normal([8, 2], mean=0.5, stddev=0.1), dtype=tf.float32)
w2 = tf.Variable(tf.random_normal([32, 2], mean=0.5, stddev=0.1), dtype=tf.float32)
b2 = tf.Variable(0, dtype=tf.float32)
n2 = tf.matmul(n1, w2) + b2
w3 = tf.Variable(tf.random_normal([16, 2], mean=0.5, stddev=0.1), dtype=tf.float32)
b3 = tf.Variable(0, dtype=tf.float32)
n3 = tf.matmul(n2, w3) + b3
y = tf.nn.softmax(tf.reshape(n3, [2]))
loss = tf.reduce_mean(tf.square(y - yTrain))
optimizer = tf.train.RMSPropOptimizer(0.01)
train = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
lossSum = 0.0
for i in range(50000):
# 随机产生[0,9]的4位整数,模拟身份证后4位
xDataRandom = [int(random.random() * 10), int(random.random() * 10), int(random.random() * 10),
int(random.random() * 10)]
# 判断倒数第2位数字奇数或者偶数来模型对应的性别男女
if xDataRandom[2] % 2 == 0:
yTrainDataRandom = [0, 1]
else:
yTrainDataRandom = [1, 0]
result = sess.run([train, x, yTrain, y, loss], feed_dict={
x: xDataRandom, yTrain: yTrainDataRandom})
lossSum = lossSum + float(result[len(result) - 1])
print("i:%d,loss:%10.10f,avgLoss:%10.10f" % (i, float(result[len(result) - 1]), lossSum / (i + 1)))
有时候学习率设置不合适也会导致训练模型效率不高,可以适当修改学习率的数值。