卷积神经网络之卷积层 学习笔记
github代码地址:https://github.com/taichuai/d2l_zh_tensorflow2.0
卷积部分推荐之前的一篇博客,内容很全面
构造了一个 1 * 2 的卷积核[1, -1],用于边缘检测
Y_hat = conv2d(X)
for i in range(10):
with tf.GradientTape(watch_accessed_variables=False) as g:
g.watch(conv2d.weights[0])
Y_hat = conv2d(X)
l = (abs(Y_hat - Y)) ** 2
dl = g.gradient(l, conv2d.weights[0])
lr = 3e-2
update = tf.multiply(lr, dl)
updated_weights = conv2d.get_weights()
updated_weights[0] = conv2d.weights[0] - update
conv2d.set_weights(updated_weights)
if (i + 1)% 2 == 0:
print('batch %d, loss %.3f' % (i + 1, tf.reduce_sum(l)))