第三章 初识TensorFlow

如果已知两位学生的成绩

90 * w1 + 80 * w2 + 70 * w3 = 85

98 * w1 + 95 * w2 + 87 * w3 = 96

两个方程式解三个未知数依然无法求解

  • 代码:

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(5000):

                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)

  • 代码说明:

神经网络在投入使用前,都要经过训练(train)的过程才能有准确的输出

w1、w2、w3已经非常接近于预期的0.6、0.3、0.1,y也非常接近于目标值

  • 运行结果:

1、安装完成

2、调试完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值