python - 在打印变量类型时,如何在 Tensorflow 中获得 pretty-print 结果?

标签 python arrays variables tensorflow

在ipython中我一直在尝试以下语句。

W1 = tf.Variable(tf.random_normal([3, 3, 1, 2], stddev=0.01))

这意味着,我正在创建 2 个过滤器,每个尺寸为 3*3,具有 1 个 channel 。 当打印这个 W1 时,我得到类似的东西

print W1
<tf.Variable 'Variable_4:0' shape=(3, 3, 1, 2) dtype=float32_ref>

是否有可能获得显示整个矩阵的 pretty-print ?

最佳答案

这一行是tensorflow变量的定义,即计算图中的节点:

W1 = tf.Variable(tf.random_normal([3, 3, 1, 2], stddev=0.01))

您已经提供了初始值,但在 session 启动并且初始化程序运行之前您无法访问它。以下是执行这一切的方法:

with tf.Session() as sess:
    sess.run(W1.initializer)
    print(W1.eval())  # another way: sess.run(W1)

...输出如下:

[[[[-0.00224525  0.00417244]]
  [[ 0.00627046 -0.01300699]]
  [[ 0.01755865 -0.01225026]]]
 [[[-0.01875982  0.00103016]]
  [[-0.01131416 -0.00079146]]
  [[-0.00957317  0.00036654]]]
 [[[ 0.00464012  0.0016774 ]]
  [[-0.00546181 -0.00818472]]
  [[ 0.01199017  0.00849589]]]]

关于python - 在打印变量类型时,如何在 Tensorflow 中获得 pretty-print 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47710467/

相关文章:

python - 当客户端断开连接时 Bottle.py 停止

java - 数组运行时错误

C++更改错误数组的值

python - Django 测试失败,显示 'django.db.utils.ProgrammingError: relation "django_content_type“不存在”

python - 测试 : Reporting and HTML output

python - 如何用新的绘图或绘图网格替换 matplotlib 图中以前的绘图?

arrays - 使用元胞数组进行平方欧氏计算

javascript - 对 JavaScript 变量作用域(单级)有点困惑

java - 如何在 Java 中的另一个类中使用一个类的变量?

PHP - 从类外部更改类变量/函数