我正在做一个需要使用时域卷积网络的项目,我找到了一个支持方便构建此网络的包:https://github.com/philipperemy/keras-tcn 下面是我用来定义 TCN 模型架构的代码。
!pip install keras-tcn
from tensorflow.keras import Input, Model
from tensorflow.keras.layers import Dense, Softmax, Lambda
from tcn import TCN
# Create the Input Layer
input_layer = Input(shape=(20, 225))
# First TCN Layer - parameters based on the tcn.py file
tcn_layer_1 = TCN(nb_filters=64,
kernel_size=3,
nb_stacks=1,
dilations=(1, 2, 4, 8),
padding='causal',
use_skip_connections=True,
return_sequences=True, # Important: Return sequences for stacked TCN layers
dropout_rate=0.05,
activation='relu',
use_batch_norm=False,
use_layer_norm=False)(input_layer)
# Second TCN Layer - similar parameters as the first layer
tcn_layer_2 = TCN(nb_filters=64,
kernel_size=3,
nb_stacks=1,
dilations=(1, 2, 4, 8),
padding='causal',
use_skip_connections=True,
return_sequences=False, # Final TCN layer, no need to return sequences
dropout_rate=0.05,
activation='relu',
use_batch_norm=False,
use_layer_norm=False)(tcn_layer_1)
# Dense Layer to further process the TCN output
dense_layer = Dense(128, activation='relu')(tcn_layer_2)
# Softmax Output Layer for classification
output_layer = Dense(len(gloss_list), activation='softmax')(dense_layer)
# Compile the model
model = Model(inputs=input_layer, outputs=output_layer)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Print the model summary to verify the architecture
model.summary()
运行上面的代码块后会弹出以下错误。
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-3a050a142399> in <cell line: 9>()
7
8 # First TCN Layer - parameters based on the tcn.py file
----> 9 tcn_layer_1 = TCN(nb_filters=64,
10 kernel_size=3,
11 nb_stacks=1,
1 frames
/usr/local/lib/python3.10/dist-packages/tcn/tcn.py in build(self, input_shape)
314 self.output_slice_index = -1 # causal case.
315 self.slicer_layer = Lambda(lambda tt: tt[:, self.output_slice_index, :], name='Slice_Output')
--> 316 self.slicer_layer.build(self.build_output_shape.as_list())
317
318 def compute_output_shape(self, input_shape):
AttributeError: 'tuple' object has no attribute 'as_list'
最奇怪的是我实际上可以在我的 Macbook M1 Pro 上运行这段代码。
我尝试查看 keras-tcn 包的代码,但一无所获。该错误来自TCN类的下面的方法,特别是来自最后一行代码
self.slicer_layer.build(self.build_output_shape.as_list())
。据我了解, self.build_output_shape
在方法开始时设置为元组 input_shape
。为了使代码能够正常运行,我认为(实际上是 GPT-4o)self.build_output_shape
将在 self.build_output_shape = self.residual_blocks[-1].res_output_shape
行更改为 TensorShape 对象。但对象的类型似乎没有改变,这导致了错误。再说一次,最奇怪的事情是这种情况只发生在 Google Colab 上,而不是我的本地计算机上。
def build(self, input_shape):
# member to hold current output shape of the layer for building purposes
self.build_output_shape = input_shape
# list to hold all the member ResidualBlocks
self.residual_blocks = []
total_num_blocks = self.nb_stacks * len(self.dilations)
if not self.use_skip_connections:
total_num_blocks += 1 # cheap way to do a false case for below
for s in range(self.nb_stacks):
for i, d in enumerate(self.dilations):
res_block_filters = self.nb_filters[i] if isinstance(self.nb_filters, list) else self.nb_filters
self.residual_blocks.append(ResidualBlock(dilation_rate=d,
nb_filters=res_block_filters,
kernel_size=self.kernel_size,
padding=self.padding,
activation=self.activation_name,
dropout_rate=self.dropout_rate,
use_batch_norm=self.use_batch_norm,
use_layer_norm=self.use_layer_norm,
use_weight_norm=self.use_weight_norm,
kernel_initializer=self.kernel_initializer,
name='residual_block_{}'.format(len(self.residual_blocks))))
# build newest residual block
self.residual_blocks[-1].build(self.build_output_shape)
self.build_output_shape = self.residual_blocks[-1].res_output_shape
# this is done to force keras to add the layers in the list to self._layers
for layer in self.residual_blocks:
self.__setattr__(layer.name, layer)
self.output_slice_index = None
if self.padding == 'same':
time = self.build_output_shape.as_list()[1]
if time is not None: # if time dimension is defined. e.g. shape = (bs, 500, input_dim).
self.output_slice_index = int(self.build_output_shape.as_list()[1] / 2)
else:
# It will known at call time. c.f. self.call.
self.padding_same_and_time_dim_unknown = True
else:
self.output_slice_index = -1 # causal case.
self.slicer_layer = Lambda(lambda tt: tt[:, self.output_slice_index, :], name='Slice_Output')
self.slicer_layer.build(self.build_output_shape.as_list())
我降级了它运行的Colab中的tensorflow版本,Colab通常安装版本2.17.0 我用这个代码来降级它 #!pip install tensorflow==2.12 # 安装tensorflow 2.12 将张量流导入为 tf 打印(tf。版本)