第一天
2019-09-26 本文已影响0人
佛心看世界
#!/usr/bin/env python3.7
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
x_data = np.float32(np.random.rand(2,100))
y_data = np.dot([0.100,0.200],x_data)+0.300
# print(y_data)
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random.uniform([1,2],-1.0,1.0))
y = tf.matmul(W,x_data) + b
loss = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.compat.v1.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
init = tf.compat.v1.global_variables_initializer()
sess = tf.compat.v1.Session()
sess.run(init)
for step in range(0, 201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(W), sess.run(b))
结果
