您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“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.Dense()
:密集連接層。參數個數 = 輸入層特征數× 輸出層特征數(weight)+ 輸出層特征數(bias)
tf.keras.layers.Activation()
:激活函數層。一般放在Dense層后面,等價于在Dense層中指定activation。
tf.keras.layers.Dropout()
:隨機置零層。訓練期間以一定幾率將輸入置0,一種正則化手段。
tf.keras.layers.BatchNormalization()
:批標準化層。通過線性變換將輸入批次縮放平移到穩定的均值和標準差。可以增強模型對輸入不同分布的適應性,加快模型訓練速度,有輕微正則化效果。一般在激活函數之前使用。
tf.keras.layers.SpatialDropout2D()
:空間隨機置零層。訓練期間以一定幾率將整個特征圖置0,一種正則化手段,有利于避免特征圖之間過高的相關性。
tf.keras.layers.Input()
:輸入層。通常使用Functional API方式構建模型時作為第一層。
tf.keras.layers.DenseFeature()
:特征列接入層,用于接收一個特征列列表并產生一個密集連接層。
tf.keras.layers.Flatten()
:壓平層,用于將多維張量壓成一維。
tf.keras.layers.Reshape()
:形狀重塑層,改變輸入張量的形狀。
tf.keras.layers.Concatenate()
:拼接層,將多個張量在某個維度上拼接。
tf.keras.layers.Add()
:加法層。
tf.keras.layers.Subtract()
:減法層。
tf.keras.layers.Maximum()
:取最大值層。
tf.keras.layers.Minimum()
:取最小值層。
tf.keras.layers.Conv1D()
:普通一維卷積,常用于文本。參數個數 = 輸入通道數×卷積核尺寸(如3)×卷積核個數
tf.keras.layers.Conv2D()
:普通二維卷積,常用于圖像。參數個數 = 輸入通道數×卷積核尺寸(如3乘3)×卷積核個數
tf.keras.layers.Conv3D()
:普通三維卷積,常用于視頻。參數個數 = 輸入通道數×卷積核尺寸(如3乘3乘3)×卷積核個數
tf.keras.layers.SeparableConv2D()
:二維深度可分離卷積層。不同于普通卷積同時對區域和通道操作,深度可分離卷積先操作區域,再操作通道。即先對每個通道做獨立卷即先操作區域,再用1乘1卷積跨通道組合即再操作通道。參數個數 = 輸入通道數×卷積核尺寸 + 輸入通道數×1×1×輸出通道數。深度可分離卷積的參數數量一般遠小于普通卷積,效果一般也更好。
tf.keras.layers.DepthwiseConv2D()
:二維深度卷積層。僅有SeparableConv2D前半部分操作,即只操作區域,不操作通道,一般輸出通道數和輸入通道數相同,但也可以通過設置depth_multiplier讓輸出通道為輸入通道的若干倍數。輸出通道數 = 輸入通道數 × depth_multiplier。參數個數 = 輸入通道數×卷積核尺寸× depth_multiplier。
tf.keras.layers.Conv2DTranspose()
:二維卷積轉置層,俗稱反卷積層。并非卷積的逆操作,但在卷積核相同的情況下,當其輸入尺寸是卷積操作輸出尺寸的情況下,卷積轉置的輸出尺寸恰好是卷積操作的輸入尺寸。
tf.keras.layers.LocallyConnected2D()
:二維局部連接層。類似Conv2D,唯一的差別是沒有空間上的權值共享,所以其參數個數遠高于二維卷積。
tf.keras.layers.MaxPooling2D()
:二維最大池化層。也稱作下采樣層。池化層無參數,主要作用是降維。
tf.keras.layers.AveragePooling2D()
:二維平均池化層。
tf.keras.layers.GlobalMaxPool2D()
:全局最大池化層。每個通道僅保留一個值。一般從卷積層過渡到全連接層時使用,是Flatten的替代方案。
tf.keras.layers.GlobalAvgPool2D()
:全局平均池化層。每個通道僅保留一個值。
示例代碼一、搭建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
讀到這里,這篇“tf.keras.layers模塊中的函數有哪些”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。