简体   繁体   English

CuPy 和 Dirichlet 给了我 TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

[英]CuPy and Dirichlet gives me TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

I simply want to create a random matrix A whose vectors are drawn from the Dirichlet distribution.我只是想创建一个随机矩阵 A,其向量来自 Dirichlet 分布。 The function works fine with numpy : function 与numpy配合良好:

import numpy as np
A = np.random.dirichlet(np.ones(n), n)

When I do the same thing with cupy当我对cupy做同样的事情时

import cupy as cp
A = cp.random.dirichlet(cp.ones(n), n)

I get the error below:我收到以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-45a4f64a8b6e> in <module>
      6 n = 10000 #Size of the square matrix
      7 
----> 8 A = cp.random.dirichlet(cp.ones(n), n)
      9 
     10 print("--- %s seconds ---" % (time.time() - start_time))

~\anaconda3\envs\tensorflow\lib\site-packages\cupy\random\_distributions.py in dirichlet(alpha, size, dtype)
    112     """
    113     rs = _generator.get_random_state()
--> 114     return rs.dirichlet(alpha, size, dtype)
    115 
    116 

~\anaconda3\envs\tensorflow\lib\site-packages\cupy\random\_generator.py in dirichlet(self, alpha, size, dtype)
    144             size = alpha.shape
    145         else:
--> 146             size += alpha.shape
    147         y = cupy.empty(shape=size, dtype=dtype)
    148         _kernels.standard_gamma_kernel(alpha, self._rk_seed, y)

TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

When the input is a numpy array like this当输入是这样的 numpy 数组时

import cupy as cp
import numpy as np

A = cp.random.dirichlet(np.ones(n), n)

then I get the same error.然后我得到同样的错误。

The alpha.shape from line 146 is (n,) when I check manually.当我手动检查时,第 146 行的alpha.shape是 (n,) 。 Is it a cupy bug or am I missing something?它是一个cupy bug还是我错过了什么?

I'm using cupy-cuda101 version 8.5.0 for CUDA 10.1.我正在为 CUDA 10.1 使用 cupy-cuda101 版本 8.5.0。 Everything else that has to do with cupy and tensorflow works perfectly on my GPU (2080ti).与 cupy 和 tensorflow 相关的所有其他东西都可以在我的 GPU (2080ti) 上完美运行。

This is a bug in cupy which you should report on their GitHub.这是 cupy 中的一个错误,您应该在他们的 GitHub 上报告。

They do not properly handle the case of an integer argument, despite the documentation.尽管有文档,但他们没有正确处理 integer 参数的情况。 They require that you provide either a tuple or None .他们要求您提供一个元组或None This is why you see the behavior you're seeing.这就是为什么你看到你所看到的行为。 (If you provided a tuple (a, b) , then the resulting shape would properly be (a, b, n) . (如果您提供了一个元组(a, b) ,那么生成的形状应该是(a, b, n)

The workaround here is to provide the shape you want as a length-1 tuple: (n,) .这里的解决方法是将您想要的形状作为长度为 1 的元组提供: (n,) Note that the comma is necessary.请注意,逗号是必需的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:/不支持的操作数类型/:&#39;Image&#39;和&#39;int&#39; - TypeError: unsupported operand type(s) for /: 'Image' and 'int' TypeError: /: 'Dimension' 和 'int' 不支持的操作数类型 - TypeError: unsupported operand type(s) for /: 'Dimension' and 'int' TypeError:/:28次迭代后,/:“ list”和“ int”不受支持的操作数类型 - TypeError: unsupported operand type(s) for /: 'list' and 'int' after 28 iterations TypeError:--不支持的操作数类型-用于绘制程序的&#39;int&#39;和&#39;list&#39; - TypeError: unsupported operand type(s) for -: 'int' and 'list' for plotting program 类型错误:不支持的操作数类型 %: 'list' 和 'int' with np.mod - TypeError: unsupported operand type(s) for %: 'list' and 'int' with np.mod Python - 类型错误:+ 不支持的操作数类型:&#39;zip&#39; 和 &#39;int&#39; - Python - TypeError: unsupported operand type(s) for +: 'zip' and 'int' TypeError:/:'dict_values'和'int'的不支持的操作数类型 - TypeError: unsupported operand type(s) for /: 'dict_values' and 'int' Python-&gt; TypeError:^不支持的操作数类型 - Python -> TypeError: unsupported operand type(s) for ^ TypeError:/:&#39;list&#39;和&#39;long&#39;的不支持的操作数类型 - TypeError: unsupported operand type(s) for /: 'list' and 'long' TypeError:*:“方法”和“浮点数”不受支持的操作数类型 - TypeError: unsupported operand type(s) for *: 'method' and 'float'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM