实例中通过单个隐藏层神经网络来拟合抑或运算,隐藏层使用Relu函数进行激活,输出层使用Tanh函数进行激活。
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 4 09:46:52 2020
@author: Melinda
"""
import tensorflow as tf
import numpy as np
# ------定义变量-----------
learning_rate =1e-4
n_input = 2
n_label = 1
n_hidden = 2
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_label])
# h1:代表隐藏层
# h2:代表输出层
# 以字典方式定义w 和 b
# ---------定义学习参数----------------
weights = {
'h1': tf.Variable(tf.truncated_normal([n_input, n_hidden], stddev=0.1)),
'h2': tf.Variable(tf.truncated_normal([n_hidden, n_label], stddev=0.1))
}
bias = {
'b1':tf.Variable(tf.zeros([n_hidden])),
'b2':tf.Variable(tf.zeros([n_label]))
}
#----------定义网络模型-----------
layer_1 = tf.nn.relu(tf.add(tf.matmul(x, weights['h1']), bias['b1']))
y_pred = tf.nn.tanh(tf.add(tf.matmul(layer_1, weights['h2']), bias['b2']))
loss = tf.reduce_mean((y_pred-y)**2)
train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss)
# 构架模拟数据
X=[[0,0],[0,1],[1,0],[1,1]]
Y=[[0],[1],[1],[0]]
X=np.array(X).astype('float32')
Y=np.array(Y).astype('int16')
# 加载session
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
for i in range(10000):
sess.run(train_step, feed_dict={x:X, y:Y})
print("10000次迭代输出")
print(sess.run(y_pred, feed_dict={x:X}))
print("隐藏层结果输出")
print(sess.run(layer_1, feed_dict={x:X}))