1. 程式人生 > >一個簡單的神經網路(TensorFlow)

一個簡單的神經網路(TensorFlow)

1.利用TensorFlow實現一個簡單的例子;

2.重點的知識有:定義權重和輸入輸出的placeholder;前向傳播;反向傳播;優化;損失函式;batch_size的基本概念

3.總結起來,訓練過程可分為三個步驟:

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

        2.定義損失函式,選擇優化演算法

        3.生成會話,選擇batch_size迭代

import tensorflow as tf
from numpy.random import RandomState      #通過numpy生成模擬資料集

#定義batch的大小
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-input")
y_ = tf.placeholder(tf.float32, shape=(None, 1), name="y-input")

#定義前向傳播;
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:
    sess.run(tf.global_variables_initializer())
    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={x: X[start:end], y_: Y[start:end]})
        if i % 1000 == 0:
            total_cross_entropy = sess.run(
            cross_entropy, feed_dict={x:X, y_:Y})
            print(i, total_cross_entropy)
        
    print(sess.run(w1))
    print(sess.run(w2))

輸出結果如下:

[[-0.8113182   1.4845988   0.06532937]
 [-2.4427042   0.0992484   0.5912243 ]]
[[-0.8113182 ]
 [ 1.4845988 ]
 [ 0.06532937]]
0 0.77943563
1000 0.7338941
2000 0.73070776
3000 0.73061645
4000 0.7305646
[[-0.427144   0.251661   1.6811208]
 [-1.8636073 -0.7442908  1.7735264]]
[[ 0.1367231]
 [ 0.5396503]
 [-1.3507011]]