【问题标题】:White spaces in Theano arraysTheano 数组中的空格
【发布时间】:2015-07-05 13:29:34
【问题描述】:

我刚开始玩 Theano,我对这段代码的结果感到惊讶。

from theano import *
import theano.tensor as T
a = T.vector()
out = a + a ** 10
f = function([a], out)
print(f([0, 1, 2]))

使用 python3 我得到:

array([    0.,     2.,  1026.])

数组本身是正确的,它包含正确的值,但是打印输出是奇数。我希望是这样的:

array([0, 2, 1026])

array([0.0, 2.0, 1026.0])

为什么会这样?什么是额外的空白?我应该担心吗?

【问题讨论】:

    标签: arrays python-3.x numpy theano


    【解决方案1】:

    您打印的是numpy.ndarray。默认情况下,它们在打印时会像这样格式化自己。

    输出数组是一个浮点数组,因为默认情况下,Theano 使用浮点张量。

    如果你想使用整数张量那么你需要指定一个dtype:

    a = T.vector(dtype='int64')
    

    或者使用一点语法糖:

    a = T.lvector()
    

    将您的输出与以下输出进行比较:

    print numpy.array([0, 2, 1026], dtype=numpy.float64)
    print numpy.array([0, 2, 1026], dtype=numpy.int64)
    

    您可以使用 numpy.set_printoptions 更改 numpy 的默认打印选项。

    【讨论】:

    • 感谢您的回答,我不知道numpy.set_printoptions,不错的提示!
    猜你喜欢
    • 2016-10-30
    • 1970-01-01
    • 2021-02-04
    • 2021-11-26
    • 2016-06-16
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多