图形已断开连接:无法在“ input_1”层获取张量Tensor()的值

时间:2020-04-11 00:55:48

标签: tensorflow keras graph neural-network disconnected

此问题的代码非常复杂,因为我正在尝试实现fractalNet,但是将卷积基础块更改为仅一个密集层。我试图分别构建两个fractalNet(一个接一个,所以我不认为它们会造成干扰)。一种用于策略,另一种用于价值功能。

到目前为止,我看到的许多问题可能也可能不相关。一种是我不能将numpy导入为np并使用np,这就是为什么我不得不使用numpy()的原因。另一个是我的代码似乎试图同时在不同部分中的张量tf.Tensor[stuff]Tensor[stuff]上工作。下面的build_model函数从Input调用输出Tensor[stuff],而神经网络构建器代码使用tf.Tensor[stuff]。我尝试过,但仍坚持打字无济于事。

这是不断杀死代码的完整错误:

/home/ryan/.local/lib/python3.6/site-packages/keras/engine/network.py:190: UserWarning: Model inputs must come from `keras.layers.Input` (thus holding past layer metadata), they cannot be the output of a previous non-Input layer. Here, a tensor specified as input to your model was not an Input tensor, it was generated by layer activation_1.
Note that input tensors are instantiated via `tensor = keras.layers.Input(shape)`.
The tensor that caused the issue was: activation_1/Relu:0
  str(x.name))
Traceback (most recent call last):
  File "train.py", line 355, in <module>
    main(**vars(args))
  File "train.py", line 302, in main
    val_func = NNValueFunction(bl,c,layersizes,dropout,deepest,obs_dim) # Initialize the value function
  File "/home/ryan/trpo_fractalNN/trpo/value.py", line 37, in __init__
    self.model = self._build_model()
  File "/home/ryan/trpo_fractalNN/trpo/value.py", line 56, in _build_model
    model = Model(inputs=obs_input, outputs=outputs)
  File "/home/ryan/.local/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/home/ryan/.local/lib/python3.6/site-packages/keras/engine/network.py", line 94, in __init__
    self._init_graph_network(*args, **kwargs)
  File "/home/ryan/.local/lib/python3.6/site-packages/keras/engine/network.py", line 241, in _init_graph_network
    self.inputs, self.outputs)
  File "/home/ryan/.local/lib/python3.6/site-packages/keras/engine/network.py", line 1511, in _map_graph_network
    str(layers_with_complete_input))
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(None, 29), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: []

所以这是我现在很怀疑的代码部分,因为它在值函数的神经网络的一开始就以某种方式中断了。

def _build_model(self):
    """ Construct TensorFlow graph, including loss function, init op and train op """
    # hid1 layer size is 10x obs_dim, hid3 size is 10, and hid2 is geometric mean
    # hid3_units = 5  # 5 chosen empirically on 'Hopper-v1'
    # hid2_units = int(np.sqrt(hid1_units * hid3_units))
    # heuristic to set learning rate based on NN size (tuned on 'Hopper-v1')

    obs = keras.layers.Input(shape=(self.obs_dim,))
    # I'm not sure why it won't work with np??????????????????????????????????????????????????????????????????????????????????
    obs_input = Dense(int(self.layersizes[0][0].numpy()))(obs) # Initial fully-connected layer that brings obs number up to a len that will work with fractal architecture
    obs_input = Activation('relu')(obs_input)
    self.lr = 1e-2 / np.sqrt(self.layersizes[2][0])  # 1e-2 empirically determined
    print('Value Params -- lr: {:.3g}'
          .format(self.lr))
    outputs = fractal_net(self,bl=self.bl,c=self.c,layersizes=self.layersizes,
        drop_path=0.15,dropout=self.dropout,
        deepest=self.deepest)(obs_input)
    model = Model(inputs=obs_input, outputs=outputs)
    optimizer = Adam(self.lr)
    model.compile(optimizer=optimizer, loss='mse')



    return model

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。问题在于,由于我尝试合并多个文件,因此我进行了“密集”调用,将obs_len调整为所需的大小,然后将其取值并将其插入fractalNet代码。但是,我没有意识到这会破坏事情。我通过删除最初的Dense调用并将其放在fractalNet代码本身中来解决了这个问题。

从故事的角度讲,不要试图将NN层的不同部分分解为单独的文件。就像附带说明一样,在当前fractalNN代码中,它先调用fractal_net,然后再调用Dense层,显然这仍然有效。但是我认为尝试扭转这种顺序是很麻烦的。我希望这对其他人有帮助。