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

Tensorflow(二十九) —— 卷积神经网络(CNN),第1张

Tensorflow(二十九) —— 卷积神经网络(CNN)
  • 1. layers.BatchNormalization
  • 2. 实战

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/714977.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存