這個程序為簡單的三層結構組成:輸入層、中間層、輸出層
運行環境為 ubuntu
要理清各層間變量個數
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
#使用numpy生成200個隨機點
x_data=np.linspace(-0.5,0.5,200)[:,np.newaxis]
noise=np.random.normal(0,0.02,x_data.shape)
y_data=np.square(x_data)+noise
#定義兩個placeholder
x=tf.placeholder(tf.float32,[None,1])
y=tf.placeholder(tf.float32,[None,1])
#定義神經網絡中間層
Weights_L1=tf.Variable(tf.random_normal([1,10]))
biases_L1=tf.Variable(tf.zeros([1,10]))
Wx_plus_b_L1=tf.matmul(x,Weights_L1)+biases_L1
L1=tf.nn.tanh(Wx_plus_b_L1)
#定義神經網絡輸出層
Weights_L2=tf.Variable(tf.random_normal([10,1]))
biases_L2=tf.Variable(tf.zeros([1,1]))
Wx_plus_b_L2=tf.matmul(L1,Weights_L2)+biases_L2
prediction=tf.nn.tanh(Wx_plus_b_L2)
#二次代價函數
loss=tf.reduce_mean(tf.square(y-prediction))
#使用梯度下降法訓練
train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss)
with tf.Session() as sess:
#變量初始化
sess.run(tf.global_variables_initializer())
for _ in range(2000):
sess.run(train_step,feed_dict={x:x_data,y:y_data})
#獲取預測值
prediction_value=sess.run(prediction,feed_dict={x:x_data})
#畫圖
plt.figure()
plt.scatter(x_data,y_data)
plt.plot(x_data,prediction_value,'r-',lw=5)
plt.show()
最終的運行結果圖片

目錄:
- tensorflow簡介、目錄
- tensorflow中的圖(02-1)
- tensorflow變量的使用(02-2)
- tensorflow中的Fetch、Feed(02-3)
- tensorflow版helloworld---擬合線性函數的k和b(02-4)
- tensorflow非線性回歸(03-1)
- MNIST手寫數字分類simple版(03-2)
- 二次代價函數、交叉熵(cross-entropy)、對數似然代價函數(log-likelihood cost)(04-1)
- 多層網絡通過防止過擬合,增加模型的准確率(04-2)
- 修改優化器進一步提升准確率(04-3)
- 手寫數字識別-卷積神經網絡cnn(06-2)
- 循環神經網絡rnn與長短時記憶神經網絡簡述(07-2)
- 循環神經網絡lstm代碼實現(07-3)
- tensorflow模型保存和使用08
- 下載inception v3 google訓練好的模型並解壓08-3
- 使用inception v3做各種圖像分類識別08-4
- word2vec模型訓練簡單案例
- word2vec+textcnn文本分類簡述及代碼
