Tensorflow(二十九) —— 卷积神经网络(CNN)

Tensorflow(二十九) —— 卷积神经网络(CNN)

1. layers.BatchNormalization

import tensorflow as tf
from tensorflow.keras import layers,optimizers
import matplotlib.pyplot as plt
# ************************ layers.BatchNormalization
x = tf.random.normal([784,10],mean = 1,stddev=0.5)
net = layers.BatchNormalization(axis = -1,\
                               center = True,\
                               scale = True,\
                               trainable = True)
net(x,training = False).shape

2. 实战

x = tf.random.normal([2,3])
net = layers.BatchNormalization()

out = net(x)
print(out.shape)

print(net.trainable_variables)
print(net.variables)

x = tf.random.normal([2,4,4,3],mean = 1,stddev=0.5)
net = layers.BatchNormalization(axis = -1)

out = net(x,training = False)
print(net.variables)

out1 = net(x,training = True)
print(net.variables)

for i in range(100):
    net(x,training = True)
print(net.variables)

# backward update
optimizer = optimizers.Adam(lr = 1e-3)
for i in range(10):
    with tf.GradientTape() as tape:
        out = net(x,training = True)
        loss = tf.reduce_mean(out**2)
    grads = tape.gradient(loss,net.trainable_variables)
    optimizer.apply_gradients(zip(grads,net.trainable_variables))

print(net.trainable_variables)

本文为参考龙龙老师的“深度学习与TensorFlow 2入门实战“课程书写的学习笔记

by CyrusMay 2022 04 18

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值