# 线性分类

，输出

，每一个样本的权重是

，偏置项bias是

。得分函数

，假设这张图片有4个像素，拉伸成单列：

# 损失函数

。因为w的增长或缩小完全可以抵消

N是训练样本的个数，取平均损失函数，

# 代码实现

def load_pickle(f):
version = platform.python_version_tuple()
if version[0] == '2':
elif version[0] == '3':
raise ValueError("invalid python version: {}".format(version))

with open(filename, 'rb') as f:
x = x.reshape(10000, 3, 32, 32).transpose(0, 3, 2, 1).astype('float')
y = np.array(y)
return x, y

xs = []
ys = []
for b in range(1, 6):
f = os.path.join(root, 'data_batch_%d' % (b, ))
xs.append(x)
ys.append(y)
X = np.concatenate(xs)
Y = np.concatenate(ys)
return X, Y, x_test, y_test

def data_validation(x_train, y_train, x_test, y_test):
num_training = 49000
num_validation = 1000
num_test = 1000
num_dev = 500
mean_image = np.mean(x_train, axis=0)
x_train -= mean_image
mask = range(num_training, num_training + num_validation)
X_train = np.reshape(X_train, (X_train.shape[0], -1))
X_val = np.reshape(X_val, (X_val.shape[0], -1))
X_test = np.reshape(X_test, (X_test.shape[0], -1))
X_dev = np.reshape(X_dev, (X_dev.shape[0], -1))
X_train = np.hstack([X_train, np.ones((X_train.shape[0], 1))])
X_val = np.hstack([X_val, np.ones((X_val.shape[0], 1))])
X_test = np.hstack([X_test, np.ones((X_test.shape[0], 1))])
X_dev = np.hstack([X_dev, np.ones((X_dev.shape[0], 1))])
return X_val, Y_val, X_train, Y_train, X_dev, Y_dev, X_test, Y_test
pass

def showPicture(x_train, y_train):
classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
num_classes = len(classes)
samples_per_classes = 7
for y, cls in enumerate(classes):
idxs = np.flatnonzero(y_train == y)
idxs = np.random.choice(idxs, samples_per_classes, replace=False)
for i, idx in enumerate(idxs):
plt_index = i*num_classes +y + 1
plt.subplot(samples_per_classes, num_classes, plt_index)
plt.imshow(x_train[idx].astype('uint8'))
plt.axis('off')
if i == 0:
plt.title(cls)
plt.show()

    def loss(self, x, y, reg):
loss = 0.0
dw = np.zeros(self.W.shape)
num_train = x.shape[0]
scores = x.dot(self.W)
correct_class_score = scores[range(num_train), list(y)].reshape(-1, 1)
margin = np.maximum(0, scores - correct_class_score + 1)
margin[range(num_train), list(y)] = 0
loss = np.sum(margin)/num_train + 0.5 * reg * np.sum(self.W*self.W)

num_classes = self.W.shape[1]
inter_mat = np.zeros((num_train, num_classes))
inter_mat[margin > 0] = 1
inter_mat[range(num_train), list(y)] = 0
inter_mat[range(num_train), list(y)] = -np.sum(inter_mat, axis=1)

dW = (x.T).dot(inter_mat)
dW = dW/num_train + reg*self.W
return loss, dW
pass

    def train(self, X, y, learning_rate=1e-3, reg=1e-5, num_iters=100,batch_size=200, verbose=False):
num_train, dim = X.shape
num_classes = np.max(y) + 1
if self.W is None:
self.W = 0.001 * np.random.randn(dim, num_classes)
# Run stochastic gradient descent to optimize W
loss_history = []
for it in range(num_iters):
X_batch = None
y_batch = None
idx_batch = np.random.choice(num_train, batch_size, replace = True)
X_batch = X[idx_batch]
y_batch = y[idx_batch]
loss, grad = self.loss(X_batch, y_batch, reg)
loss_history.append(loss)
if verbose and it % 100 == 0:
print('iteration %d / %d: loss %f' % (it, num_iters, loss))
return loss_history
pass

    def predict(self, X):
y_pred = np.zeros(X.shape[0])
scores = X.dot(self.W)
y_pred = np.argmax(scores, axis = 1)
return y_pred

 svm = LinearSVM()
tic = time.time()
cifar10_name = '../Data/cifar-10-batches-py'
x_train, y_train, x_test, y_test = loadCIFAR10(cifar10_name)
X_val, Y_val, X_train, Y_train, X_dev, Y_dev, X_test, Y_test = data_validation(x_train, y_train, x_test, y_test)
loss_hist = svm.train(X_train, Y_train, learning_rate=1e-7, reg=2.5e4,
num_iters=3000, verbose=True)
toc = time.time()
print('That took %fs' % (toc - tic))
plt.plot(loss_hist)
plt.xlabel('Iteration number')
plt.ylabel('Loss value')
plt.show()
y_test_pred = svm.predict(X_test)
test_accuracy = np.mean(Y_test == y_test_pred)
print('accuracy: %f' % test_accuracy)
w = svm.W[:-1, :]  # strip out the bias
w = w.reshape(32, 32, 3, 10)
w_min, w_max = np.min(w), np.max(w)
classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
for i in range(10):
plt.subplot(2, 5, i + 1)
wimg = 255.0 * (w[:, :, :, i].squeeze() - w_min) / (w_max - w_min)
plt.imshow(wimg.astype('uint8'))
plt.axis('off')
plt.title(classes[i])
plt.show()

68 篇文章14 人订阅

0 条评论

## 相关文章

### 深度学习: 检测算法演进

[1] 干货 | 目标检测入门，看这篇就够了 [2] 基于深度学习的目标检测算法综述 [3] 基于深度学习的「目标检测」算法综述

953

### TF.Learn 手写文字识别

minist问题 计算机视觉领域的Hello world 给定55000个图片，处理成28*28的二维矩阵，矩阵中每个值表示一个像素点的灰度，作为feature...

3966

2825

1426

5204

6512

5756

### 支持向量机原理(四)SMO算法原理

在SVM的前三篇里，我们优化的目标函数最终都是一个关于$\alpha$向量的函数。而怎么极小化这个函数，求出对应的$\alpha$向量，进而求出分离超平面我...

942

### Andrew Ng机器学习课程笔记--week2（多元线性回归&正规公式）

1. 内容概要 Multivariate Linear Regression(多元线性回归) 多元特征 多元变量的梯度下降 特征缩放 Computing Pa...

2338

651