从 Conv1D 层到 MaxPool1D 层的额外维度
Posted
技术标签:
【中文标题】从 Conv1D 层到 MaxPool1D 层的额外维度【英文标题】:Extra dimension to MaxPool1D layer from Conv1D layer 【发布时间】:2022-01-16 13:17:39 【问题描述】:我对 Tensorflow 很陌生(这是我第一个使用它的项目),我不太了解输入形状的工作原理。我正在尝试在一组金融时间序列数据上训练 CNN-LSTM。
对于我的用例,我有一个 tf.keras.data.DataLoader
对象,用于为模型提供批量训练数据。
一个训练实例对应于过去 30 天的价格历史记录,因此形状应为 (30,)
。
运行以下代码:
for x, y in train_ds:
print(x, y)
print(x.shape)
break
我知道x.shape
是(4, 30)
,其中我定义的Dataset
对象以4 个批次为训练实例提供服务。
这是我的代码:
# driver code for experiments
import keras
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from keras import layers
WIDTH = 30
BATCH_SIZE = 4
# load datasets (prepended with 'n' for 'normalized' )
nXtrain = np.load('cad_90s_nXtrain.npy')
nytrain = np.load('cad_90s_nytrain.npy')
nXval = np.load('cad_90s_nXval.npy')
nyval = np.load('cad_90s_nyval.npy')
nXtest = np.load('cad_90s_nXtest.npy')
nytest = np.load('cad_90s_nytest.npy')
# instantiate tensorflow Datasets
train_ds = tf.data.Dataset.from_tensor_slices((nXtrain, nytrain)).batch(BATCH_SIZE)
val_ds = tf.data.Dataset.from_tensor_slices((nXval, nyval)).batch(BATCH_SIZE)
test_ds = tf.data.Dataset.from_tensor_slices((nXtest, nytest)).batch(BATCH_SIZE)
input_shape = (BATCH_SIZE, WIDTH, 1 )
testnet = tf.keras.Sequential([
layers.InputLayer(input_shape=input_shape),
layers.Conv1D(filters=32,
kernel_size=3,
activation='tanh',
padding='same',
strides=1),
layers.MaxPool1D(pool_size=2,
padding='same'),
layers.ReLU(),
layers.LSTM(units=64, dropout=0.2, activation='tanh'),
layers.Dense(units=1)
])
testnet.build()
testnet.summary()
附带错误消息:
ValueError: Input 0 of layer "max_pooling1d" is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 4, 30, 32)
我不明白发生了什么——为什么Conv1D
层会出现一个额外的维度?我的意思是,一维卷积的输出不应该是
(BATCH_SIZE, WIDTH, 32)
(填充设置为'same'
)?
如果文档中解决了这个问题,我深表歉意,但我一直在到处寻找答案,但我似乎无法解决这个问题。我真的很感谢这里的一些帮助。
谢谢!
【问题讨论】:
input
形状不应包含批次维度;只使用(WIDTH, 1)
。这在文档中得到解决吗?我实际上不确定这是否在任何地方明确呈现。绝对是新手最常见的问题之一。
谢谢,这解决了问题——我确定我已经尝试过了!
【参考方案1】:
正如@xdurch0 所说,tf.keras.layers.InputLayer 不采用 Batch_size。
input_shape:形状元组(不包括批处理轴),或 TensorShape 实例(不包括批处理轴)。
有关信息,请参阅link
工作示例代码
import tensorflow as tf
input_shape = (4, 1)
x = tf.random.normal(input_shape)
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.InputLayer(input_shape=input_shape))
model.add(tf.keras.layers.Conv1D(filters=32,
kernel_size=3,
activation='tanh',
padding='same',
strides=1),)
model.add(tf.keras.layers.MaxPool1D(pool_size=2,
padding='same'))
output = model(x)
输出
<tf.Tensor: shape=(4, 1, 32), dtype=float32, numpy=
array([[[ 0.4607179 , 0.15637583, -0.5004345 , -0.07796285,
0.30203223, 0.45201266, 0.01206997, 0.49042067,
0.190878 , -0.06044334, 0.35961443, -0.2021137 ,
-0.49953443, -0.03703883, 0.31947324, 0.10558284,
-0.4071377 , -0.17169577, 0.41974816, 0.35714787,
-0.1540432 , 0.16283943, -0.38012537, 0.08710621,
0.5129782 , -0.38887107, 0.06570872, 0.12256396,
-0.2707871 , 0.31438676, 0.08643996, 0.02944363]],
[[ 0.15212363, 0.04848025, -0.16761488, -0.024035 ,
0.09564026, 0.1488167 , 0.00371435, 0.16364299,
0.05939662, -0.01862026, 0.11532419, -0.06297952,
-0.16725594, -0.01140236, 0.10152339, 0.03260009,
-0.13221109, -0.05331242, 0.13680711, 0.11446481,
-0.04774643, 0.05051624, -0.12253318, 0.02686608,
0.17265812, -0.12564273, 0.02024639, 0.03788798,
-0.08525081, 0.09980103, 0.02665966, 0.00906281]],
[[-0.15694636, -0.05004002, 0.17290986, 0.02480924,
-0.09870265, -0.15353796, -0.00383405, -0.16881739,
-0.06130603, 0.01922018, -0.11900602, 0.06500347,
0.17254004, 0.01176979, -0.1047715 , -0.03364989,
0.13641952, 0.05502706, -0.14115801, -0.11811972,
0.04928267, -0.05214129, 0.1264404 , -0.02773144,
-0.17810565, 0.12964691, -0.02089867, -0.03910775,
0.08798415, -0.1029948 , -0.02751837, -0.00935485]],
[[ 0.25630587, 0.08277144, -0.281522 , -0.04108214,
0.16258071, 0.25089246, 0.0063511 , 0.27507976,
0.10133278, -0.03183164, 0.19552334, -0.10741517,
-0.28094047, -0.01949524, 0.17245385, 0.05570482,
-0.22355829, -0.09099286, 0.2311481 , 0.19409074,
-0.08152226, 0.0862364 , -0.20751894, 0.04591698,
0.28967807, -0.21268068, 0.03461014, 0.06472494,
-0.14509352, 0.16956565, 0.04556449, 0.01549565]]],
dtype=float32)>
【讨论】:
以上是关于从 Conv1D 层到 MaxPool1D 层的额外维度的主要内容,如果未能解决你的问题,请参考以下文章