Skip to main content
 首页 » 编程设计

python之tensorflow 跟踪不起作用

2025年04月02日31bluestorm

我是 Tensorflow 的新手。我有一个张量 x 如下:

>>> x.eval() 
array([[1, 2, 3], 
       [4, 5, 6], 
       [7, 8, 9]], dtype=int32) 

我想计算这个张量的迹,所以我使用 tf.trace , 但它抛出错误:

>>> tf.trace(x) 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
AttributeError: 'module' object has no attribute 'trace' 

我该如何解决?非常感谢

请您参考如下方法:

对我有用

import tensorflow as tf 
x = tf.constant([[1, 2, 3],[4, 5, 6],[7, 8, 9]], dtype=tf.int32) 
sess = tf.InteractiveSession() 
x.eval() 
tf.trace(x).eval() 

这打印

[[1 2 3] 
 [4 5 6] 
 [7 8 9]] 
15