乙個簡單的神經網路例子

2021-09-05 08:45:58 字數 4533 閱讀 5984

神經系統的搭建

import tensorflow as tf

import numpy as np

import matplotlib,pylab as plt

def add_layer(input,in_size,out_size,activation_function = none):

weights = tf.variable(tf.random_normal([in_size,out_size]))

biases = tf.variable(tf.zeros([1,out_size])+0.1)

wx_plus_b = tf.matmul(input,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 #對x_data進行平方

xs = tf.placeholder(tf.float32,[none,1])

ys = tf.placeholder(tf.float32,[none,1])

#神經層到輸出層

l1 = add_layer(xs,1,10,activation_function=tf.nn.relu)

prediction = add_layer(l1,10,1,activation_function=none)

loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1])) #將每個y_data-prediction值取平方,然後求和後去平均值

train_step = tf.train.gradientdescentoptimizer(0.1).minimize(loss)

init = tf.initialize_all_variables()

sess = tf.session()

sess.run(init)

fig = plt.figure()

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

ax.scatter(x_data,y_data)

plt.ion() #使程式不暫停,影象可變動

plt.show()

for i in range(1000):

sess.run(train_step,feed_dict = )

if i%50==0:

try:

ax.lines.remove(lines[0])

except exception:

pass

prediction_value = sess.run(prediction,feed_dict=)

lines = ax.plot(x_data,prediction_value,'r-',lw = 5)

plt.pause(0.1)

#print(sess.run(loss,feed_dict =))```

將其視覺化

import numpy as np

import matplotlib,pylab as plt

def add_layer(input,in_size,out_size,n_layer,activation_function = none):

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.summary.histogram(layer_name+'/weights',weights)

with tf.name_scope('biases'):

biases = tf.variable(tf.zeros([1,out_size])+0.1)

tf.summary.histogram(layer_name + '/biases', biases)

with tf.name_scope('wx_plus_b'):

wx_plus_b = tf.matmul(input,weights)+biases

if activation_function is none:

outputs = wx_plus_b

tf.summary.histogram(layer_name + '/outputs', outputs)

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 #對x_data進行平方

with tf.name_scope('input'):

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,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])) #將每個y_data-prediction值取平方,然後求和後去平均值

tf.summary.scalar('loss',loss) #event圖

with tf.name_scope('train'):

train_step = tf.train.gradientdescentoptimizer(0.1).minimize(loss)

init = tf.initialize_all_variables()

sess = tf.session()

merged = tf.summary.merge_all()

writer = tf.summary.filewriter("c:/users/administrator/desktop/model",sess.graph)

sess.run(init)

for i in range(1000):

sess.run(train_step,feed_dict=)

if i%50==0:

result = sess.run(merged,feed_dict=)

writer.add_summary(result,i)

fig = plt.figure()

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

ax.scatter(x_data,y_data)

plt.ion() #使程式不暫停,影象可變動

plt.show()

for i in range(1000):

sess.run(train_step,feed_dict = )

if i%50==0:

try:

ax.lines.remove(lines[0])

except exception:

pass

prediction_value = sess.run(prediction,feed_dict=)

lines = ax.plot(x_data,prediction_value,'r-',lw = 5)

plt.pause(0.1)

#print(sess.run(loss,feed_dict =))

步驟1:

步驟2:

乙個簡單的神經網路例子

來自神經網路之家 日期 2015 07 1611 18 37.0 用於訓練的輸入資料 對應的輸出資料 我們這裡設定 1 節點個數設定 輸入層 隱層 輸出層的節點個數分別為 2 3,1 2 傳遞函式設定 隱層 tansig函式 輸出層 purelin函式 3 訓練方式 trainlm。即得到下圖的模型...

TensorFlow 乙個簡單的神經網路

利用tensorflow實現第乙個簡單的神經網路 encoding utf 8 import tensorflow as tf numpy生成模擬資料集 from numpy.random import randomstate 定義訓練資料batch大小 batch size 8 定義神經網路的引數...

搭建乙個簡單的神經網路

def add layer 最基礎的四個引數 輸入值,輸入的大小,輸出的大小和激勵函式,激勵函式可以自己新增,也可以不使用激勵函式。def add layer inputs,in size,out size,activation function none 接下來我們定義weight和biases ...