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

测试[[1,0,0],[0,1,0]]预测结果
# 数据集
data_x = [[0,0,1],[1,1,1],[1,0,1],[0,1,1]]
data_y = [[0],[1],[1],[0]]

# 测试集
test_x = [[1,0,0],[0,1,0]]

手写代码如下

import tensorflow as tf
import matplotlib.pyplot as plt

tf.set_random_seed(1)

# 数据集
data_x = [[0,0,1],[1,1,1],[1,0,1],[0,1,1]]
data_y = [[0],[1],[1],[0]]

# 测试集
test_x = [[1,0,0],[0,1,0]]

# 占位符
x = tf.placeholder(tf.float32,[None,3])
y = tf.placeholder(tf.float32,[None,1])

#定义模型
w1 = tf.Variable(tf.random_normal([3,5]),name='Weight1')
b1 = tf.Variable(tf.random_normal([5]),name='baci1')
w2 = tf.Variable(tf.random_normal([5,1]),name='Weight2')
b2 = tf.Variable(tf.random_normal([1]),name='baci2')

# 前向传播
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 = 10e-2
update = [
    tf.assign(w2,w2-alpha*dw2),
    tf.assign(w1,w1-alpha*dw1),
    tf.assign(b2,b2-alpha*db2),
    tf.assign(b1,b1-alpha*db1),
]

# 开启会话
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()

# 预测
test_h = sess.run(a2,feed_dict={x:test_x})
print(test_h)


效果如下

step 0 cost 0.7326546
step 500 cost 0.09527467
step 1000 cost 0.032945182
step 1500 cost 0.018553516
step 2000 cost 0.012614363
step 2500 cost 0.0094508035
step 3000 cost 0.0075089647
step 3500 cost 0.006204647
step 4000 cost 0.005272277
step 4500 cost 0.004574685
step 5000 cost 0.004034303
step 5500 cost 0.0036040938
step 6000 cost 0.0032539144
step 6500 cost 0.0029636188
step 7000 cost 0.0027192552
step 7500 cost 0.0025109127
step 8000 cost 0.0023311898
step 8500 cost 0.002174723
step 9000 cost 0.0020373205
step 9500 cost 0.001915689
step 10000 cost 0.0018073446

[[0.9991935 ]
 [0.01141745]]

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值