2017-08-08 45 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層將是輸入到隨後的層(切片)。