使用感知器进行多分类#
来自 AI for Beginners Curriculum 的实验任务。
In [1]:
import matplotlib.pyplot as plt
import numpy as np
import pickle
import os您可以使用讲座中的以下感知器训练代码:
In [6]:
def train(positive_examples, negative_examples, num_iterations = 100):
num_dims = positive_examples.shape[1]
weights = np.zeros((num_dims,1)) # initialize weights
pos_count = positive_examples.shape[0]
neg_count = negative_examples.shape[0]
report_frequency = 10
for i in range(num_iterations):
pos = random.choice(positive_examples)
neg = random.choice(negative_examples)
z = np.dot(pos, weights)
if z < 0:
weights = weights + pos.reshape(weights.shape)
z = np.dot(neg, weights)
if z >= 0:
weights = weights - neg.reshape(weights.shape)
if i % report_frequency == 0:
pos_out = np.dot(positive_examples, weights)
neg_out = np.dot(negative_examples, weights)
pos_correct = (pos_out >= 0).sum() / float(pos_count)
neg_correct = (neg_out < 0).sum() / float(neg_count)
print("Iteration={}, pos correct={}, neg correct={}".format(i,pos_correct,neg_correct))
return weightsIn [13]:
def accuracy(weights, test_x, test_labels):
res = np.dot(np.c_[test_x,np.ones(len(test_x))],weights)
return (res.reshape(test_labels.shape)*test_labels>=0).sum()/float(len(test_labels))
accuracy(wts, test_x, test_labels)1.0读取数据集#
此代码从互联网的仓库中下载数据集。你也可以手动从 AI Curriculum 仓库的 /data 目录中复制数据集。
In [8]:
!rm *.pkl
https://github.com/mnielsen/neural-networks-and-deep-learning/blob/master/data/mnist.pkl.gz!gzip -d mnist.pkl.gzIn [19]:
with open('mnist.pkl', 'rb') as mnist_pickle:
MNIST = pickle.load(mnist_pickle)In [20]:
print(MNIST['Train']['Features'][0][130:180])
print(MNIST['Train']['Labels'][0])
features = MNIST['Train']['Features'].astype(np.float32) / 256.0
labels = MNIST['Train']['Labels']
fig = plt.figure(figsize=(10,5))
for i in range(10):
ax = fig.add_subplot(1,10,i+1)
plt.imshow(features[i].reshape(28,28))
plt.show()[ 0 0 188 255 94 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 191 250 253 93 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0]
1
创建用于两位数字分类的one-vs-other数据集的代码。您需要修改此代码以创建one-vs-all数据集。
In [23]:
def set_mnist_pos_neg(positive_label, negative_label):
positive_indices = [i for i, j in enumerate(MNIST['Train']['Labels'])
if j == positive_label]
negative_indices = [i for i, j in enumerate(MNIST['Train']['Labels'])
if j == negative_label]
positive_images = MNIST['Train']['Features'][positive_indices]
negative_images = MNIST['Train']['Features'][negative_indices]
return positive_images, negative_images现在你需要完成以下任务:
- 为所有数字创建10个一对多数据集
- 训练10个感知器
- 定义
classify函数以执行数字分类 - 测量分类的准确率并打印混淆矩阵
- [可选] 创建改进版的
classify函数,通过一次矩阵乘法完成分类。
免责声明:
本文档使用AI翻译服务Co-op Translator进行翻译。尽管我们努力确保翻译的准确性,但请注意,自动翻译可能包含错误或不准确之处。应以原始语言的文档作为权威来源。对于关键信息,建议使用专业人工翻译。我们对因使用此翻译而引起的任何误解或误读不承担责任。