Python ValueError:图形已断开连接:无法获取keras中张量的值,如何修复?

Python ValueError:图形已断开连接:无法获取keras中张量的值,如何修复?,python,tensorflow,keras,Python,Tensorflow,Keras,我尝试构建一个看起来像编码器-解码器模型的模型,但不同之处在于我在两个for循环中使用相同的LSTM模型 为了合并t-1和t-2中的隐藏状态,我构建了一个名为MyLayer的中间层。但以下错误总是会引起: ValueError: Graph disconnected: cannot obtain value for tensor Tensor("lstm_layer_18/Identity_1:0", shape=(8, 128), dtype=float32) at lay

我尝试构建一个看起来像编码器-解码器模型的模型,但不同之处在于我在两个for循环中使用相同的LSTM模型

为了合并t-1和t-2中的隐藏状态,我构建了一个名为
MyLayer
的中间层。但以下错误总是会引起:

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("lstm_layer_18/Identity_1:0", shape=(8, 128), dtype=float32) at layer "my_layer". The following previous layers were accessed without issue: ['input_1', 'embedding', 'input_3', 'input_2', 'lambda1', 'reshape1']
我整个下午都在试着调试,但没能解决

我发现大多数答案都是关于避免中间参数的输入层名称。但这种方法对我不起作用

我试图在错误中打印出
“lstm\u layer\u 18/Identity\u 1:0”
,这是第二个for循环中的
状态。但我无法修复它。有人能帮我找到错误吗?我的代码如下所示

