python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > tf.keras.layers模块的函数

解读tf.keras.layers模块中的函数

作者:. 小王同学.

这篇文章主要介绍了tf.keras.layers模块中的函数,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

tf.keras.layers模块中的函数

from __future__ import print_function as _print_function

import sys as _sys

from . import experimental
from tensorflow.python.keras.engine.base_layer import Layer
from tensorflow.python.keras.engine.input_layer import Input
from tensorflow.python.keras.engine.input_layer import InputLayer
from tensorflow.python.keras.engine.input_spec import InputSpec
from tensorflow.python.keras.feature_column.dense_features_v2 import DenseFeatures
from tensorflow.python.keras.layers.advanced_activations import ELU
from tensorflow.python.keras.layers.advanced_activations import LeakyReLU
from tensorflow.python.keras.layers.advanced_activations import PReLU
from tensorflow.python.keras.layers.advanced_activations import ReLU
from tensorflow.python.keras.layers.advanced_activations import Softmax
from tensorflow.python.keras.layers.advanced_activations import ThresholdedReLU
from tensorflow.python.keras.layers.convolutional import Conv1D
from tensorflow.python.keras.layers.convolutional import Conv1D as Convolution1D
from tensorflow.python.keras.layers.convolutional import Conv1DTranspose
from tensorflow.python.keras.layers.convolutional import Conv1DTranspose as Convolution1DTranspose
from tensorflow.python.keras.layers.convolutional import Conv2D
from tensorflow.python.keras.layers.convolutional import Conv2D as Convolution2D
from tensorflow.python.keras.layers.convolutional import Conv2DTranspose
from tensorflow.python.keras.layers.convolutional import Conv2DTranspose as Convolution2DTranspose
from tensorflow.python.keras.layers.convolutional import Conv3D
from tensorflow.python.keras.layers.convolutional import Conv3D as Convolution3D
from tensorflow.python.keras.layers.convolutional import Conv3DTranspose
from tensorflow.python.keras.layers.convolutional import Conv3DTranspose as Convolution3DTranspose
from tensorflow.python.keras.layers.convolutional import Cropping1D
from tensorflow.python.keras.layers.convolutional import Cropping2D
from tensorflow.python.keras.layers.convolutional import Cropping3D
from tensorflow.python.keras.layers.convolutional import DepthwiseConv2D
from tensorflow.python.keras.layers.convolutional import SeparableConv1D
from tensorflow.python.keras.layers.convolutional import SeparableConv1D as SeparableConvolution1D
from tensorflow.python.keras.layers.convolutional import SeparableConv2D
from tensorflow.python.keras.layers.convolutional import SeparableConv2D as SeparableConvolution2D
from tensorflow.python.keras.layers.convolutional import UpSampling1D
from tensorflow.python.keras.layers.convolutional import UpSampling2D
from tensorflow.python.keras.layers.convolutional import UpSampling3D
from tensorflow.python.keras.layers.convolutional import ZeroPadding1D
from tensorflow.python.keras.layers.convolutional import ZeroPadding2D
from tensorflow.python.keras.layers.convolutional import ZeroPadding3D
from tensorflow.python.keras.layers.convolutional_recurrent import ConvLSTM2D
from tensorflow.python.keras.layers.core import Activation
from tensorflow.python.keras.layers.core import ActivityRegularization
from tensorflow.python.keras.layers.core import Dense
from tensorflow.python.keras.layers.core import Dropout
from tensorflow.python.keras.layers.core import Flatten
from tensorflow.python.keras.layers.core import Lambda
from tensorflow.python.keras.layers.core import Masking
from tensorflow.python.keras.layers.core import Permute
from tensorflow.python.keras.layers.core import RepeatVector
from tensorflow.python.keras.layers.core import Reshape
from tensorflow.python.keras.layers.core import SpatialDropout1D
from tensorflow.python.keras.layers.core import SpatialDropout2D
from tensorflow.python.keras.layers.core import SpatialDropout3D
from tensorflow.python.keras.layers.dense_attention import AdditiveAttention
from tensorflow.python.keras.layers.dense_attention import Attention
from tensorflow.python.keras.layers.embeddings import Embedding
from tensorflow.python.keras.layers.local import LocallyConnected1D
from tensorflow.python.keras.layers.local import LocallyConnected2D
from tensorflow.python.keras.layers.merge import Add
from tensorflow.python.keras.layers.merge import Average
from tensorflow.python.keras.layers.merge import Concatenate
from tensorflow.python.keras.layers.merge import Dot
from tensorflow.python.keras.layers.merge import Maximum
from tensorflow.python.keras.layers.merge import Minimum
from tensorflow.python.keras.layers.merge import Multiply
from tensorflow.python.keras.layers.merge import Subtract
from tensorflow.python.keras.layers.merge import add
from tensorflow.python.keras.layers.merge import average
from tensorflow.python.keras.layers.merge import concatenate
from tensorflow.python.keras.layers.merge import dot
from tensorflow.python.keras.layers.merge import maximum
from tensorflow.python.keras.layers.merge import minimum
from tensorflow.python.keras.layers.merge import multiply
from tensorflow.python.keras.layers.merge import subtract
from tensorflow.python.keras.layers.noise import AlphaDropout
from tensorflow.python.keras.layers.noise import GaussianDropout
from tensorflow.python.keras.layers.noise import GaussianNoise
from tensorflow.python.keras.layers.normalization import LayerNormalization
from tensorflow.python.keras.layers.normalization_v2 import BatchNormalization
from tensorflow.python.keras.layers.pooling import AveragePooling1D
from tensorflow.python.keras.layers.pooling import AveragePooling1D as AvgPool1D
from tensorflow.python.keras.layers.pooling import AveragePooling2D
from tensorflow.python.keras.layers.pooling import AveragePooling2D as AvgPool2D
from tensorflow.python.keras.layers.pooling import AveragePooling3D
from tensorflow.python.keras.layers.pooling import AveragePooling3D as AvgPool3D
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling1D
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling1D as GlobalAvgPool1D
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling2D
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling2D as GlobalAvgPool2D
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling3D
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling3D as GlobalAvgPool3D
from tensorflow.python.keras.layers.pooling import GlobalMaxPooling1D
from tensorflow.python.keras.layers.pooling import GlobalMaxPooling1D as GlobalMaxPool1D
from tensorflow.python.keras.layers.pooling import GlobalMaxPooling2D
from tensorflow.python.keras.layers.pooling import GlobalMaxPooling2D as GlobalMaxPool2D
from tensorflow.python.keras.layers.pooling import GlobalMaxPooling3D
from tensorflow.python.keras.layers.pooling import GlobalMaxPooling3D as GlobalMaxPool3D
from tensorflow.python.keras.layers.pooling import MaxPooling1D
from tensorflow.python.keras.layers.pooling import MaxPooling1D as MaxPool1D
from tensorflow.python.keras.layers.pooling import MaxPooling2D
from tensorflow.python.keras.layers.pooling import MaxPooling2D as MaxPool2D
from tensorflow.python.keras.layers.pooling import MaxPooling3D
from tensorflow.python.keras.layers.pooling import MaxPooling3D as MaxPool3D
from tensorflow.python.keras.layers.recurrent import AbstractRNNCell
from tensorflow.python.keras.layers.recurrent import RNN
from tensorflow.python.keras.layers.recurrent import SimpleRNN
from tensorflow.python.keras.layers.recurrent import SimpleRNNCell
from tensorflow.python.keras.layers.recurrent import StackedRNNCells
from tensorflow.python.keras.layers.recurrent_v2 import GRU
from tensorflow.python.keras.layers.recurrent_v2 import GRUCell
from tensorflow.python.keras.layers.recurrent_v2 import LSTM
from tensorflow.python.keras.layers.recurrent_v2 import LSTMCell
from tensorflow.python.keras.layers.serialization import deserialize
from tensorflow.python.keras.layers.serialization import serialize
from tensorflow.python.keras.layers.wrappers import Bidirectional
from tensorflow.python.keras.layers.wrappers import TimeDistributed
from tensorflow.python.keras.layers.wrappers import Wrapper

