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

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

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

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

Q&A

解決済

1回答

1074閲覧

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

yuseiww

総合スコア1

Python

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

0グッド

0クリップ

投稿2021/11/26 07:09

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

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

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

NumbaWarning: Compilation is falling back to object mode WITH looplifting enabled because Function "ap" failed type inference due to: Invalid use of Function(<built-in function setitem>) with argument(s) of type(s): (readonly array(float64, 1d, C), int64, 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, 1d, C) raised from C:\Users\yusei\anaconda3\lib\site-packages\numba\typing\arraydecl.py:179 In definition 9: TypeError: Cannot modify value of type readonly array(float64, 1d, C) raised from C:\Users\yusei\anaconda3\lib\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 C:\Users\yusei\OneDrive\ドキュメント\Desktop\Desktop.spyder-py3\CIM.py (80) File "CIM.py", line 80: def ap(): <source elided> n_p = np.random.normal(0,np.sqrt(1-e)/2)*e_p ^ @jit C:\Users\yusei\OneDrive\ドキュメント\Desktop\Desktop.spyder-py3\CIM.py:74: NumbaWarning: Compilation is falling back to object mode WITHOUT looplifting enabled because Function "ap" failed type inference due to: cannot determine Numba type of <class 'numba.dispatcher.LiftedLoop'> File "CIM.py", line 77: def ap(): <source elided> for k in range(box-1): ^ @jit C:\Users\yusei\anaconda3\lib\site-packages\numba\object_mode_passes.py:178: NumbaWarning: Function "ap" was compiled in object mode without forceobj=True, but has lifted loops. File "CIM.py", line 77: def ap(): <source elided> for k in range(box-1): ^ state.func_ir.loc)) C:\Users\yusei\anaconda3\lib\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 "CIM.py", line 77: def ap(): <source elided> for k in range(box-1): ^ state.func_ir.loc)) C:\Users\yusei\OneDrive\ドキュメント\Desktop\Desktop.spyder-py3\CIM.py:74: NumbaWarning: Compilation is falling back to object mode WITHOUT looplifting enabled because Function "ap" failed type inference due to: Invalid use of Function(<built-in function setitem>) with argument(s) of type(s): (readonly array(float64, 1d, C), int64, 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, 1d, C) raised from C:\Users\yusei\anaconda3\lib\site-packages\numba\typing\arraydecl.py:179 In definition 9: TypeError: Cannot modify value of type readonly array(float64, 1d, C) raised from C:\Users\yusei\anaconda3\lib\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 C:\Users\yusei\OneDrive\ドキュメント\Desktop\Desktop.spyder-py3\CIM.py (80) File "CIM.py", line 80: def ap(): <source elided> n_p = np.random.normal(0,np.sqrt(1-e)/2)*e_p ^ @jit C:\Users\yusei\anaconda3\lib\site-packages\numba\object_mode_passes.py:178: NumbaWarning: Function "ap" was compiled in object mode without forceobj=True. File "CIM.py", line 77: def ap(): <source elided> for k in range(box-1): ^ state.func_ir.loc)) C:\Users\yusei\anaconda3\lib\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 "CIM.py", line 77: def ap(): <source elided> for k in range(box-1): ^ state.func_ir.loc))

該当のソースコード

python

1import numpy as np 2import matplotlib.pyplot as plt 3import time 4from numba import jit,prange 5start = time.time() 6spin = 2000 7c = 130 8P_k = 65*10**(-3) 9L = 0.05 10k = 0 11e_p = 2.59e-19 12box = 1000 13P = np.arange(0,1000,1e-4) 14G = np.zeros(spin) 15asd = np.zeros(box*spin).reshape(box,spin) 16 17e =1/12.6 18 19import sys 20import numpy as np 21mat = [] # 結果を入れるリスト(行列) 22 23with open('G22.txt', 'r', encoding='utf-8') as fin: # ファイルを開く 24 25 x = fin.readline() 26 for line in fin.readlines(): # 行をすべて読み込んで1行ずつfor文で回す 27 row = [] # 行のデータを保存するリスト 28 toks = line.split(' ') # 行を半角スペースで分割する 29 for tok in toks: # 分割したトークン列を回す 30 try: 31 num = int(tok) # 整数に変換 32 except ValueError: 33 print(file=sys.stderr) # エラーが出たら画面に出力して 34 continue # スキップ 35 row_append = row.append 36 row_append(num) # 行に保存 37 38 mat.append(row) # 行をnumsに保存 39J= np.zeros(2000*2000).reshape(2000,2000) 40for a in range(19990): 41 x = mat[a][0] 42 y = mat[a][1] 43 z = float(mat[a][2]) 44 J[x-1][y-1]=J[y-1][x-1] = -z 45 46for a in range(spin): 47 N_1 = np.random.normal(0,np.sqrt(G[a])/2)*e_p#自然放出光 48 asd[0][a] = np.sqrt(G[a])*0 + N_1 49 G[a] = np.exp(2*c*np.sqrt(P[0]*L)) 50 51 52@jit 53def saturated(a,p): 54 A_P = np.zeros(50) 55 A_S = np.zeros(50) 56 A_S[0] = a 57 A_P[0] = p 58 59 for l in prange(49): 60 A_P[l+1] = A_P[l] - c*(A_S[l])**2*0.05/50 61 A_S[l+1] = A_S[l] + c*A_P[l]*A_S[l]*0.05/50 62 K = A_S[49]**2/A_S[0]**2 63 out = np.sqrt(K)*a 64 return out 65 66@jit 67def interact(a,b): 68 69 70 result = 0.015*np.dot(J[b],asd[a]) 71 72 return result 73 74@jit 75def ap(): 76 77 for k in range(box-1): 78 for i in range(spin): 79 n_p = float(np.random.normal(0,np.sqrt(1-e)/2)*e_p) 80 asd[k+1][i] = saturated(asd[k][i]*np.sqrt(e)+n_p+interact(k,i),np.sqrt(P[k])) 81 82 83 84 85 86 #plt.plot(asd) 87 #plt.xlabel('kaisuu') 88 #plt.ylabel('sinpuku') 89 A = np.sign(asd[box-1]) 90 sum=0 91 for i in range(spin): 92 for j in range(i+1,spin): 93 sum += 1/2*(-J[i][j])*(1-A[i]*A[j]) 94 95 print(sum) 96for a in range(1): 97 ap() 98elapsedtime = time.time() - start 99print(elapsedtime) 100

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

エラーは、read onlyな変数を変更しようとしておきてきます。
numbaでjitコンパイルされた関数では、外部の変数はread onlyです。
今回のコードでは、ap()関数の中でglobalなasdを変更しようとしてエラーになっています。

また、外部変数はコンパイルされた時点での値で固定されるので、注意が必要です。
例えば、interact()関数の中のasdは、外でasdを変更しても、初回呼び出しの時の値のままになります。

numbaでは、本当の定数以外は、関数の引数として渡すように関数を作りましょう。

投稿2021/11/28 02:22

bsdfan

総合スコア4567

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問