我尝试使用神经网络为图像分类编写代码,而不使用KERAS等库。
但是当我要计算误差时我遇到了困难,因为输出前馈的矩阵大小与一个热门标签[ multiclasslabels_train = np.array([0] * 15 + [1] * 15 + [2] * 15 + [3] * 15 + [4] * 15 + [5] * 15 + [6] * 15 + [7]
* 15 + [8] * 15 + [9] * 15 + [10] * 15 + [11] * 15 + [12] * 15 + [13] * 15 + [14]
* 15 + [15] * 15 + [16] * 15 + [17] * 15 + [18] * 15 + [19] * 15)
y_train = np.zeros((300,20))
for i in range(300):
y_train[i, labels_train[i]] = 1
以下是用于前馈和错误值计算的代码:
for epoch in range(1):
hasil_ih = np.dot(W_ih, x_train) + B_ih
output_ih = sigmoid(hasil_ih)
hasil_hh = np.dot(W_hh, output_ih) + B_hh
output_hh = sigmoid(hasil_hh)
hasil_ho = np.dot(W_ho, output_hh) + B_ho
output_ho = sigmoid(hasil_ho)
print(output_ho.shape)
print(y_train.shape)
error_o = labels_train - output_ho
这里是程序运行时的结果:
Traceback (most recent call last):
(300, 4096)
File "D:/Pengenalan_Induk_Aksara/NN2.py", line 71, in <module>
(300, 20)
error_o = labels_train - output_ho
ValueError: operands could not be broadcast together with shapes (300,) (300,4096)
Process finished with exit code 1
请帮助我,我仍然是初学者,但尚未找到解决方案。以下是完整的代码:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import os
dir = "data_train/LBP_R1/"
kategori = ["ka","ga","nga","pa","ba","ma","ta","da","na","ca","ja","nya","ya","a","la","ra","sa","wa","ha","gha"]
features = []; label = []
for y in kategori:
path = os.path.join(dir, y)
class_num = kategori.index(y)
label.append(class_num)
for img in os.listdir(path):
im = Image.open(os.path.join(path,img))
imgs = list(im.getdata())
features.append(imgs)
x_train = np.vstack([features])
x_train = (x_train/255)
labels_train = np.array([0] * 15 + [1] * 15 + [2] * 15 + [3] * 15 + [4] * 15 +
[5] * 15 + [6] * 15 + [7] * 15 + [8] * 15 + [9] * 15 +
[10] * 15 + [11] * 15 + [12] * 15 + [13] * 15 + [14] *
15 + [15] * 15 + [16] * 15 + [17] * 15 + [18] * 15 + [19]
* 15)
y_train = np.zeros((300,20))
for i in range(300):
y_train[i, labels_train[i]] = 1
#Training Phase
def sigmoid(x):
return 1/(1+np.exp(-x))
def dsigmoid(y):
return y*(1-y)
attributes = x_train.shape[0]
hidden_nodes = 4
output_nodes = 300
np.random.seed(42)
W_ih = np.random.rand(hidden_nodes,attributes) #bobot input layer 1
B_ih = np.random.rand(hidden_nodes,1) #Bias input layer 1
W_hh = np.random.rand(hidden_nodes, hidden_nodes) #weight hidden layer 2
B_hh = np.random.rand(hidden_nodes, 1) #bias hidden layer 2
W_ho = np.random.rand(output_nodes, hidden_nodes) #weight hidden output
B_ho = np.random.rand(output_nodes, 1) #bias hidden output
lr = 0.25; #Learning rate
for epoch in range(1):
hasil_ih = np.dot(W_ih, x_train) + B_ih
output_ih = sigmoid(hasil_ih)
hasil_hh = np.dot(W_hh, output_ih) + B_hh
output_hh = sigmoid(hasil_hh)
hasil_ho = np.dot(W_ho, output_hh) + B_ho
output_ho = sigmoid(hasil_ho)
print(output_ho.shape)
print(y_train.shape)
error_o = labels_train - output_ho
print(output_ho)
print(y_train)
print(error_o)
[我试图使用神经网络为图像分类编写代码,而不使用KERAS等库,但是由于矩阵的大小,我想计算误差时遇到了困难...