乙個簡單的tensorFlow關於神經網路的示例

2021-08-19 02:14:21 字數 3497 閱讀 9360

這個示例源自《實戰google 深度學習框架》一書的第三章,實現了乙個簡單的前向網路的演算法。下面掛完整的**

import tensorflow as tf

from numpy.random import randomstate

batch_size = 8

w1 = tf.variable(tf.random_normal([2, 3], stddev=1, seed=1))

w2 = tf.variable(tf.random_normal([3, 1], stddev=1, seed=1))

x = tf.placeholder(tf.float32, shape=(none, 2), name='x-inout')

y_ = tf.placeholder(tf.float32, shape=(none, 1), name='y-inout')

a = tf.matmul(x, w1)

y = tf.matmul(a, w2)

y = tf.sigmoid(y)

cross_entropy = -tf.reduce_mean(

y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)) + (1 - y) * tf.log(tf.clip_by_value(1 - y, 1e-10, 1.0)))

train_step = tf.train.adamoptimizer(0.001).minimize(cross_entropy)

rdm = randomstate(1)

dataset_size = 128

x = rdm.rand(dataset_size, 2)

y = [[int(x1 + x2 < 1)] for (x1, x2) in x]

with tf.session() as sess:

init_op = tf.global_variables_initializer()

sess.run(init_op)

print(sess.run(w1))

print(sess.run(w2))

steps = 5000

for i in range(steps):

start = (i * batch_size) % dataset_size

end=min(start+batch_size,dataset_size)

sess.run(train_step,feed_dict=)

if i%1000==0:

total_cross_entropy=sess.run(cross_entropy,feed_dict=)

print("after %d training step(s),cross entropy on all data is %g",(i,total_cross_entropy))

print(sess.run(w1))

print(sess.run(w2))

w1 = tf.variable(tf.random_normal([2, 3], stddev=1, seed=1))

w2 = tf.variable(tf.random_normal([3, 1], stddev=1, seed=1))

表示初始化兩個權重,其中w1是2*3的矩陣,其元素為正態分佈(random_normal),並且標準差為1(stddev=1);同理,w2是乙個3*1的矩陣。需要強調的是,這裡只是定義了矩陣,並沒有實際的運算賦值。

x = tf.placeholder(tf.float32, shape=(none, 2), name='x-inout')

y_ = tf.placeholder(tf.float32, shape=(none, 1), name='y-inout')

以上兩行**意味著生成了兩個輸入向量x,y_,placeholder函式分別定義了(資料型別,維度,名字)。shape(none,2)代表第一維為空,第二維有兩個元素。這裡使用none是為了可以使用不同的batch大小。

a = tf.matmul(x, w1)

y = tf.matmul(a, w2)

y = tf.sigmoid(y)

這裡表示了正向運算的過程,通過線性變換並且使用激勵函式sigmoid得到乙個y值,這個值就是計算(估計)出的結果。

cross_entropy = -tf.reduce_mean(

y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)) + (1 - y) * tf.log(tf.clip_by_value(1 - y, 1e-10, 1.0)))

train_step = tf.train.adamoptimizer(0.001).minimize(cross_entropy)

這裡是寫了乙個簡單的損失函式,函式的具體工作就是通過反饋網路實現權重w1與w2的更新,知道損失函式的值達到「最小時」,認為權重w1與w2穩定並趨於最優解。

rdm = randomstate(1)

dataset_size = 128

x = rdm.rand(dataset_size, 2)

y = [[int(x1 + x2 < 1)] for (x1, x2) in x]

這裡規定了資料集的元素個數,以及生成了輸入向量x以及真實結果向量y

with tf.session() as sess:

init_op = tf.global_variables_initializer()

sess.run(init_op)

print(sess.run(w1))

print(sess.run(w2))

steps = 5000

for i in range(steps):

start = (i * batch_size) % dataset_size

end=min(start+batch_size,dataset_size)

sess.run(train_step,feed_dict=)

if i%1000==0:

total_cross_entropy=sess.run(cross_entropy,feed_dict=)

print("after %d training step(s),cross entropy on all data is %g",(i,total_cross_entropy))

print(sess.run(w1))

print(sess.run(w2))

以上對整個神經網路進行迭代,其中每次選擇batch_size=8個輸入向量進行權值,一共迭代了5000次,最後輸出穩定的結果。

1.定義神經網路的結構和前向傳播的輸出結果

2.定義損失函式以及反向傳播優化演算法

3.生成會話 tf.session,並在訓練資料上反覆執行直到方向傳播演算法使權值穩定的時候(損失函式最小)時收斂。

TensorFlow 乙個簡單的神經網路

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

用tensorflow建造乙個簡單的神經網路

reference 這篇文章主要講怎樣建造乙個完整的神經網路,包括新增神經層,計算誤差,訓練步驟 我們知道乙個神經網路可能包含很多個隱藏層,所以如果我們定義乙個新增層函式,在後面如果需要增加神經層就只需要呼叫我們定義的新增層函式就可以了。神經層裡常見的引數通常有 weights,biases和激勵函...

用tensorflow搭建乙個簡單的神經網路

在神經網路板塊斷斷續續進行了4個月的摸索,逐漸形成了自己對這個領域的認識,寫成部落格一是為了分享自己的觀點,希望不足之處能得到指正 二是作為讀書周記,能督促自己能不要停下來更新部落格,持續學習。好了以下就是我的第一章,用tensorflow搭建乙個簡易的 沒有隱藏層的 神經網路。用tensorflo...