前提・実現したいこと
'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で動かしています.
回答1件
あなたの回答
tips
プレビュー