tensorflow笔记--回归

#tensorflow2代码
#设置优化器
x_data = np.random.rand(100).astype(np.float32)
b_data = tf.random.normal([1],mean=1,stddev=0.5)
print(tf.reduce_mean(b_data))
y_data = x_data * 0.3 + b_data
Weights = tf.Variable(tf.random.uniform([1],-1.0,1.0))
biases = tf.Variable(tf.zeros([1]))
optimizer = tf.keras.optimizers.Adam(learning_rate=0.01)
for i in range(10000):
    with tf.GradientTape() as tape:
        y  = x * Weights   + biases
        loss = tf.reduce_mean(tf.square(y - y_data))
        grads = tape.gradient(loss, [Weights, biases])
    optimizer.apply_gradients(zip(grads,[Weights, biases]))
    if i % 100 ==0:
        print('误差',np.float(loss),'\t','权重1',np.float(Weights),'\t','偏差',np.float(biases))

Dependencies:
tensorflow: 1.1.0
matplotlib
numpy
"""
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

tf.set_random_seed(1)
np.random.seed(1)

# fake data
x = np.linspace(-1, 1, 100)[:, np.newaxis]          # shape (100, 1)
noise = np.random.normal(0, 0.1, size=x.shape)
y = np.power(x, 2) + noise                          # shape (100, 1) + some noise

# plot data
plt.scatter(x, y)
plt.show()

tf_x = tf.placeholder(tf.float32, x.shape)     # input x
tf_y = tf.placeholder(tf.float32, y.shape)     # input y

# neural network layers
l1 = tf.layers.dense(tf_x, 10, tf.nn.relu)          # hidden layer
output = tf.layers.dense(l1, 1)                     # output layer

loss = tf.losses.mean_squared_error(tf_y, output)   # compute cost
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.5)
train_op = optimizer.minimize(loss)

sess = tf.Session()                                 # control training and others
sess.run(tf.global_variables_initializer())         # initialize var in graph

plt.ion()   # something about plotting

for step in range(100):
    # train and net output
    _, l, pred = sess.run([train_op, loss, output], {tf_x: x, tf_y: y})
    if step % 5 == 0:
        # plot and show learning process
        plt.cla()
        plt.scatter(x, y)
        plt.plot(x, pred, 'r-', lw=5)
        plt.text(0.5, 0, 'Loss=%.4f' % l, fontdict={'size': 20, 'color': 'red'})
        plt.pause(0.1)

plt.ioff()
plt.show()
posted @ 2021-08-26 18:48  kuanleung  阅读(21)  评论(0)    收藏  举报  来源