深度学习之BP算法②——底层手写实现(TensorFlow)

数据集

# 数据
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]]

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值