线性函数预测
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
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.1)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for step in range(1001):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(Weights), sess.run(biases), sess.run(loss))
Weights = sess.run(Weights)
biases = sess.run(biases)
y = sess.run(y)
plt.plot(x_data, y_data, "+")
plt.plot(x_data, y)
plt.show()
tf.stop_gradient()
gradients = optimizer.compute_gradients(loss)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步