标签:normal mini 表示 show 实现 ESS plot oss and
构造数据,实现简单的线性回归import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
#模拟数据
num_points = 1000
vec_points = []
for i in range(num_points):
x = np.random.normal(0.0,0.5)
y = x * 0.1 + 0.3 + np.random.normal(0.0, 0.06)
vec_points.append([x,y])
x_set = [i[0] for i in vec_points]
y_set = [i[1] for i in vec_points]
#plt.scatter(x_set, y_set, c=‘r‘)
#plt.show()
W = tf.Variable(tf.random_normal([1], -10.0, 0.0))#shape,mean,stddev
b = tf.Variable(tf.zeros([1]))
y = W * x_set + b
loss = tf.reduce_mean(tf.square(y - y_set))#用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,不指定表示所有值的平均值
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(50):
sess.run(train)
print("W=", sess.run(W), "b=", sess.run(b), "loss=", sess.run(loss))
标签:normal mini 表示 show 实现 ESS plot oss and
原文地址:https://blog.51cto.com/5669384/2415939