当我运行下面的代码时,我得到了一个错误。
def __init__(self, model, sess, loss_fn=None):
"""
To generate the White-box Attack Agent.
:param model: the target model which should have the input tensor, the target tensor and the loss tensor.
:param sess: the tensorflow session.
:param loss_fn: None if using original loss of the model.
"""
self.model = model
self.input_tensor = model.inputs[0]
self.output_tensor = model.outputs[0]
self.target_tensor = model.targets[0]
self._sample_weights = model.sample_weights[0]
if loss_fn is None:
self.loss_tensor = model.total_loss
self.gradient_tensor = K.gradients(self.loss_tensor, self.input_tensor)[0]
else:
self.set_loss_function(loss_fn)
self.sess = sess错误:
self.target_tensor = model.targets[0] , AttributeError: 'Model' object has no attribute 'targets'我正在使用Tensorflow 1.14.0,keras 2.2.4-tf和python 3.6.13,我该如何解决这个问题?
谢谢
发布于 2021-05-14 03:50:45
你的函数正在使用的model参数显然是在谈论一个唯一的类,而这个类不是默认的Keras或Tensorflow的一部分。您需要确保传递给函数的model对象是相应类的实例。或者,您可以通过执行以下操作来对其进行jerry rig:
# whatever code instantiates the `model` would be here
...
...
model.targets = [ "targets of an appropriate structure and type go here" ]然后,当您将该model对象传递给函数时,它将有一个要访问的targets属性。但最好的做法是首先使用正确的对象。
或者,第三种选择:只注释掉有问题的行,并希望它实际上不会在任何地方使用(如果设置了它,它可能会在某个地方或其他地方使用)。
def __init__(self, model, sess, loss_fn=None):
"""
To generate the White-box Attack Agent.
:param model: the target model which should have the input tensor, the target tensor and the loss tensor.
:param sess: the tensorflow session.
:param loss_fn: None if using original loss of the model.
"""
self.model = model
self.input_tensor = model.inputs[0]
self.output_tensor = model.outputs[0]
#self.target_tensor = model.targets[0]
self._sample_weights = model.sample_weights[0]
if loss_fn is None:
self.loss_tensor = model.total_loss
self.gradient_tensor = K.gradients(self.loss_tensor, self.input_tensor)[0]
else:
self.set_loss_function(loss_fn)
self.sess = sesshttps://stackoverflow.com/questions/67491948
复制相似问题