# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + origin_pos=4 tab=["tensorflow"]
import tensorflow as tf

# + [markdown] origin_pos=5
# ## Conv2d(卷积核)
# tf.keras.layers.Conv2D(  
# &nbsp;&nbsp;&nbsp;&nbsp;filters, kernel_size, strides=(1, 1), padding='valid',  
# &nbsp;&nbsp;&nbsp;&nbsp;data_format=None, dilation_rate=(1, 1), groups=1, &nbsp;&nbsp;&nbsp;&nbsp;activation=None,  
# &nbsp;&nbsp;&nbsp;&nbsp;use_bias=True, kernel_initializer='glorot_uniform',  
# &nbsp;&nbsp;&nbsp;&nbsp;bias_initializer='zeros', kernel_regularizer=None,  
# &nbsp;&nbsp;&nbsp;&nbsp;bias_regularizer=None, activity_regularizer=None, &nbsp;&nbsp;&nbsp;&nbsp;kernel_constraint=None,  
# &nbsp;&nbsp;&nbsp;&nbsp;bias_constraint=None, **kwargs  
# )
# -

filters = 1

# + origin_pos=23 tab=["tensorflow"]
# 构造一个二维卷积层，它具有1个输出通道和形状为（1，2）的卷积核
conv2d = tf.keras.layers.Conv2D(filters, (1, 2), use_bias=False)

# +
## Padding
## 'same': keep the size the same
# -

# 注意!!! 这里每边都填充了1行或1列，因此总共添加了2行或2列
conv2d = tf.keras.layers.Conv2D(filters, kernel_size=3, padding='same')

conv2d = tf.keras.layers.Conv2D(filters, kernel_size=(5, 3), padding='same')

# ## stride

conv2d = tf.keras.layers.Conv2D(filters, kernel_size=3, padding='same', strides=2)

conv2d = tf.keras.layers.Conv2D(filters, kernel_size=(3,5), padding='valid',strides=(3, 4))

# ## Pooling layer(汇聚层)
# tf.keras.layers.MaxPool2D(  
# &nbsp;&nbsp;&nbsp;&nbsp;pool_size=(2, 2),  
# &nbsp;&nbsp;&nbsp;&nbsp;strides=None,  
# &nbsp;&nbsp;&nbsp;&nbsp;padding='valid',  
# &nbsp;&nbsp;&nbsp;&nbsp;data_format=None,  
# )

X = tf.reshape(tf.range(16, dtype=tf.float32), (1, 4, 4, 1))
X

# ### 默认情况下，步幅与汇聚窗口的大小相同。 因此，如果我们使用形状为(3, 3)的汇聚窗口，那么默认情况下，我们得到的步幅形状为(3, 3)。

pool2d = tf.keras.layers.MaxPool2D(pool_size=[3, 3])
pool2d(X)

pool2d = tf.keras.layers.MaxPool2D(pool_size=[3, 3], padding='valid',
                                   strides=2)

pool2d = tf.keras.layers.MaxPool2D(pool_size=[2, 3], padding='valid',
                                   strides=(2, 3))

# # Batch Normalization

bn = tf.keras.layers.BatchNormalization()

StarcoderdataJupyterScriptsDedupFiltered