目標(biāo):利用終端實(shí)現(xiàn)一個(gè)簡單的神經(jīng)網(wǎng)絡(luò)示意圖
硬件工具:Mac
軟件工具:tensorflow
創(chuàng)建py文件
這是一個(gè)小型的神經(jīng)網(wǎng)絡(luò)讯壶,輸入--》隱層1 --》 隱層2 --》輸出
import tensorflow as tf
#create layer
def add_layer(inputs, in_size, out_size, activation_function=None):
# add one more layer and return the output of this layer
#this is label
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 ,y placeholder
with tf.name_scope('inputs'):
xs= tf.placeholder(tf.float32, [None, 1],name='x_in')
ys= tf.placeholder(tf.float32, [None, 1],name='y_in')
#layer
l1 = add_layer(xs,1,10,activation_function=tf.nn.relu)
#prediction
prediction = add_layer(l1,10,1,activation_function=None)
#loss to measure model
with tf.name_scope('loss'):
loss = tf.reduce_mean(
tf.reduce_sum(
tf.square(ys - prediction),
reduction_indices=[1]
))
#train
with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# start run 荠雕,first create session
sess = tf.Session()
#create a file in the logs document
writer = tf.train.SummaryWriter("logs/",sess.graph)
# this isimportant
sess.run(tf.initialize_all_variables())
運(yùn)行python坑质,接著使用terminal中cd定位log所在的文件位置狐赡,輸入
python -m tensorflow.tensorboard --logdir=logs
終端會(huì)出現(xiàn)
圖片中的 http://192.168.0.101:6006
此時(shí)就能在圖片中查看生成的小型神經(jīng)網(wǎng)絡(luò)結(jié)構(gòu)圖啦
另外一般會(huì)在event中生成loss圖像唯竹,在distributions展示圖標(biāo)中 histograms展示權(quán)重和偏值與輸出值的直方圖
需要對(duì)代碼進(jìn)行相應(yīng)的添加顯示語句
import tensorflow as tf
import numpy as np
def add_layer(inputs, in_size, out_size, n_layer,activation_function=None):
# add one more layer and return the output of this layer
layer_name = 'layer%s'%n_layer
with tf.name_scope(layer_name):
with tf.name_scope('weights'):
Weights = tf.Variable(
tf.random_normal([in_size, out_size]),
name='W')
tf.histogram_summary(layer_name+'/weights',Weights)
with tf.name_scope('biases'):
biases = tf.Variable(
tf.zeros([1, out_size]) + 0.1,
name='b')
tf.histogram_summary(layer_name+'/biases',biases)
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, )
tf.histogram_summary(layer_name+'/outputs',outputs)
return outputs
# make up some real data
#numpy.linspace(start沸停,stop辆影,num = 50良狈,endpoint = True后添,retstep = False,dtype = None)
x_data = np.linspace(-1,1,300)[:,np.newaxis] #從-1 ~1 列上添加一個(gè)維度
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_in')
ys= tf.placeholder(tf.float32, [None, 1],name='y_in')
l1 = add_layer(xs,1,10,n_layer=1,activation_function=tf.nn.relu)
prediction = add_layer(l1,10,1,n_layer=2,activation_function=None)
with tf.name_scope('loss'):
loss = tf.reduce_mean(
tf.reduce_sum(
tf.square(ys - prediction),
reduction_indices=[1]
))
tf.scalar_summary('loss',loss)
with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
sess = tf.Session()
merged = tf.merge_all_summaries()
writer = tf.train.SummaryWriter("logs/",sess.graph)
sess.run(tf.initialize_all_variables())
for i in range(1000):
sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
if i%50 == 0:
result = sess.run(merged,feed_dict={xs:x_data,ys:y_data})
writer.add_summary(result,i)
圖形如下