二次函数用dnn拟合, Matplotlib.pyplot 可视化,6种optmizer, varibal_scop, tensorboar

二次函数用dnn拟合, Matplotlib.pyplot 可视化,6种optmizer, varibal_scop, tensorboard

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

def add_layer(input, in_size, out_size, activation=None, name='nn', reuse=False):
    with tf.variable_scope(name, reuse=reuse):
        # Weight = tf.Variable(tf.random_normal(shape=(in_size, out_size)), dtype=tf.float32)
        Weight = tf.get_variable(name='Weight', shape=(in_size, out_size), dtype=tf.float32, initializer=tf.random_normal_initializer())
        tf.summary.histogram(name + '/Weight', Weight)
        # bias = tf.Variable(tf.zeros(shape=(1, out_size), dtype=tf.float32) + 0.1)
        bias = tf.get_variable(name='bias', shape=(out_size), dtype=tf.float32, initializer=tf.constant_initializer(0.1))
        with tf.variable_scope('matmul'):
            Wx_b = tf.matmul(input, Weight) + bias
            print(Wx_b)
        if activation is None:
            output = Wx_b
        else:
            output = activation(Wx_b)
        return output

#data prepare
x_data = np.linspace(-1, 1, 300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

#add placehoder
xs = tf.placeholder(shape=(None, 1), dtype=tf.float32, name='input_x')
ys = tf.placeholder(shape=(None, 1), dtype=tf.float32, name='input_y')

#def nn structure
hidden_value = add_layer(xs, 1, 10, tf.nn.relu, name='hidden_layer')
y = add_layer(hidden_value, 10, 1, None, name='output_layer')
with tf.variable_scope('loss'):
    loss = tf.reduce_mean(tf.square(y - ys))
    tf.summary.scalar('loss', loss)
# opt = tf.train.GradientDescentOptimizer(0.01)
with tf.variable_scope('train'):
    opt = tf.train.AdamOptimizer(0.01)
    train = opt.minimize(loss)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

ax.scatter(x_data, y_data)
plt.ion()
plt.show()
ax.scatter(y_data, x_data)
plt.pause(1)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    merged = tf.summary.merge_all()
    writer = tf.summary.FileWriter('logs/', graph=sess.graph)
    sess.run(init)
    for i in range(1000):
        _, error = sess.run([train, loss], feed_dict={xs:x_data, ys:y_data})
        if i % 50 == 0:
            merged_value = sess.run(merged, feed_dict={xs:x_data, ys:y_data})
            writer.add_summary(merged_value, i)
            print(i, error)
            predict = sess.run(y, feed_dict={xs:x_data})
            ax.plot(x_data, predict)
            plt.pause(0.2)
            # plt.show()

猜你喜欢

转载自blog.csdn.net/hujiankun073/article/details/88699467