简体   繁体   English

AttributeError: 'NDArray' 对象没有属性 'ravel' 或 'tolist'

[英]AttributeError: 'NDArray' object has no attribute 'ravel' or 'tolist'

I am working with a massive NDArray and am trying to recurse over each individual integer within this nested array.我正在处理一个巨大的 NDArray,并试图递归这个嵌套数组中的每个单独的整数。 The NDArray, called box_ids, looks like this:名为 box_ids 的 NDArray 如下所示:

[[[-1.]
  [-1.]
  [-1.]
  ...
  [-1.]
  [-1.]
  [-1.]]]

and when I write当我写

for ids in box_ids[0]:
    print(ids)

I am returned:我回来了:

<NDArray 1 @cpu(0)>, 
[0.]
<NDArray 1 @cpu(0)>, 
[0.]
<NDArray 1 @cpu(0)>, 
[0.]
<NDArray 1 @cpu(0)>, 
[1.]
<NDArray 1 @cpu(0)>, 
[1.]

...over and over again. ...再三,一而再再而三。

I have tried box_ids.flatten() and this has yielded the same result.我已经尝试过 box_ids.flatten() 并且这产生了相同的结果。 So, I went on to try .tolist() and .ravel(), but then I got the Attribute Error saying Numpy does not have these objects.所以,我继续尝试 .tolist() 和 .ravel(),但后来我收到了属性错误,说 Numpy 没有这些对象。

Basically, I want a list of all the individual integers in the array so that I can recurse over them.基本上,我想要一个数组中所有单个整数的列表,以便我可以对它们进行递归。 Ideally, a final list might look like [0, 0, 0, 1, 1, ...].理想情况下,最终列表可能看起来像 [0, 0, 0, 1, 1, ...]。 The fact that each element when I print(ids) has the newline and , is really confusing and I can't figure out how to get rid of this.当我 print(ids) 时每个元素都有换行符 and , 真的很令人困惑,我不知道如何摆脱这一点。 When I print(ids), I simply want something that would look like: 0 0 1 1...当我打印(ids)时,我只想要看起来像这样的东西:0 0 1 1 ...

I hope this makes sense.我希望这是有道理的。 Thanks so much for the help if you can!!如果可以的话,非常感谢您的帮助!!

You can use mxnet.ndarray.squeeze to remove unused nested dimensions, then convert to numpy array with mxnet.ndarray.asnumpy to extract the list of values, like this:您可以使用mxnet.ndarray.squeeze删除未使用嵌套的尺寸,然后转换为numpy的阵列mxnet.ndarray.asnumpy提取值列表,就像这样:

box_ids.squeeze().asnumpy().tolist()

As mentioned in the comment above, please note that even though MXNet NDArrays have by design an API extremely similar to Numpy arrays, they are actually totally different libraries with different internals.正如上面的评论中提到的,请注意,尽管 MXNet NDArrays 的 API 与 Numpy 数组非常相似,但它们实际上是完全不同的库,具有不同的内部结构。 See in this post more details about MXNet NDArray specificities.这篇文章中查看有关 MXNet NDArray 特性的更多详细信息。

NDArray really shines at doing things in a vectorized fashion over CPU or GPU, so before implementing a manual for loop over an NDArray consider if you can implement things in native mxnet NDArray only. NDArray 在 CPU 或 GPU 上以矢量化方式处理事情方面确实很出色,因此在对 NDArray 实施手动 for 循环之前,请考虑是否只能在本机 mxnet NDArray 中实施。 This will likely be much faster since mxnet commands are ran asynchronously in C++这可能会快得多,因为 mxnet 命令在 C++ 中异步运行

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

相关问题 AttributeError:“函数”对象没有属性“ Ravel” - AttributeError: 'function' object has no attribute 'ravel' AttributeError:&#39;NoneType&#39;对象没有属性&#39;ravel&#39; - AttributeError: 'NoneType' object has no attribute 'ravel' AttributeError:&#39;tuple&#39;对象没有属性&#39;ravel&#39; - AttributeError: 'tuple' object has no attribute 'ravel' “AttributeError:&#39;list&#39;对象没有属性&#39;ravel&#39;” - “AttributeError: 'list' object has no attribute 'ravel'” AttributeError:&#39;DataFrame&#39;对象没有属性&#39;ravel&#39; - AttributeError: 'DataFrame' object has no attribute 'ravel' Bokeh:AttributeError:&#39;DataFrame&#39;对象没有属性&#39;tolist&#39; - Bokeh: AttributeError: 'DataFrame' object has no attribute 'tolist' AttributeError:“ DataFrame”对象没有属性“ tolist” - AttributeError: 'DataFrame' object has no attribute 'tolist' AttributeError:“SeriesGroupBy”对象没有属性“tolist” - AttributeError: 'SeriesGroupBy' object has no attribute 'tolist' AttributeError:“ numpy.ndarray”对象没有属性“ A” - AttributeError: 'numpy.ndarray' object has no attribute 'A' 获取 AttributeError: 'builtin_function_or_method' object 没有属性 'tolist' - Getting AttributeError: 'builtin_function_or_method' object has no attribute 'tolist'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM