数据集
# 数据
data_x = [[0,0],[0,1],[1,0],[1,1]]
data_y = [[0],[1],[1],[0]]
手写底层
import tensorflow as tf
import matplotlib.pyplot as plt
# 随机种子
tf.set_random_seed(777)
# 数据
data_x = [[0,0],[0,1],[1,0],[1,1]]
data_y = [[0],[1],[1],[0]]
# 站位
x = tf.placeholder(tf.float32,[None,2])
y = tf.placeholder(tf.float32,[None,1])
# 定义模型
w1 = tf.Variable(tf.random_normal([2,3]),tf.float32)
b1 = tf.Variable(tf.random_normal([3]),tf.float32)
w2 = tf.Variable(tf.random_normal([3,1]),tf.float32)
b2 = tf.Variable(tf.random_normal([1]),tf.float32)
# 前向传播
z1 = tf.matmul(x,w1) + b1
a1 = tf.sigmoid(z1)
z2 = tf.matmul(a1,w2) + b2
a2 = tf.sigmoid(z2)
# 代价
cost = -tf.reduce_mean(y*tf.log(a2)+(1-y)*tf.log(1-a2))
cost_history = []
# 向后传播
dz2 = a2 - y
dw2 = tf.matmul(tf.transpose(a1),dz2) / tf.cast(tf.shape(x)[0],tf.float32)
db2 = tf.reduce_mean(dz2,0)
da1 = tf.matmul(dz2,tf.transpose(w2))
dz1 = da1 * a1*(1-a1)
dw1 = tf.matmul(tf.transpose(x),dz1) / tf.cast(tf.shape(x)[0],tf.float32)
db1 = tf.reduce_mean(dz1,0)
alpha = 2*10e-2
update = [
tf.assign(w1,w1-alpha*dw1),
tf.assign(b1,b1-alpha*db1),
tf.assign(w2,w2-alpha*dw2),
tf.assign(b2,b2-alpha*db2),
]
# 开启会话
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# 更新参数
for step in range(10001):
cost_cvl,_ = sess.run([cost,update],feed_dict={x:data_x,y:data_y})
if step % 500 == 0:
print('step',step,'cost',cost_cvl)
cost_history.append(cost_cvl)
# 画图
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.plot(cost_history[1:])
plt.show()
# 预测
pre = sess.run(a2,feed_dict={x:data_x})
print(pre)
效果展示
step 0 cost 1.1753627
step 500 cost 0.685488
step 1000 cost 0.60371494
step 1500 cost 0.3757024
step 2000 cost 0.10215757
step 2500 cost 0.04646945
step 3000 cost 0.02885376
step 3500 cost 0.020645259
step 4000 cost 0.015976189
step 4500 cost 0.01298678
step 5000 cost 0.010917876
step 5500 cost 0.00940509
step 6000 cost 0.008252881
step 6500 cost 0.00734713
step 7000 cost 0.006617183
step 7500 cost 0.0060167587
step 8000 cost 0.0055144695
step 8500 cost 0.005088268
step 9000 cost 0.00472223
step 9500 cost 0.0044045085
step 10000 cost 0.004126258
[[0.00451179]
[0.99503833]
[0.99661213]
[0.00360679]]