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

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

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

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

Python

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

Q&A

解決済

1回答

2709閲覧

pythonのyou must specify 'dtype=object' when creating the ndarrayのエラーを解消したい

nekonoko09

総合スコア3

NumPy

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

Python

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

0グッド

0クリップ

投稿2021/06/30 05:57

前提・実現したいこと

'Artificial Neural Network Implementation using NumPy and Classification of the Fruits360 Image Dataset'の,画像を判断する問題を解いています.掲載されているコードを動かしたところエラーが出てしまいました.エラーを読むと,「配列が正しくない」「引数が間違っている」ようなのですが,解決方法がわかりません.どなたか教えていただけないでしょうか.

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

<ipython-input-5-52cbda16dbf7>:73: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray weights = numpy.array([input_HL1_weights, --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-5-52cbda16dbf7> in <module> 75 HL2_output_weights]) 76 ---> 77 weights = train_network(num_iterations=10, 78 weights=weights, 79 data_inputs=data_inputs, <ipython-input-5-52cbda16dbf7> in train_network(num_iterations, weights, data_inputs, data_outputs, learning_rate, activation) 21 for idx in range(len(weights) - 1): 22 curr_weights = weights[idx] ---> 23 r1 = numpy.matmul(a=r1, b=curr_weights) 24 if activation == "relu": 25 r1 = relu(r1) ValueError: invalid number of arguments

該当のソースコード

Python

1import numpy 2import pickle 3 4def sigmoid(inpt): 5 return 1.0 / (1 + numpy.exp(-1 * inpt)) 6 7def relu(inpt): 8 result = inpt 9 result[inpt < 0] = 0 10 return result 11 12def update_weights(weights, learning_rate): 13 new_weights = weights - learning_rate * weights 14 return new_weights 15 16def train_network(num_iterations, weights, data_inputs, data_outputs, learning_rate, activation="relu"): 17 for iteration in range(num_iterations): 18 print("Itreation ", iteration) 19 for sample_idx in range(data_inputs.shape[0]): 20 r1 = data_inputs[sample_idx, :] 21 for idx in range(len(weights) - 1): 22 curr_weights = weights[idx] 23 r1 = numpy.matmul(a=r1, b=curr_weights) 24 if activation == "relu": 25 r1 = relu(r1) 26 elif activation == "sigmoid": 27 r1 = sigmoid(r1) 28 curr_weights = weights[-1] 29 r1 = numpy.matmul(a=r1, b=curr_weights) 30 predicted_label = numpy.where(r1 == numpy.max(r1))[0][0] 31 desired_label = data_outputs[sample_idx] 32 if predicted_label != desired_label: 33 weights = update_weights(weights, learning_rate=0.001) 34 return weights 35 36def predict_outputs(weights, data_inputs, activation="relu"): 37 predictions = numpy.zeros(shape=(data_inputs.shape[0])) 38 for sample_idx in range(data_inputs.shape[0]): 39 r1 = data_inputs[sample_idx, :] 40 for curr_weights in weights: 41 r1 = numpy.matmul(a=r1, b=curr_weights) 42 if activation == "relu": 43 r1 = relu(r1) 44 elif activation == "sigmoid": 45 r1 = sigmoid(r1) 46 predicted_label = numpy.where(r1 == numpy.max(r1))[0][0] 47 predictions[sample_idx] = predicted_label 48 return predictions 49 50f = open("dataset_features.pkl", "rb") 51data_inputs2 = pickle.load(f) 52f.close() 53 54features_STDs = numpy.std(a=data_inputs2, axis=0) 55data_inputs = data_inputs2[:, features_STDs > 50] 56 57f = open("outputs.pkl", "rb") 58data_outputs = pickle.load(f) 59f.close() 60 61HL1_neurons = 150 62input_HL1_weights = numpy.random.uniform(low=-0.1, high=0.1, 63size=(data_inputs.shape[1], HL1_neurons)) 64 65HL2_neurons = 60 66HL1_HL2_weights = numpy.random.uniform(low=-0.1, high=0.1, 67size=(HL1_neurons, HL2_neurons)) 68 69output_neurons = 4 70HL2_output_weights = numpy.random.uniform(low=-0.1, high=0.1, 71size=(HL2_neurons, output_neurons)) 72 73weights = numpy.array([input_HL1_weights, 74 HL1_HL2_weights, 75 HL2_output_weights]) 76 77weights = train_network(num_iterations=10, 78 weights=weights, 79 data_inputs=data_inputs, 80 data_outputs=data_outputs, 81 learning_rate=0.01, 82 activation="relu") 83 84predictions = predict_outputs(weights, data_inputs) 85num_flase = numpy.where(predictions != data_outputs)[0] 86print("num_flase ", num_flase.size)

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

・使用しているサイトのURLになります.(https://towardsdatascience.com/artificial-neural-network-implementation-using-numpy-and-classification-of-the-fruits360-image-3c56affa4491)
・JupyterLabで動かしています.

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

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

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

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

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

meg_

2021/06/30 11:25

質問タイトルの「you must specify 'dtype=object' when creating the ndarray」はVisibleDeprecationWarningの一文ですね。 エラーは「ValueError: invalid number of arguments」の方ではありませんか?
meg_

2021/06/30 11:36

「r1 = numpy.matmul(a=r1, b=curr_weights)」のa=、b=を削除すると動きますか?
nekonoko09

2021/06/30 11:58

私も「ValueError: invalid number of arguments」がエラー文だと思ったのですが,メッセージについて色々調べていると配列が原因というサイトにたどり着いたので,そこがエラーだと思ってしまいました.ご指摘の通りだと思います,教えていただきありがとうございます. 「r1 = numpy.matmul(a=r1, b=curr_weights)」のa=、b=を削除すると動きますか? についてですが,該当の箇所のa=、b=を削除したところ,エラーは出ず動きました,ありがとうございます. しかし,以下のような 「<ipython-input-8-77271a77b395>:73: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray weights = numpy.array([input_HL1_weights,」 という警告が出ました.これは正しく作動しているということなのでしょうか?重ね重ね申し訳ありません.
guest

回答1

0

ベストアンサー

numpy.matmulには、第一引数x1第二引数x2をマストで渡さないといけないのですが、

r1 = numpy.matmul(a=r1, b=curr_weights)

こちらのコードだと、abに引数を渡していて、x1x2には引数が渡されていないことになります。

そのため、

r1 = numpy.matmul(x1=r1, x2=curr_weights)

もしくは

r1 = numpy.matmul(r1, curr_weights)

に変更すれば解決できるように思います。

こちらのドキュメント参考にしています。

https://numpy.org/doc/stable/reference/generated/numpy.matmul.html

投稿2021/07/06 15:15

nnkkmto

総合スコア175

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

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

nekonoko09

2021/07/07 05:17 編集

a, bを削除したら動いた理由もわかりました.とてもわかりやすい解説をありがとうございます.
nnkkmto

2021/07/07 16:14

ありがとうございます!解決したのであれば質問を閉じていただけると幸いです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問