python - 使用 keras 的卷积网络中的自定义过滤器

标签 python machine-learning neural-network keras

我正在尝试使用 keras 创建一个卷积网络,其中

from keras.layers import Input, LSTM, concatenate
from keras.models import Model
from keras.utils.vis_utils import model_to_dot
from IPython.display import display, SVG


inputs = Input(shape=(None, 4))
filter_unit = LSTM(1)
conv = concatenate([filter_unit(inputs[..., 0:2]),
                    filter_unit(inputs[..., 2:4])])
model = Model(inputs=inputs, outputs=conv)
SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))

我尝试沿特征维度对输入张量进行切片,以分割(人为较小的)输入,以便与滤波器的两个单元一起使用。在示例中,过滤器是单个 LSTM 单元。我希望能够使用任意模型来代替 LSTM。

但是,这在 model = ... 行失败:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-a9f7f2ffbe17> in <module>()
      9 conv = concatenate([filter_unit(inputs[..., 0:2]),
     10                     filter_unit(inputs[..., 2:4])])
---> 11 model = Model(inputs=inputs, outputs=conv)
     12 SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))

~/.local/opt/anaconda3/envs/trafficprediction/lib/python3.6/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     86                 warnings.warn('Update your `' + object_name +
     87                               '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 88             return func(*args, **kwargs)
     89         wrapper._legacy_support_signature = inspect.getargspec(func)
     90         return wrapper

~/.local/opt/anaconda3/envs/trafficprediction/lib/python3.6/site-packages/keras/engine/topology.py in __init__(self, inputs, outputs, name)
   1703         nodes_in_progress = set()
   1704         for x in self.outputs:
-> 1705             build_map_of_graph(x, finished_nodes, nodes_in_progress)
   1706 
   1707         for node in reversed(nodes_in_decreasing_depth):

~/.local/opt/anaconda3/envs/trafficprediction/lib/python3.6/site-packages/keras/engine/topology.py in build_map_of_graph(tensor, finished_nodes, nodes_in_progress, layer, node_index, tensor_index)
   1693                 tensor_index = node.tensor_indices[i]
   1694                 build_map_of_graph(x, finished_nodes, nodes_in_progress,
-> 1695                                    layer, node_index, tensor_index)
   1696 
   1697             finished_nodes.add(node)

~/.local/opt/anaconda3/envs/trafficprediction/lib/python3.6/site-packages/keras/engine/topology.py in build_map_of_graph(tensor, finished_nodes, nodes_in_progress, layer, node_index, tensor_index)
   1693                 tensor_index = node.tensor_indices[i]
   1694                 build_map_of_graph(x, finished_nodes, nodes_in_progress,
-> 1695                                    layer, node_index, tensor_index)
   1696 
   1697             finished_nodes.add(node)

~/.local/opt/anaconda3/envs/trafficprediction/lib/python3.6/site-packages/keras/engine/topology.py in build_map_of_graph(tensor, finished_nodes, nodes_in_progress, layer, node_index, tensor_index)
   1663             """
   1664             if not layer or node_index is None or tensor_index is None:
-> 1665                 layer, node_index, tensor_index = tensor._keras_history
   1666             node = layer.inbound_nodes[node_index]
   1667 

AttributeError: 'Tensor' object has no attribute '_keras_history'

如果将LSTM替换为Dense,也会出现同样的问题。我还不清楚这个错误消息的含义。我做错了什么?

有一个关于同一错误的问题(下面的链接),但我不清楚应该如何使用 Lambda 层,或者这是否是正确的解决方案。

AttributeError: 'Tensor' object has no attribute '_keras_history'

最佳答案

问题在于输入的切片方式。 LSTM 层需要一个 Layer 对象作为输入,而您正在输入一个 Tensor 对象。您可以尝试添加一个 lambda 层(或示例中的两个)来对输入进行切片,以便为 LSTM 层提供数据。像这样的东西:

y = Lambda(lambda x: x[:,0,:,:], output_shape=(1,) + input_shape[2:])(x)

这个y层将是后续层的(切片)输入。

关于python - 使用 keras 的卷积网络中的自定义过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45569938/

相关文章:

python - 有没有办法在启动新对象时返回先前定义的对象?

python - 抑制 libsvm 中的输出(python)

python - 在 PyTorch 中创建具有多个 channel 的简单一维 CNN

python-3.x - bert层中的池化输出和序列输出有什么区别?

python - 如何从扭曲的 inlineCallbacks 修饰函数中捕获异常?

Python - 如何清除文本中的空格

python - 为什么当我按下 ctrl-c 时 Tornado 会花这么长时间才能死掉?

machine-learning - 训练集只有一个标签,缺少另一个标签

python - 查找原始特征对用作内核 PCA 中输入的主成分的影响

python - 了解 scikit 神经网络参数