1

这个问题的代码非常复杂,因为我正在尝试实现 fractalNet,但将卷积基础块更改为密集层。我正在尝试分别构建两个分形网络(一个接一个,所以我认为它们不应该干扰)。一个用于策略,一个用于价值函数。

到目前为止,我还看到了许多可能相关也可能不相关的问题。一是我不能将 numpy 作为 np 导入并使用 np,这就是我被迫使用 numpy() 的原因。另一个是我的代码似乎试图同时处理张量tf.Tensor[stuff]以及Tensor[stuff]不同部分。下面的 build_model 函数Tensor[stuff]从 Input 调用输出,而神经网络构建器代码使用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
4

1 回答 1

0

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

故事的寓意是,不要试图将 NN 层的不同部分分解为单独的文件。顺便说一句,在当前的 fractalNN 代码中,它调用了 fractal_net,然后调用了 Dense 层,显然这仍然有效。但我认为试图颠倒这个顺序会破坏事情。我希望这对其他人有帮助。

于 2020-04-13T19:08:14.623 回答