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

本文介绍了TensorFlow中BatchNormalization层的基本用法,包括参数设置、维度处理,并通过实战演示了如何在不同场景下使用,以及如何配合优化器进行梯度更新。
部署运行你感兴趣的模型镜像

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

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值