0

我正在尝试在我的模型中实现嘈杂的网络。我在 GitHub 上找到了一个代码,它是NoisyDense Class的一个实现。我在我的模型中使用了这个类。这里的代码: -


class Agent:

    def __init__(self, state_size, strategy="t-dqn", reset_every=1000, pretrained=False, model_name=None):
        self.strategy = strategy

        # agent config
        self.state_size = state_size        
        self.action_size = 3                
        self.model_name = model_name
        self.inventory = []
        self.buffer = []
        self.first_iter = True
        self.nstep = 5
        self.n_step_buffer = deque(maxlen = self.nstep)
        self.cnt = count()
        self.alpha = 0.6
        self.model_name = model_name
        self.gamma = 0.95 # affinity for long term reward
        self.epsilon = 1.0
        self.epsilon_min = 0.01
        self.epsilon_decay = 0.995
        self.learning_rate = 0.001
        self.loss = huber_loss
        self.custom_objects = {"huber_loss": huber_loss}  # important for loading the model from memory
        self.optimizer = Adam(lr=self.learning_rate)

        if pretrained and self.model_name is not None:
            self.model = self.load()
        else:
            self.model = self._model()

        # strategy config
        if self.strategy in ["t-dqn", "double-dqn"]:
            self.n_iter = 1
            self.reset_every = reset_every

            # target network
            self.target_model = clone_model(self.model)
            self.target_model.set_weights(self.model.get_weights())
    def _model(self):
         model = Sequential()
         model.add(Dense(units=128, activation="relu", input_dim=self.state_size))
         model.add(Dense(units=256, activation="relu"))
         model.add(Dense(units=256, activation="relu"))
         model.add(NoisyDense(128, activation="relu"))
         model.add(NoisyDense(self.action_size + 1, activation='linear'))
         model.compile(loss=self.loss, optimizer=self.optimizer)
         return model

但是遇到了一个错误,我似乎不知道如何解决它,也检查了 NoisyDense 的实现。我在 StackOverflow 上寻找了类似的问题,但没有得到任何适用于我的代码的解决方案。此外,当我不调用 NoisyDense 类而是使用 Dense 层时,相同的模型也有效。回溯中的 Train.py 文件调用了代理类,我猜没有什么比这更重要的了。使用的模型是 Keras 模型,它的版本是 2.3.1

Traceback (most recent call last):
  File "train.py", line 85, in <module>
    pretrained=pretrained, debug=debug)
  File "train.py", line 52, in main
    agent = Agent(window_size, strategy=strategy, pretrained=pretrained, model_name=model_name)
  File "C:\Users\Desktop\agent.py", line 158, in __init__
    self.target_model = clone_model(self.model)
  File "D:\anaconda\lib\site-packages\keras\models.py", line 255, in clone_model
    return _clone_sequential_model(model, input_tensors=input_tensors)
  File "D:\anaconda\lib\site-packages\keras\models.py", line 207, in _clone_sequential_model
    layers = [clone(layer) for layer in model.layers]
  File "D:\anaconda\lib\site-packages\keras\models.py", line 207, in <listcomp>
    layers = [clone(layer) for layer in model.layers]
  File "D:\anaconda\lib\site-packages\keras\models.py", line 205, in clone
    return layer.__class__.from_config(layer.get_config())
  File "D:\anaconda\lib\site-packages\keras\engine\base_layer.py", line 1179, in from_config
    return cls(**config)
TypeError: __init__() missing 1 required positional argument: 'units'
4

2 回答 2

0

感谢您抽出宝贵时间,但我从 StackOverflow 上的类似问题中得到了答案

“ValueError: Unknown layer: ...” 使用 Tensorflow 调用 copy.deepcopy(network) 时

于 2020-03-04T11:11:37.537 回答
0

如您所见,Github 上的 NoisyDense 类将单位作为参数。因为你没有给出这个论点。这就是显示错误的原因

于 2020-03-03T06:55:30.537 回答