0x00 前言
找到一本比较好的书,这里做一个简单的记录,用来解决已知a,b,c 根据一个未知的权重,可以得到d,那么如何通过深度学习解决这个问题就是本次探讨的话题
0x01 正文
首先来看代码,内容全部都写在注解里了。
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(6000):
result = sess.run([train, x1, x2, x3, w1, w2, w3, y, yTrain, loss], feed_dict={
x1: 92, x2: 98, x3: 90, yTrain: 94})
print(result)
result = sess.run([train, x1, x2, x3, w1, w2, w3, y, yTrain, loss], feed_dict={
x1: 92, x2: 99, x3: 98, yTrain: 96})
print(result)
运行结果: