tensorflow
W3Cschool中tensorflow官方文档中tensorflow构建图的代码是tensorflow1.x的版本
本文给出tensorflow2.x的代码以供入门萌新学习
import tensorflow as tf
import numpy as np
tf.compat.v1.disable_eager_execution()
x_data = np.float32(np.random.rand(2, 100))
y_data = np.dot([0.100, 0.200], x_data) + 0.300
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.compat.v1.random_uniform([1, 2], -1.0, 1.0))
y = tf.matmul(W, x_data) + b
loss = tf.compat.v1.reduce_mean(tf.square(y - y_data))
optimizer = tf.compat.v1.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
init = tf.compat.v1.initialize_all_variables()
sess = tf.compat.v1.Session()
sess.run(init)
xrange = 201
for step in range(xrange):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(W), sess.run(b))