搭建解决三好学生成绩问题的神经网络
3.1 score1a
程序:
import tensorflow as tf
#德、智、体数值输入
x1 = tf.placeholder(dtype=tf.float32)
x2 = tf.placeholder(dtype=tf.float32)
x3 = tf.placeholder(dtype=tf.float32)
#权重
w1 = tf.Variable(0.1, dtype=tf.float32)
w2 = tf.Variable(0.1, dtype=tf.float32)
w3 = tf.Variable(0.1, dtype=tf.float32)
#定义隐藏层节点
n1 = x1 * w1
n2 = x2 * w2
n3 = x3 * w3
#定义输出层
y = n1 + n2 + n3
sess = tf.Session()
#初始化所有可变参数
init = tf.global_variables_initializer()
sess.run(init)
#计算一次神经网络
result = sess.run([x1, x2, x3, w1, w2, w3, y], feed_dict={x1: 90, x2: 80, x3: 70})
print(result)
结果:
[array(90., dtype=float32), array(80., dtype=float32), array(70., dtype=float32), 0.1, 0.1, 0.1, 24.0]
训练神经网络
3.2 score1b
程序:
import tensorflow as tf
x1 = tf.placeholder(dtype=tf.float32)
x2 = tf.placeholder(dtype=tf.float32)
x3 = tf.placeholder(dtype=tf.float32)
yTrain = tf.placeholder(dtype=tf.float32)
w1 = tf.Variable(0.1, dtype=tf.float32)
w2 = tf.Variable(0.1, dtype=tf.float32)
w3 = tf.Variable(0.1, dtype=tf.float32)
n1 = x1 * w1
n2 = x2 * w2
n3 = x3 * w3
y = n1 + n2 + n3
loss = tf.abs(y - yTrain)
optimizer = tf.train.RMSPropOptimizer(0.001)
train = optimizer.minimize(loss)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
result = sess.run([train, x1, x2, x3, w1, w2, w3, y, yTrain, loss], feed_dict={x1: 90, x2: 80, x3: 70, yTrain: 85})
print(result)
result = sess.run([train, x1, x2, x3, w1, w2, w3, y, yTrain, loss], feed_dict={x1: 98, x2: 95, x3: 87, yTrain: 96})
print(result)
结果:
[None, array(90., dtype=float32), array(80., dtype=float32), array(70., dtype=float32), 0.1, 0.1, 0.1, 24.0, array(85., dtype=float32), 61.0]
[None, array(98., dtype=float32), array(95., dtype=float32), array(87., dtype=float32), 0.10316052, 0.10316006, 0.103159375, 28.884804, array(96., dtype=float32), 67.1152]
多轮训练
3.3 score1c
程序:
import tensorflow as tf
x1 = tf.placeholder(dtype=tf.float32)
x2 = tf.placeholder(dtype=tf.float32)
x3 = tf.placeholder(dtype=tf.float32)
yTrain = tf.placeholder(dtype=tf.float32)
w1 = tf.Variable(0.1, dtype=tf.float32)
w2 = tf.Variable(0.1, dtype=tf.float32)
w3 = tf.Variable(0.1, dtype=tf.float32)
n1 = x1 * w1
n2 = x2 * w2
n3 = x3 * w3
y = n1 + n2 + n3
loss = tf.abs(y - yTrain)
optimizer = tf.train.RMSPropOptimizer(0.001)
train = optimizer.minimize(loss)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(2):
result = sess.run([train, x1, x2, x3, w1, w2, w3, y, yTrain, loss], feed_dict={x1: 90, x2: 80, x3: 70, yTrain: 85})
print(result)
result = sess.run([train, x1, x2, x3, w1, w2, w3, y, yTrain, loss], feed_dict={x1: 98, x2: 95, x3: 87, yTrain: 96})
print(result)
结果:
[None, array(90., dtype=float32), array(80., dtype=float32), array(70., dtype=float32), 0.1, 0.1, 0.1, 24.0, array(85., dtype=float32), 61.0]
[None, array(98., dtype=float32), array(95., dtype=float32), array(87., dtype=float32), 0.10316052, 0.10316006, 0.103159375, 28.884804, array(96., dtype=float32), 67.1152]
[None, array(90., dtype=float32), array(80., dtype=float32), array(70., dtype=float32), 0.10554425, 0.10563005, 0.1056722, 25.346441, array(85., dtype=float32), 59.653557]
[None, array(98., dtype=float32), array(95., dtype=float32), array(87., dtype=float32), 0.10740828, 0.107431844, 0.107439496, 30.079273, array(96., dtype=float32), 65.92073]