|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是一个关于BP神经网络实现手写数字识别的代码 在运行的最后几步出现了未对齐的错误 求助大佬们应该怎么改啊
- #导入模块
- import numpy as np
- from sklearn.datasets import load_digits
- from sklearn.preprocessing import LabelBinarizer
- from sklearn.model_selection import train_test_split
- from sklearn.metrics import classification_report,confusion_matrix
- import matplotlib.pyplot as plt
- #载入数据
- digits = load_digits()
- print(digits.images.shape)
- #显示图片
- plt.imshow(digits.images[0],cmap = 'gray')
- plt.show()
- plt.imshow(digits.images[1],cmap = 'gray')
- plt.show()
- plt.imshow(digits.images[2],cmap = 'gray')
- plt.show
- #数据
- X = digits.data
- #标签
- y = digits.target
- print(X.shape)
- print(y.shape)
- print(X[:3])
- print(y[:3])
- # 定义一个神经网络,结构:64-100-10
- # 定义输入层到隐藏层之间的权值矩阵
- V = np.random.random((64,100))*2-1
- # 定义隐藏层到输出层之间的权值矩阵
- V = np.random.random((100,10))*2-1
- # 数据切分
- # 1/4为测试集,3/4为训练集
- X_train,X_test,y_train,y_test = train_test_split(X,y)
- #标签二值化
- lables_train = LabelBinarizer().fit_transform(y_train)
- print(y_train[:5])
- print('哈哈')
- print(lables_train[:5])
- # 激活函数
- def sigmoid(x):
- return 1/(1 + np.exp(-x))
- #激活函数的导数
- def dsigmoid(x):
- return x*(1-x)
- #训练模型
- def train(X,y,steps = 10000,lr = 0.11):
- global V,W
- for n in range(steps + 1):
- #随机选取一个数据
- i = np.random.randint(X.shape[0])
- #获取一个数据
- x = X[i]
- x = np.atleast_2d(x)
- # BP算法公式
- # 计算隐藏层的输出
- L1 = sigmoid(np.dot(x,V))
- #计算输出层的输出
- L2 = sigmoid(np.dot(L1,W))
- #计算L2_delta,L1_delta
- L2_delta = (y[i] - L2) * dsigmoid(L2)
- L1_delta = L2_delta.dot(W.t) * dsigmoid(L1)
- #更新权值
- W += lr * L1.T.dot(L2_delta)
- V += lr*x.T.dot(L1_delta)
- #每训练1000次预测一次准确率
- if n % 1000 == 0:
- output = predict(X_test)
- predictions = np.argmax(output,axis = 1)
- acc = np.mean(np.equal(predictions,y_test))
- print('steps:',n,'accuracy:',acc)
- def predict(x):
- #计算隐藏的输出
- L1 = sigmoid(np.dot(x,V))
- #计算输出的输出
- L2 = sigmoid(np.dot(L1,W))
- return L2
- # 进行30000次的模型训练,完成1000次训练输出一次训练结果
- train(X_train,lables_train,30000)
- # 用测试数据对已训练的模型进行测试
- output = predict(X_test)
- # 按行查找训练结果的最大元素
- predictions = np.argmax(output,axis = 1)
- # 比较预测测试标签和真实标签,并输出准确率
- print(classification_report(predictions,y_test))
- #利用混淆矩阵实现模型评估,矩阵行数据相加是真实值类别数,列数据相加是分类后的类别数
- print(confusion_matrix(predictions,y_test))
复制代码
报错那写了
- L1 = sigmoid(np.dot(x,V))
复制代码
这里58行未对奇,可能是长度不一样,导致的吧,你检查检查你的代码
|
-
|