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

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

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

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

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

Q&A

解決済

1回答

3030閲覧

Numbaのjitで行列の計算を早くしたい

textensor

総合スコア5

NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

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

0グッド

0クリップ

投稿2020/01/17 17:55

numbaのjitを用いて行列の計算を早くしたい。

pythonで関数を定義してjitを使って計算速度を早くしようとしたら次のようなエラーがでました。
このエラーはどうやったら取り除けるのでしょうか?

発生している問題・エラーメッセージ

sum.py:13: NumbaWarning: Compilation is falling back to object mode WITH looplifting enabled because Function "LOOP" failed type inference due to: Invalid use of Function(<built-in function setitem>) with argument(s) of type(s): (readonly array(float64, 2d, C), UniTuple(int64 x 2), float64) * parameterized In definition 0: All templates rejected with literals. In definition 1: All templates rejected without literals. In definition 2: All templates rejected with literals. In definition 3: All templates rejected without literals. In definition 4: All templates rejected with literals. In definition 5: All templates rejected without literals. In definition 6: All templates rejected with literals. In definition 7: All templates rejected without literals. In definition 8: TypeError: Cannot modify value of type readonly array(float64, 2d, C) raised from /usr/local/lib/python3.7/site-packages/numba/typing/arraydecl.py:179 In definition 9: TypeError: Cannot modify value of type readonly array(float64, 2d, C) raised from /usr/local/lib/python3.7/site-packages/numba/typing/arraydecl.py:179 This error is usually caused by passing an argument of a type that is unsupported by the named function. [1] During: typing of setitem at sum.py (19) File "sum.py", line 19: def LOOP(): <source elided> for l in range(D1): o[i,j] += i+j+k+l ^ @jit sum.py:13: NumbaWarning: Compilation is falling back to object mode WITHOUT looplifting enabled because Function "LOOP" failed type inference due to: cannot determine Numba type of <class 'numba.dispatcher.LiftedLoop'> File "sum.py", line 15: def LOOP(): for i in range(D1): ^ @jit /usr/local/lib/python3.7/site-packages/numba/object_mode_passes.py:178: NumbaWarning: Function "LOOP" was compiled in object mode without forceobj=True, but has lifted loops. File "sum.py", line 15: def LOOP(): for i in range(D1): ^ state.func_ir.loc)) /usr/local/lib/python3.7/site-packages/numba/object_mode_passes.py:188: 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 "sum.py", line 15: def LOOP(): for i in range(D1): ^ state.func_ir.loc)) sum.py:13: NumbaWarning: Compilation is falling back to object mode WITHOUT looplifting enabled because Function "LOOP" failed type inference due to: Invalid use of Function(<built-in function setitem>) with argument(s) of type(s): (readonly array(float64, 2d, C), UniTuple(int64 x 2), float64) * parameterized In definition 0: All templates rejected with literals. In definition 1: All templates rejected without literals. In definition 2: All templates rejected with literals. In definition 3: All templates rejected without literals. In definition 4: All templates rejected with literals. In definition 5: All templates rejected without literals. In definition 6: All templates rejected with literals. In definition 7: All templates rejected without literals. In definition 8: TypeError: Cannot modify value of type readonly array(float64, 2d, C) raised from /usr/local/lib/python3.7/site-packages/numba/typing/arraydecl.py:179 In definition 9: TypeError: Cannot modify value of type readonly array(float64, 2d, C) raised from /usr/local/lib/python3.7/site-packages/numba/typing/arraydecl.py:179 This error is usually caused by passing an argument of a type that is unsupported by the named function. [1] During: typing of setitem at sum.py (19) File "sum.py", line 19: def LOOP(): <source elided> for l in range(D1): o[i,j] += i+j+k+l ^ @jit /usr/local/lib/python3.7/site-packages/numba/object_mode_passes.py:178: NumbaWarning: Function "LOOP" was compiled in object mode without forceobj=True. File "sum.py", line 15: def LOOP(): for i in range(D1): ^ state.func_ir.loc)) /usr/local/lib/python3.7/site-packages/numba/object_mode_passes.py:188: 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 "sum.py", line 15: def LOOP(): for i in range(D1): ^ state.func_ir.loc))

該当のソースコード

python

1#coding:utf-8 2from numba import jit 3import math 4import numpy as np 5import matplotlib.pyplot as plt 6import datetime 7import time 8now = datetime.datetime.now() 9start = time.time() 10 11 12D1 = 50 13@jit 14def LOOP(): 15 for i in range(D1): 16 for j in range(D1): 17 for k in range(D1): 18 for l in range(D1): 19 o[i,j] += i+j+k+l 20 return o 21 22o= np.zeros((D1,D1)) 23A = list() 24for p in range(3): 25 if p<2: 26 B = list(o) 27 A.append(np.trace(LOOP())) 28 o= np.zeros((D1,D1)) 29 else: 30 B = list(o) 31 A.append(np.trace(LOOP())) 32 o= np.zeros((D1,D1)) 33 34print(A) 35 36elapsed_time = time.time() - start 37print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")

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

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

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

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

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

guest

回答1

0

ベストアンサー

リファレンスにもありますが、Numbaでは外部変数(グローバル変数)は、コンパイル時に定数として固定されてしまいます。
なのでreadonlyといったエラーが出ています。

解決策としては、ローカル変数になるようにすればいいです。→ 関数の引数で渡す。

python

1import time 2import numpy as np 3from numba import jit 4 5start = time.time() 6 7@jit 8def loop2(o, d): 9 for i in range(d): 10 for j in range(d): 11 for k in range(d): 12 for l in range(d): 13 o[i,j] += i+j+k+l 14 return o 15 16D1 = 50 17A = list() 18for p in range(3): 19 o = np.zeros((D1,D1)) 20 if p < 2: 21 A.append(np.trace(loop2(o, D1))) 22 else: 23 A.append(np.trace(loop2(o, D1))) 24 25print(A) 26 27elapsed_time = time.time() - start 28print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")

投稿2020/01/20 04:12

bsdfan

総合スコア4560

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

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

textensor

2020/01/23 11:24

なるほどローカル変数じゃないとだめなのですね。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問