1. 程式人生 > >用tensorflow構建兩層簡單神經網路(全連線)

用tensorflow構建兩層簡單神經網路(全連線)

中國大學Mooc 北京大學 人工智慧實踐:Tensorflow筆記(week3)

#coding:utf-8
#兩層簡單神經網路(全連線)

import tensorflow as tf

#定義輸入和引數
#用placeholder實現輸入定義(sess.run中喂一組資料)
x = tf.placeholder(tf.float32, shape = (None, 2))
w1 = tf.Variable(tf.random_normal([2, 3], stddev = 1, seed = 1))
w2 = tf.Variable(tf.random_normal([3, 1], stddev = 1, seed = 1))

#定義向前傳播過程
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)


#用會話計算結果
with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    print("the result of this exercise is \n", sess.run(y, feed_dict = {x:[[0.7, 0.5], [0.2, 0.3], [0.3, 0.4], [0.4, 0.5]]}))
    print("w1\n", sess.run(w1))
    print("w2\n", sess.run(w2))