tf 卷积函数 Conv2D & Conv1D

本文通过构建一个多输入多输出的模型,介绍了TensorFlow中Conv2D和Conv1D函数的应用。模型包含两个输入:32x32的RGB图像和包含20个10维单词向量的时间序列数据。经过卷积层处理后,使用全局池化层降低维度,并通过全连接层进行最终的分类和评分预测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tf 卷积函数 Conv2D & Conv1D

import tensorflow as tf

# 每一张图片的尺寸是 32*32,通道数是 3
image_input = tf.keras.Input(shape=(32, 32, 3), name='img_input') 
# 每一个句子20个单词,每个单词向量的长度是10
timeseries_input = tf.keras.Input(shape=(20, 10), name='ts_input') 

# 第1个参数表示过滤器的个数(输出的通道数),第2个参数表示2维过滤器的长和宽(tuple2或者整数),output_shape: (30,30,3)
x1 = tf.keras.layers.Conv2D(3, 3)(image_input) 
x1 = tf.keras.layers.GlobalMaxPooling2D()(x1)  # output_shape: (3,)

# 第1个参数表示过滤器的个数(输出的通道数),第2个参数表示1维过滤器的长和宽(tuple1或者整数),output_shape: (18,3)
x2 = tf.keras.layers.Conv1D(3, 3)(timeseries_input)  
x2 = tf.keras.layers.GlobalMaxPooling1D()(x2)  # output_shape: (3,)

x = tf.keras.layers.concatenate([x1, x2]) # output_shape: (6,)

score_output = tf.keras.layers.Dense(1, name='score_output')(x)  # shape: (1,)
class_output = tf.keras.layers.Dense(5, name='class_output')(x)  # shape: (5,)

model = tf.keras.Model(inputs=[image_input, timeseries_input],
                    outputs=[score_output, class_output])

print(model.summary())

tf.keras.utils.plot_model(model, 'multi_input_and_output_model.png', show_shapes=True,dpi=500)

输出结果:

Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
img_input (InputLayer)          [(None, 32, 32, 3)]  0                                            
__________________________________________________________________________________________________
ts_input (InputLayer)           [(None, 20, 10)]     0                                            
__________________________________________________________________________________________________
conv2d (Conv2D)                 (None, 30, 30, 3)    84          img_input[0][0]                  
__________________________________________________________________________________________________
conv1d (Conv1D)                 (None, 18, 3)        93          ts_input[0][0]                   
__________________________________________________________________________________________________
global_max_pooling2d (GlobalMax (None, 3)            0           conv2d[0][0]                     
__________________________________________________________________________________________________
global_max_pooling1d (GlobalMax (None, 3)            0           conv1d[0][0]                     
__________________________________________________________________________________________________
concatenate (Concatenate)       (None, 6)            0           global_max_pooling2d[0][0]       
                                                                 global_max_pooling1d[0][0]       
__________________________________________________________________________________________________
score_output (Dense)            (None, 1)            7           concatenate[0][0]                
__________________________________________________________________________________________________
class_output (Dense)            (None, 5)            35          concatenate[0][0]                
==================================================================================================
Total params: 219
Trainable params: 219
Non-trainable params: 0
__________________________________________________________________________________________________
None

model-image

### 卷积神经网络中的Conv2DConv1D #### 定义维度差异 `Conv2D` 和 `Conv1D` 是卷积神经网络 (CNN) 中常用的两种卷积操作,分别用于处理二维和一维数据。 `Conv2D` 主要应用于图像处理领域,因为它能够捕捉空间上的特征关系,例如边缘检测、纹理分析等[^1]。而 `Conv1D` 则通常用于序列数据分析,比如时间序列或自然语言处理任务。 #### 数据输入形状 对于 `Conv2D`,其输入通常是三维张量 `(height, width, channels)`,其中高度 (`height`) 和宽度 (`width`) 表示图像的空间尺寸,通道数 (`channels`) 可能表示 RGB 颜色通道或其他多光谱信息。而在 `Conv1D` 的情况下,输入是一个二维张量 `(sequence_length, features)`,这里 `sequence_length` 表示序列长度,`features` 表示每个时间步的特征数量[^2]。 #### 应用场景对比 - **Conv2D**: 常见于计算机视觉任务,如对象识别、语义分割以及风格迁移等。它通过滑动窗口的方式提取局部区域内的模式,并利用共享权重减少参数数量。 - **Conv1D**: 更适合处理具有顺序特性的数据集,例如音频信号波形图或者文本字符编码向量。由于这些类型的数据本质上是一维排列的时间轴上采样点集合,因此采用线性扫描方式即可有效捕获上下文依赖关系。 以下是两个函数的一个简单实现例子: ```python import tensorflow as tf # Example of Conv2D usage on an image-like input input_tensor_2d = tf.random.normal([10, 64, 64, 3]) # Batch size=10, Image Size=(64x64), Channels=RGB(3) conv_layer_2d = tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu') output_conv2d = conv_layer_2d(input_tensor_2d) print(f"Output shape from Conv2D layer: {output_conv2d.shape}") # Example of Conv1D usage on a sequence-like input input_tensor_1d = tf.random.normal([10, 100, 8]) # Batch size=10, Sequence Length=100, Features per step=8 conv_layer_1d = tf.keras.layers.Conv1D(filters=16, kernel_size=5, activation='relu') output_conv1d = conv_layer_1d(input_tensor_1d) print(f"Output shape from Conv1D layer: {output_conv1d.shape}") ``` 上述代码展示了如何定义并应用这两种类型的卷积层到不同结构的数据集中去。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值