在Chaquopy中转换数组和张量 [英] Converting Arrays and Tensors in Chaquopy

查看:201
本文介绍了在Chaquopy中转换数组和张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该怎么做?

我看到您的帖子说您可以将Java对象传递给Python方法,但这不适用于numpy数组和TensorFlow张量.以下是我尝试过的各种变体,但无济于事.

I saw your post saying that you can just pass java Objects to Python methods but this is not working for numpy arrays and TensorFlow tensors. The following, and various variation of this, is what I tried and to no avail.

double[][] anchors = new double[][]{{0.57273, 0.677385}, {1.87446, 2.06253}, {3.33843, 5.47434}, {7.88282, 3.52778}, {9.77052, 9.16828}};
PyObject anchors_ = numpy.callAttr("array", anchors);

我也尝试使用连接创建此连接,但是它不起作用.这是因为连接(和堆栈等)要求包含数组的 names 的序列作为参数传递,而Java中似乎没有办法用Chaquopy做到这一点.

I also tried to use concatenate to create this but it does not work. This is because concatenate (and stack, etc.) require a sequence containing the names of the arrays to be passed as an argument and there does not seem to be a way to do that with Chaquopy in Java.

有什么建议吗?

推荐答案

我假设您收到的错误是"ValueError:仅接受2个非关键字参数".

I assume the error you received was "ValueError: only 2 non-keyword arguments accepted".

在调用numpy.array的过程中,您可能还会收到来自Android Studio的警告,说混淆参数'anchors',不清楚是否需要varargs或non-varargs调用".这是问题的根源.您打算传递一个double[][]参数,但不幸的是Java将其解释为五个double[]参数.

You probably also got a warning from Android Studio in the call to numpy.array, saying "Confusing argument 'anchors', unclear if a varargs or non-varargs call is desired". This is the source of the problem. You intended to pass one double[][] argument, but unfortunately Java has interpreted it as five double[] arguments.

Android Studio应该为您自动修复将参数强制转换为Object的情况,即:

Android Studio should offer you an automatic fix of casting the parameter to Object, i.e.:

numpy.callAttr("array", (Object)anchors);

这告诉Java编译器您只打算传递一个参数,然后numpy.array将正确工作.

This tells the Java compiler that you intend to pass only one argument, and numpy.array will then work correctly.

这篇关于在Chaquopy中转换数组和张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