del _print_function

汇总tf.keras模型层layers

卷积网络相关层

示例代码一、搭建LeNet-5神经网络

import tensorflow as tf
from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics, losses
 
# 1.数据集准备
(x, y), (x_val, y_val) = datasets.mnist.load_data()  # 加载数据集,返回的是两个元组,分别表示训练集和测试集
x = tf.convert_to_tensor(x, dtype=tf.float32) / 255.  # 转换为张量,并缩放到0~1
y = tf.convert_to_tensor(y, dtype=tf.int32)  # 转换为张量(标签)
print(x.shape, y.shape)
train_dataset = tf.data.Dataset.from_tensor_slices((x, y))  # 构建数据集对象
train_dataset = train_dataset.batch(32).repeat(10)  # 设置批量训练的batch为32,要将训练集重复训练10遍
 
# 2.搭建网络
network = Sequential([  # 搭建网络容器
    layers.Conv2D(6, kernel_size=3, strides=1),  # 第一个卷积层,6个3*3*1卷积核
    layers.MaxPooling2D(pool_size=2, strides=2),  # 池化层,卷积核2*2,步长2
    layers.ReLU(),  # 激活函数
    layers.Conv2D(16, kernel_size=3, strides=1),  # 第二个卷积层,16个3*3*6卷积核
    layers.MaxPooling2D(pool_size=2, strides=2),  # 池化层
    layers.ReLU(),  # 激活函数
    layers.Flatten(),  # 拉直,方便全连接层处理
    layers.Dense(120, activation='relu'),  # 全连接层,120个节点
    layers.Dense(84, activation='relu'),  # 全连接层,84个节点
    layers.Dense(10)  # 输出层,10个节点
])
network.build(input_shape=(None, 28, 28, 1))  # 定义输入,batch_size=32,输入图片大小是28*28,通道数为1。
network.summary()  # 显示出每层的待优化参数量
 
# 3.模型训练(计算梯度,迭代更新网络参数)
optimizer = optimizers.SGD(lr=0.01)  # 声明采用批量随机梯度下降方法,学习率=0.01
acc_meter = metrics.Accuracy()  # 新建accuracy测量器
for step, (x, y) in enumerate(train_dataset):  # 一次输入batch组数据进行训练
    with tf.GradientTape() as tape:  # 构建梯度记录环境
        x = tf.reshape(x, (32, 28, 28, 1))  # 将输入拉直,[b,28,28]->[b,784]
        # x = tf.extand_dims(x, axis=3)
        out = network(x)  # 输出[b, 10]
        y_onehot = tf.one_hot(y, depth=10)  # one-hot编码
        loss = tf.square(out - y_onehot)
        loss = tf.reduce_sum(loss) / 32  # 定义均方差损失函数,注意此处的32对应为batch的大小
        grads = tape.gradient(loss, network.trainable_variables)  # 计算网络中各个参数的梯度
        optimizer.apply_gradients(zip(grads, network.trainable_variables))  # 更新网络参数
        acc_meter.update_state(tf.argmax(out, axis=1), y)  # 比较预测值与标签,并计算精确度(写入数据,进行求精度)
 
    if step % 200 == 0:  # 每200个step,打印一次结果
        print('Step', step, ': Loss is: ', float(loss), ' Accuracy: ', acc_meter.result().numpy())  # 读取数据
        acc_meter.reset_states()  # 清零测量器l

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文