質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

6823閲覧

Numba njitにおけるappendメソッドの使用について

hamberger

総合スコア24

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/03/15 12:04

pythonの関数内の処理を速くしようと思い、njitコンパイラーを使用しています。以下に簡単なコードを作成しておきましたので、エラーの改善方法を教えていただきたいです。

やりたいこと:
cという名前のarrayがあるとして、次のような処理を行いたいです。
c = ([])に対してループ処理し、cの最後尾にループのインデックスを追加して更新されたcを取得

from numba import njit import numpy as np @njit def func(n): c = np.empty(0) for i in range(1,n): c = np.append(c,i) return c func(5)

上記コードを実行すると、以下のエラー文:
numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Use of unsupported NumPy function 'numpy.append' or unsupported use of the function.

numpyのappendメソッドはnumbaで対応しているようですが、(http://numba.pydata.org/numba-doc/latest/reference/numpysupported.html#other-functions)改善方法を教えて下さい。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

同じコードを実行しましたが、自分の環境では問題なく動作しました。エラー文の内容からFailed in nopython mode pipelineとありますので、nopythonを無効にしたら動くかもしれません。なので、試しに以下のコードを動かすとどうですか?

python

1from numba import njit 2import numpy as np 3 4@njit(nopython=False) 5def func(n): 6 c = np.empty(0) 7 for i in range(1,n): 8 c = np.append(c,i) 9 return c 10 11func(5)

投稿2020/03/16 02:57

Supernove

総合スコア1154

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hamberger

2020/03/16 04:00

@njit(nopythonmode=False)で実行してみましたが、やはり以下のエラーが出現します。 =============================================== RuntimeWarning: nopython is set for njit and is ignored warnings.warn('nopython is set for njit and is ignored', RuntimeWarning) Traceback (most recent call last): File "test_jit.py", line 35, in <module> func(5) File "C:\python\anaconda\lib\site-packages\numba\dispatcher.py", line 351, in _compile_for_args error_rewrite(e, 'typing') File "C:\python\anaconda\lib\site-packages\numba\dispatcher.py", line 318, in error_rewrite reraise(type(e), e, None) File "C:\python\anaconda\lib\site-packages\numba\six.py", line 658, in reraise raise value.with_traceback(tb) numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend) Use of unsupported NumPy function 'numpy.append' or unsupported use of the function.  File "test_jit.py", line 32: def func(n): <source elided> for i in range(1,n):  c = np.append(c,i)  ^  [1] During: typing of get attribute at test_jit.py (32)  File "test_jit.py", line 32: def func(n): <source elided> for i in range(1,n):  c = np.append(c,i)  ^ This is not usually a problem with Numba itself but instead often caused by the use of unsupported features or an issue in resolving types. To see Python/NumPy features supported by the latest release of Numba visit: http://numba.pydata.org/numba-doc/dev/reference/pysupported.html and http://numba.pydata.org/numba-doc/dev/reference/numpysupported.html For more information about typing errors and how to debug them visit: http://numba.pydata.org/numba-doc/latest/user/troubleshoot.html#my-code-doesn-t-compile If you think your code should work with Numba, please report the error message and traceback, along with a minimal reproducer at: https://github.com/numba/numba/issues/new ================================================= ======= @jitで実行すると次のエラー?が出現します。最後尾のように結果自体は得られました。 ============================================== NumbaDeprecationWarning:  Fall-back from the nopython compilation path to the object mode compilation path has been detected, this is deprecated behaviour. For more information visit http://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit  File "test_jit.py", line 6: @jit def average_func(n): ^  warnings.warn(errors.NumbaDeprecationWarning(msg, self.func_ir.loc)) test_jit.py:5: NumbaWarning:  Compilation is falling back to object mode WITHOUT looplifting enabled because Function "average_func" failed type inference due to: Use of unsupported NumPy function 'numpy.append' or unsupported use of the function.  File "test_jit.py", line 16: def average_func(n): <source elided> # s = sum/count  c=np.append(c,i)  ^  [1] During: typing of get attribute at test_jit.py (16)  File "test_jit.py", line 16: def average_func(n): <source elided> # s = sum/count  c=np.append(c,i)  ^  @jit C:\python\anaconda\lib\site-packages\numba\compiler.py:725: NumbaWarning: Function "average_func" was compiled in object mode without forceobj=True.  File "test_jit.py", line 12: def average_func(n): <source elided> av=0  for i in range(1,n):  ^  self.func_ir.loc)) C:\python\anaconda\lib\site-packages\numba\compiler.py:734: NumbaDeprecationWarning:  Fall-back from the nopython compilation path to the object mode compilation path has been detected, this is deprecated behaviour. For more information visit http://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit  File "test_jit.py", line 12: def average_func(n): <source elided> av=0  for i in range(1,n):  ^  warnings.warn(errors.NumbaDeprecationWarning(msg, self.func_ir.loc)) test_jit.py:5: NumbaWarning:  Compilation is falling back to object mode WITHOUT looplifting enabled because Function "average_func" failed type inference due to: Use of unsupported NumPy function 'numpy.append' or unsupported use of the function.  File "test_jit.py", line 16: def average_func(n): <source elided> # s = sum/count  c=np.append(c,i)  ^  [1] During: typing of get attribute at test_jit.py (16)  File "test_jit.py", line 16: def average_func(n): <source elided> # s = sum/count  c=np.append(c,i)  ^  @jit C:\python\anaconda\lib\site-packages\numba\compiler.py:725: NumbaWarning: Function "average_func" was compiled in object mode without forceobj=True.  File "test_jit.py", line 12: def average_func(n): <source elided> av=0  for i in range(1,n):  ^  self.func_ir.loc)) C:\python\anaconda\lib\site-packages\numba\compiler.py:734: NumbaDeprecationWarning:  Fall-back from the nopython compilation path to the object mode compilation path has been detected, this is deprecated behaviour. For more information visit http://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit  File "test_jit.py", line 12: def average_func(n): <source elided> av=0  for i in range(1,n):  ^  warnings.warn(errors.NumbaDeprecationWarning(msg, self.func_ir.loc)) average= [1. 2. 3. 4.]
Supernove

2020/03/16 04:56

なるほど、@jitで結果が出ているということはそれはエラーではなくWarningですね。Warningはエラーほどではないけど、動作の保証がないことを指しています。原因としてはNumbaは依存性の高いパッケージでコンパイルが不十分にできていない可能性があります。Numpyやディープラーニングなどの高度な計算ライブラリではよくある現象です。動けば問題ないプログラムであれば無視してもいいですが、ワーニングを解決するにはvirtualenvで仮想環境を構築するか一度numbaをアンインストールした上で以下のコマンドを実行して、再インストールするといいかもしれません。```--no-cache-dir```は以前ビルドしたときのキャッシュを無効にしてライブラリをインストールします。 ``` pip install numba --no-cache-dir ```
hamberger

2020/03/16 06:24

ご助言ありがとうございます。 numbaを再インストール(キャッシュ無効で)することでnjit及びjit両方でwarningなく正常に動作しました。ありがとうございます!
bsdfan

2020/03/16 06:45

解決済になったので、こちらに。 numpy.append()のサポートはバージョン0.46からのようなので、再インストール前は古いバージョンだったのでしょう。 ただnumpy.append()はarrayを作り直すので重い処理です。 ループのなかでnumpy.append()を繰り返すのは、やってはいけません。 これはnumbaを使っても高速化できません。
hamberger

2020/03/17 11:51

わかりました。appendの遅さについては、固定長配列や可変長配列が関係しているようですね。このようなコメント大変助かります。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問