2017-08-08 59 views
0

我试图创建在自定义过滤器与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')) 

我试图切片沿着特征尺寸输入张量分裂(人工小)输入keras卷积网络与两个单位的过滤器一起使用。在这个例子中,过滤器是一个单独的LSTM单元。我希望能够使用任意模型代替LSTM。

然而,这种失败的model = ...线:如果LSTMDense替换发生

--------------------------------------------------------------------------- 
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' 

相同的问题。这个错误消息意味着什么,这远远不清楚。我究竟做错了什么?

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

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

+0

我认为问题是你必须使用嵌入连接LSTM层。你见过[这篇文章](https://stackoverflow.com/questions/41052494/how-to-merge-two-lstm-layers-in-keras#41175522)? – rll

+0

@rll感谢您的提示。然而,对网络的输入将是时间序列(实值),嵌入似乎主要用于单热型输入。另外,我描述的问题也发生在''LSTM''被''Dense''取代(我将更新问题以包含该信息)。 – josteinb

回答

1

的问题就出在输​​入切片的方式。 LSTM层预计将有一个Layer对象作为输入,并且您正在输入Tensor对象。你可以尝试添加一个lambda层(或者在这个例子中是两个),这个层为输入LSTM层而对输入进行分片。类似:

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

y层将是输入到随后的层(切片)。