class MyLayer(Layer):

    def __init__(self, output_dim, **kwargs):
        self.output_dim = output_dim
        super(MyLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        assert isinstance(input_shape, list)
        # 为该层创建一个可训练的权重
        self.kernel_1 = self.add_weight(name='kernel_1',
                                        shape=input_shape[0],
                                        initializer='uniform',
                                        trainable=True)

        self.kernel_2 = self.add_weight(name='kernel_2',
                                        shape=input_shape[1],
                                        initializer='uniform',
                                        trainable=True)

        super(MyLayer, self).build(input_shape)  # 一定要在最后调用它

    def call(self, v):
        assert isinstance(v, list)
        a, b = v
        return Add()([Multiply()([a, self.kernel_1]),
                      Multiply()([b, self.kernel_2])])

    def compute_output_shape(self, input_shape):
        assert isinstance(input_shape, list)
        shape_a, shape_b = input_shape
        assert shape_a == shape_b
        return shape_a


def cf_lstm_model(input_step, output_step, features=2, embed_dim=64, lstm_dim=128, batch_size=8):
    # input_shape:(batch_size, input_step, features)
    x = Input(batch_shape=(batch_size, input_step, features))
    h_1 = Input(batch_shape=(batch_size, lstm_dim))
    h_2 = Input(batch_shape=(batch_size, lstm_dim))
    c = Input(batch_shape=(batch_size, lstm_dim))
    state_c = c

    embedding = Dense(embed_dim, activation='relu', name='embedding')
    inverse_embedding = Dense(features, activation='relu', name='inverse_embedding')
    lstm_layer = LSTM(lstm_dim, activation='relu', return_state=True, name='lstm_layer')
    cascade = MyLayer(lstm_dim)
    # decoder = LSTM(lstm_dim, activation='relu', return_state=True, name='decoder')

    # embeding ---> (batch_size, input_step, embed_dim)
    emb_x = embedding(x)

    # cascade_input
    cascade_input = [h_2, h_1]

    # Observe
    for i in range(input_step - 1):
        h_star = cascade(cascade_input)
        m = Lambda(lambda xx: xx[:, i, :], name='lambda1')(emb_x)
        m1 = Reshape((1, embed_dim), name='reshape1')(m)
        lstm1, state_h, state_c = lstm_layer(m1, [h_star, state_c])
        cascade_input.pop()
        cascade_input.append(state_h)

    out = Lambda(lambda xx: xx[:, input_step - 1, :], name='lambda2')(emb_x)
    out = Reshape((1, embed_dim), name='reshape2')(out)
    # Predict
    for j in range(output_step):
        h_star = cascade(cascade_input)
        out, state_h, state_c = lstm_layer(out, [h_star, state_c])
        # print('state_h', state_h)
        print('state_c', state_c)

        out = inverse_embedding(out)
        if j == 0:
            all_outputs = Reshape((1, features), name='reshape3')(out)
        #             print('output shape:', out.shape, K.int_shape(out))
        else:
            # 注意axis的纬度
            all_outputs = K.concatenate([all_outputs, Reshape((1, features))(out)], axis=1)
        #             print('output shape:', K.int_shape(outputs))
        cascade_input.pop()
        cascade_input.append(state_h)
        out = embedding(out)
        out = Reshape((1, embed_dim), name='reshape4')(out)
    #         print('out', out.shape)

    return Model(inputs=[x, h_1, h_2, c], outputs=all_outputs)


cf_lstm = cf_lstm_model(input_step=8,
                        output_step=12,
                        embed_dim=64,
                        lstm_dim=128)

错误反馈

state_c Tensor("lstm_layer_7/Identity_2:0", shape=(8, 128), dtype=float32)
state_c Tensor("lstm_layer_8/Identity_2:0", shape=(8, 128), dtype=float32)
state_c Tensor("lstm_layer_9/Identity_2:0", shape=(8, 128), dtype=float32)
state_c Tensor("lstm_layer_10/Identity_2:0", shape=(8, 128), dtype=float32)
state_c Tensor("lstm_layer_11/Identity_2:0", shape=(8, 128), dtype=float32)
state_c Tensor("lstm_layer_12/Identity_2:0", shape=(8, 128), dtype=float32)
state_c Tensor("lstm_layer_13/Identity_2:0", shape=(8, 128), dtype=float32)
state_c Tensor("lstm_layer_14/Identity_2:0", shape=(8, 128), dtype=float32)
state_c Tensor("lstm_layer_15/Identity_2:0", shape=(8, 128), dtype=float32)
state_c Tensor("lstm_layer_16/Identity_2:0", shape=(8, 128), dtype=float32)
state_c Tensor("lstm_layer_17/Identity_2:0", shape=(8, 128), dtype=float32)
state_c Tensor("lstm_layer_18/Identity_2:0", shape=(8, 128), dtype=float32)

Traceback (most recent call last):
  File "/Users/duoshaoshishi/Desktop/Pedestrian prediction/CF-lstm/model.py", line 105, in <module>
    cf_lstm = cf_lstm_model(input_step=8,
  File "/Users/duoshaoshishi/Desktop/Pedestrian prediction/CF-lstm/model.py", line 102, in cf_lstm_model
    return Model(inputs=[x, h_1, h_2, c], outputs=all_outputs)
  File "/Users/duoshaoshishi/opt/anaconda3/envs/tf2/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 167, in __init__
    super(Model, self).__init__(*args, **kwargs)
  File "/Users/duoshaoshishi/opt/anaconda3/envs/tf2/lib/python3.8/site-packages/tensorflow/python/keras/engine/network.py", line 173, in __init__
    self._init_graph_network(*args, **kwargs)
  File "/Users/duoshaoshishi/opt/anaconda3/envs/tf2/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py", line 456, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/Users/duoshaoshishi/opt/anaconda3/envs/tf2/lib/python3.8/site-packages/tensorflow/python/keras/engine/network.py", line 306, in _init_graph_network
    nodes, nodes_by_depth, layers, _ = _map_graph_network(
  File "/Users/duoshaoshishi/opt/anaconda3/envs/tf2/lib/python3.8/site-packages/tensorflow/python/keras/engine/network.py", line 1787, in _map_graph_network
    raise ValueError('Graph disconnected: '
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("lstm_layer_18/Identity_1:0", shape=(8, 128), dtype=float32) at layer "my_layer". The following previous layers were accessed without issue: ['input_1', 'embedding', 'input_3', 'input_2', 'lambda1', 'reshape1']
state_c张量(“lstm_layer_7/Identity_2:0”,shape=(8128),dtype=float32)
状态张量(“lstm_层_8/Identity_2:0”,shape=(8128),dtype=float32)
状态张量(“lstm_层_9/Identity_2:0”,shape=(8128),dtype=float32)
状态张量(“lstm_层_10/Identity_2:0”,shape=(8128),dtype=float32)
状态张量(“lstm_层_11/Identity_2:0”,shape=(8128),dtype=float32)
状态张量(“lstm_层_12/Identity_2:0”,shape=(8128),dtype=float32)
状态张量(“lstm_层_13/Identity_2:0”,shape=(8128),dtype=float32)
状态张量(“lstm_层_14/Identity_2:0”,shape=(8128),dtype=float32)
状态张量(“lstm_层_15/Identity_2:0”,形状=(8128),数据类型=float32)
状态张量(“lstm_层_16/Identity_2:0”,shape=(8128),dtype=float32)
状态张量(“lstm_层_17/Identity_2:0”,shape=(8128),dtype=float32)
状态张量(“lstm_层_18/Identity_2:0”,shape=(8128),dtype=float32)
回溯(最近一次呼叫最后一次):
文件“/Users/duoshaoshishi/Desktop/people prediction/CF lstm/model.py”,第105行,在
cf_lstm=cf_lstm_模型(输入步骤=8,
文件“/Users/duoshaoshishi/Desktop/people prediction/CF lstm/model.py”,第102行,在CF_lstm_模型中
返回模型(输入=[x,h_1,h_2,c],输出=所有输出)
文件“/Users/duoshaoshishi/opt/anaconda3/envs/tf2/lib/python3.8/site packages/tensorflow/python/keras/engine/training.py”,第167行,在__
超级(模型,自身)。\uuuuu初始值(*args,**kwargs)
文件“/Users/duoshaoshishi/opt/anaconda3/envs/tf2/lib/python3.8/site packages/tensorflow/python/keras/engine/network.py”,第173行,在__
自初始化图网络(*args,**kwargs)
文件“/Users/duoshaoshishi/opt/anaconda3/envs/tf2/lib/python3.8/site packages/tensorflow/python/training/tracking/base.py”,第456行,在方法包装中
结果=方法(自身、*args、**kwargs)
文件“/Users/duoshaoshishi/opt/anaconda3/envs/tf2/lib/python3.8/site packages/tensorflow/python/keras/engine/network.py”,第306行,在网络的初始图中
节点,节点,按深度,层,映射图网络(
文件“/Users/duoshaoshishi/opt/anaconda3/envs/tf2/lib/python3.8/site packages/tensorflow/python/keras/engine/network.py”,第1787行,在网络图中
raise VALUE ERROR('图形已断开连接:'
ValueError:Graph disconnected:无法获取层“my_layer”处的张量张量值(“lstm_layer_18/Identity_1:0”,shape=(8128),dtype=float32)。访问以下以前的层时没有问题:['input_1','Embedded','input_3','input_2',lambda1','Reforme1']