【问题标题】:TypeError: 'float' object cannot be interpreted as an index in Multi GPU'sTypeError:“浮动”对象不能解释为多 GPU 中的索引
【发布时间】:2018-12-15 22:18:36
【问题描述】:

我无法理解为什么我的 jupyter 笔记本每次执行时都会显示 TypeError 的浮点数。

A = np.random.rand(1e4, 1e4).astype('float32')
B = np.random.rand(1e4, 1e4).astype('float32')


c1 = []
c2 = []


def matpow(M, n):
    if n < 1:  #Abstract cases where n < 1
        return M
    else:
        return tf.matmul(M, matpow(M, n-1))

错误:

Traceback (most recent call last) 
<ipython-input-5-61ea4b300d55> in <module>()
      1 # Example: compute A^n + B^n on 2 GPUs
      2 #Create random Large matrix
----> 3 A = np.random.rand(1e4, 1e4).astype('float32')
      4 B = np.random.rand(1e4, 1e4).astype('float32')
      5 
mtrand.pyx in mtrand.RandomState.rand()
mtrand.pyx in mtrand.RandomState.random_sample()
mtrand.pyx in mtrand.cont0_array()
TypeError: 'float' object cannot be interpreted as an index

【问题讨论】:

  • 请正确格式化您的问题
  • 一旦你意识到 OP 意味着整数而不是索引,这是一个重复。 stackoverflow.com/questions/32687012/…
  • 您错误地陈述了您的问题。它不应该说'float'对象不能被解释为整数吗?不是索引。标题相同。

标签: python tensorflow typeerror


【解决方案1】:

你的 TypeError 很清楚。浮点数不能解释为整数。此外,即使您不尝试将其转换为浮点数, 1e4 也是一个浮点数,即 python 中的 &gt;&gt;&gt;1v4 将输出 10000.0 这是一个浮点数。您需要将 1e4 转换为整数才能使您的代码正常工作。

A = np.random.rand(int(1e4), int(1e4))
B = np.random.rand(int(1e4), int(1e4))

这可行,但为什么不为每个 1e4 实例键入 10000,而不是编写更多代码将其转换为整数类型?

【讨论】:

  • np.random.rand 总是返回 [0, 1) 中的数字。他们将形状指定为 (10000, 10000)。
  • @xdurch0 你是对的。我更改了我的帖子以反映。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-14
  • 1970-01-01
  • 2021-12-05
  • 2017-10-11
  • 2021-03-03
  • 2015-06-18
  • 2015-04-01
相关资源
最近更新 更多