神经网络可视化
# -*- coding: utf-8 -*-
"""
Created on Mon May 17 09:49:58 2021
@author: monastic
"""
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# in_size=输入向量的维度,即一个神经元的权值个数
#out_size=这层神经元的个数
def add_layer(inputs,in_size,out_size,activation_function=None):
with tf.name_scope('layer'):
with tf.name_scope('weights'):
Weights = tf.Variable(tf.random_normal([in_size,out_size]), name = 'W')
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([1,out_size])+0.1, name = 'b')
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.add(tf.matmul(inputs,Weights),biases)
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
###
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
###
with tf.name_scope('inputs'):
xs = tf.placeholder(tf.float32, [None, 1], name = 'x_input')
ys = tf.placeholder(tf.float32, [None, 1], name = 'y_input')
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
prediction = add_layer(l1, 10, 1, activation_function=None)
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
#reduction_indices表求值的维度
with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
sess = tf.Session()
writer = tf.summary.FileWriter('logs/', sess.graph)#指定一个文件用来保存图。log是事件文件所在的目录,这里是工程目录下的log目录。第二个参数是事件文件要记录的图,也就是TensorFlow默认的图。
init = tf.initialize_all_variables()
sess.run(init)
fig = plt.figure()#生成一个图片框
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data,y_data)
plt.show()
for i in range(1000):
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
if i % 50 == 0:
# print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
try:
ax.lines.remove(lines[0])
except Exception:
pass
prediction_value = sess.run(prediction, feed_dict = {xs: x_data})
lines = ax.plot(x_data,prediction_value,'r-', lw = 5)
plt.pause(0.3)
(python3.6) PS C:\Users\monk> cd d:
(python3.6) PS D:\> cd .\Experiment\
(python3.6) PS D:\Experiment> cd .\logs\
(python3.6) PS D:\Experiment\logs> tensorboard --logdir='/'
D:\ProgramFiles\anaconda3\envs\python3.6\lib\site-packages\tensorflow\python\framework\dtypes.py:523: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
D:\ProgramFiles\anaconda3\envs\python3.6\lib\site-packages\tensorflow\python\framework\dtypes.py:524: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
D:\ProgramFiles\anaconda3\envs\python3.6\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
D:\ProgramFiles\anaconda3\envs\python3.6\lib\site-packages\tensorflow\python\framework\dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
D:\ProgramFiles\anaconda3\envs\python3.6\lib\site-packages\tensorflow\python\framework\dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
D:\ProgramFiles\anaconda3\envs\python3.6\lib\site-packages\tensorflow\python\framework\dtypes.py:532: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
TensorBoard 1.9.0 at http://DESKTOP-VFSGDHV:6006 (Press CTRL+C to quit)
复制http://DESKTOP-VFSGDHV:6006 用浏览器打开