首页 » 技术分享 » TensorFlow学习笔记(四)自己动手求Weights和biases

TensorFlow学习笔记(四)自己动手求Weights和biases

 

这里自己构造一些数据,求Weights和biases

源码:

import tensorflow as tf
import numpy as np

# create data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3

### create tensorflow structure start ###
Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
biases = tf.Variable(tf.zeros([1]))

y = Weights*x_data + biases

loss = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

init = tf.initialize_all_variables()
### create tensorflow structure end ###

sess = tf.Session()
sess.run(init)          # Very important

for step in range(201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(Weights), sess.run(biases))

经过201次迭代,得到结果:

0 [-0.24914408] [ 0.66447592]
20 [-0.00150566] [ 0.35365885]
40 [ 0.0756695] [ 0.31286183]
60 [ 0.09416807] [ 0.30308294]
80 [ 0.09860212] [ 0.30073899]
100 [ 0.09966495] [ 0.30017713]
120 [ 0.09991969] [ 0.30004248]
140 [ 0.09998076] [ 0.30001017]
160 [ 0.09999537] [ 0.30000246]
180 [ 0.0999989] [ 0.30000061]
200 [ 0.09999973] [ 0.30000016]

转载自原文链接, 如需删除请联系管理员。

原文链接:TensorFlow学习笔记(四)自己动手求Weights和biases,转载请注明来源!

0