欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TensorFlow拟合一条直线

程序员文章站 2022-05-23 12:25:13
...

最近学习TensorFlow,从最基本的做起,这是一个简单的拟合一条直线的例子,并且训练201次,每20步输出一次,并输出图像:

开始训练:


TensorFlow拟合一条直线

训练100步后:

TensorFlow拟合一条直线


以下是完整代码:

#coding: utf-8
#author: 吴晶
#wechat: 18007148050



import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# a = tf.constant(2)
# b = tf.constant(3)
#
# with tf.Session() as sess:
#     print(sess.run(a)+sess.run(b))

# state = tf.Variable(0,name='counter')
# one = tf.constant(1)
# new_value = tf.add(state,one)
# update = tf.assign(state,new_value)
# init = tf.global_variables_initializer()
# with tf.Session() as sess:
#     sess.run(init)
#     for _ in range(3):
#         sess.run(update)
#         print(sess.run(state))

# input1 = tf.placeholder(tf.float32)
# input2 = tf.placeholder(tf.float32)
#
# output = tf.multiply(input1,input2)
#
# with tf.Session() as sess:
#     print(sess.run(output,feed_dict={input1:[7.],input2:[2.]}

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

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.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.scatter(x_data,y_data)
    plt.ion()
    plt.show()
    for step in range(201):
        sess.run(train)
        prediction = sess.run(Weights)*x_data + sess.run(biases)
        lines = ax.plot(x_data,prediction,'r-',lw=3)
        plt.pause(0.1)
        try:
            ax.lines.remove(lines[0])
        except Exception:
            pass

        if step % 20 == 0:
            print(step,sess.run(Weights),sess.run(biases))




相关标签: python tensorflow