Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';操作';对象没有属性'_id#u值';_Python_Tensorflow_Machine Learning_Keras_Loss Function - Fatal编程技术网

Python ';操作';对象没有属性'_id#u值';

Python ';操作';对象没有属性'_id#u值';,python,tensorflow,machine-learning,keras,loss-function,Python,Tensorflow,Machine Learning,Keras,Loss Function,agent和prior是根据存储的图和权重初始化的两个预先试验的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), axi

agent
prior
是根据存储的图和权重初始化的两个预先试验的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'
AttributeError回溯(最近一次调用)
在()
40#返回梯度,tf.负值(梯度)
41
--->42级=tf梯度(损失、代理、可训练重量)
43级=计算梯度(损失、代理、可训练权重)
44打印('[agent.inputs]=',agent.inputs)
~/anaconda3/envs/hw3/lib/python3.5/site-packages/tensorflow/python/ops/gradients\u impl.py in gradients(ys、xs、grad\u ys、name、colocate\u gradients\u with\u ops、gate\u gradients、aggregate\u方法、stop\u gradients)
488挂起计数,循环状态=_PendingCount(ops.get_default_graph(),to_ops,
489名来自联合行动队,
-->490个同形梯度(带操作)
491
492#迭代收集的操作。
~/anaconda3/envs/hw3/lib/python3.5/site-packages/tensorflow/python/ops/gradients\u impl.py in _PendingCount(图、to_ops、from_ops、colocate_gradients\u with_ops)
169对于op in to_ops:
170达到的操作[操作id]=真
-->171号到达点(从到达点到到达点)
172
173#在行动之间做标记。
~/anaconda3/envs/hw3/lib/python3.5/site-packages/tensorflow/python/ops/gradients\u impl.py in\u MarkReachedOps(从运行到运行)
115排队时:
116 op=queue.popleft()
-->117如果未到达操作[操作id]:
118已到达操作[操作id]=真
119对于op.outputs中的输出:
~/anaconda3/envs/hw3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in_id(self)
1635 def_id(自身):
1636“”此操作的唯一整数id。”“”
->1637返回自我识别值
1638
1639@property
AttributeError:“操作”对象没有属性“\u id\u值”

有谁能给我一些建议吗?

因为您直接使用后端,我认为您还需要从后端使用
渐变
方法:

grads = K.gradients(...)

谢谢!现在可以了。当我发布这个问题时,我直接通过调用agent.output和prior.output获得了agent\u输出和prior\u输出。现在,我为输入批初始化一个占位符x,并将输出作为agent\u output=agent(x)。不知道它们是一样的还是只有后一个是可行的。@beepretty我认为它们是一样的。你有没有得到不同的结果或任何错误?刚刚测试过,结果很有趣。直接使用agent.output,并将会话作为sess.run(grads,feed_dict={agent.input:trainingBacth})馈送,上述错误仍然存在。通过占位符,可以计算梯度,但我仍然不确定权重是否真正更新。@beepretty在
sess
中存储了什么会话?你创建了一个新的吗?或者使用Keras隐式创建的会话?我使用sess=tf.InteractiveSession()和K.set_会话(sess)