【问题标题】:'Operation' object has no attribute '_id_value'“操作”对象没有属性“_id_value”
【发布时间】:2018-07-24 20:49:34
【问题描述】:

agentprior 是两个预先测试过的 Keras 函数模型,从存储的图和权重初始化。

然后我这样定义一个损失函数:

agent_logits = K.cast(K.max(agent_output, axis = 2), dtype = tf.float32)
prior_logits = K.cast(K.min(prior_output, axis = 2), dtype = tf.float32)

p_a_prior = K.sum(K.log(prior_logits), axis = 1)
p_a_agent = K.sum(K.log(agent_logits), axis = 1)

loss = K.mean(K.square(p_a_agent - p_a_prior))

然后我尝试计算梯度:

grads = tf.gradients(loss, agent.trainable_weights)

我收到了这个错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-28-435dabc8f049> in <module>()
     40 #   return grad, tf.negative(grad)
     41 
---> 42 grads = tf.gradients(loss, agent.trainable_weights)
     43 # grads = _compute_gradients(loss, agent.trainable_weights)
     44 print('[agent.inputs] = ', agent.inputs)

~/anaconda3/envs/hw3/lib/python3.5/site-packages/tensorflow/python/ops/gradients_impl.py in gradients(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients)
    488     pending_count, loop_state = _PendingCount(ops.get_default_graph(), to_ops,
    489                                               from_ops,
--> 490                                               colocate_gradients_with_ops)
    491 
    492     # Iterate over the collected ops.

~/anaconda3/envs/hw3/lib/python3.5/site-packages/tensorflow/python/ops/gradients_impl.py in _PendingCount(graph, to_ops, from_ops, colocate_gradients_with_ops)
    169   for op in to_ops:
    170     reached_ops[op._id] = True
--> 171   _MarkReachedOps(from_ops, reached_ops)
    172 
    173   # Mark between ops.

~/anaconda3/envs/hw3/lib/python3.5/site-packages/tensorflow/python/ops/gradients_impl.py in _MarkReachedOps(from_ops, reached_ops)
    115   while queue:
    116     op = queue.popleft()
--> 117     if not reached_ops[op._id]:
    118       reached_ops[op._id] = True
    119       for output in op.outputs:

~/anaconda3/envs/hw3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in _id(self)
   1635   def _id(self):
   1636     """The unique integer id of this operation."""
-> 1637     return self._id_value
   1638 
   1639   @property

AttributeError: 'Operation' object has no attribute '_id_value'

谁能给我一些建议?

【问题讨论】:

    标签: python tensorflow machine-learning keras loss-function


    【解决方案1】:

    由于你直接使用后端,我想你也需要从后端使用gradients方法:

    grads = K.gradients(...)
    

    【讨论】:

    • 非常感谢!现在可以了。当我发布这个问题时,我直接从调用agent.output 和prior.output 获得了agent_output 和prior_output。现在我为输入批次初始化一个占位符 x 并将输出作为 agent_output = agent(x)。想知道它们是否相同或只有后一种可行。
    • @beepretty 我认为它们是相同的。您是否得到不同的结果或任何错误?
    • 刚刚测试,结果很有趣。直接使用agent.output,并且session作为sess.run(grads, feed_dict = {agent.input: trainingBacth})馈送,上述错误依然存在。使用占位符,可以计算梯度,但我仍然不确定权重是否没有真正更新。
    • @beepretty sess 中存储了哪些会话?你创造了一个新的吗?还是使用了 Keras 隐式创建的会话?
    • 我使用 sess = tf.InteractiveSession() 和 K.set_session(sess)
    猜你喜欢
    • 2018-05-21
    • 2020-07-19
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2021-08-03
    • 1970-01-01
    相关资源
    最近更新 更多