Beispiel #1
0
 def test_svm_loss_vectorized(self):
     loss_naive, grad_naive = svm_loss_naive(self.weights, self.x, self.y,
                                             self.reg)
     loss_vect, grad_vect = svm_loss_vectorized(self.weights, self.x,
                                                self.y, self.reg)
     np.testing.assert_allclose(loss_naive, loss_vect)
     np.testing.assert_allclose(grad_naive, grad_vect)
Beispiel #2
0
# do the gradient check once again with regularization turned on
# you didn't forget the regularization gradient did you?
loss, grad = linear_svm.svm_loss_naive(W, X_dev, y_dev, 5e1)
f = lambda w: linear_svm.svm_loss_naive(w, X_dev, y_dev, 5e1)[0]
grad_numerical = gradient_check.grad_check_sparse(f, W, grad)

# Next implement the function svm_loss_vectorized; for now only compute the loss;
# we will implement the gradient in a moment.
tic = time.time()
loss_naive, grad_naive = linear_svm.svm_loss_naive(W, X_dev, y_dev, 0.000005)
toc = time.time()
print('Naive loss: %e computed in %fs' % (loss_naive, toc - tic))

tic = time.time()
loss_vectorized, _ = linear_svm.svm_loss_vectorized(W, X_dev, y_dev, 0.000005)
toc = time.time()
print('Vectorized loss: %e computed in %fs' % (loss_vectorized, toc - tic))

# The losses should match but your vectorized implementation should be much faster.
print('difference: %f' % (loss_naive - loss_vectorized))

# Complete the implementation of svm_loss_vectorized, and compute the gradient
# of the loss function in a vectorized way.

# The naive implementation and the vectorized implementation should match, but
# the vectorized version should still be much faster.
tic = time.time()
_, grad_naive = linear_svm.svm_loss_naive(W, X_dev, y_dev, 0.000005)
toc = time.time()
print('Naive loss and gradient: computed in %fs' % (toc - tic))
Beispiel #3
0
# do the gradient check once again with regularization turned on
# you didn't forget the regularization gradient did you?
loss, grad = svm_loss_naive(W, X_dev, y_dev, 5e1)
f = lambda w: svm_loss_naive(w, X_dev, y_dev, 5e1)[0]
grad_numerical = grad_check_sparse(f, W, grad)


# Next implement the function svm_loss_vectorized; for now only compute the loss;
# we will implement the gradient in a moment.
tic=time.time()
loss_naive, grad_naive = svm_loss_naive(W, X_dev, y_dev, 0.000005)
toc=time.time()
print('Naive loss: %e computed in %fs' % (loss_naive, toc - tic))
tic = time.time()
loss_vectorized, _ = svm_loss_vectorized(W, X_dev, y_dev, 0.000005)
toc = time.time()
print('Vectorized loss: %e computed in %fs' % (loss_vectorized, toc - tic))

# The losses should match but your vectorized implementation should be much faster.
print('difference: %f' % (loss_naive - loss_vectorized))


print('make sure ve is right')

# Once you've implemented the gradient, recompute it with the code below
# and gradient check it with the function we provided for you

# Compute the loss and its gradient at W.
loss,grad=svm_loss_vectorized(W,X_dev,y_dev,0.0)
# Numerically compute the gradient along several randomly chosen dimensions, and
 def loss(self, X_batch, y_batch, reg):
     return svm_loss_vectorized(self.W, X_batch, y_batch, reg)
Beispiel #5
0
 def loss(self, X, y, reg):
     return svm_loss_vectorized(self.theta, X, y, reg)
Beispiel #6
0
X_val -= mean_image
X_test -= mean_image

# third: append the bias dimension of ones (i.e. bias trick) so that our SVM
# only has to worry about optimizing a single weight matrix W.
# Also, lets transform both data matrices so that each image is a column.
X_train = np.hstack([X_train, np.ones((X_train.shape[0], 1))]).T
X_val = np.hstack([X_val, np.ones((X_val.shape[0], 1))]).T
X_test = np.hstack([X_test, np.ones((X_test.shape[0], 1))]).T

#print X_train.shape, X_val.shape, X_test.shape

W = np.random.randn(10, 3073) * 0.0001
loss, grad = svm_loss_naive(W, X_train, y_train, 0.00001)
print 'loss:%f' % (loss, )
loss, grad = svm_loss_vectorized(W, X_train, y_train, 0.00001)
print 'loss:%f' % (loss, )

svm = LinearSVM()
tic = time.time()
loss_hist = svm.train(X_train,
                      y_train,
                      learning_rate=1e-7,
                      reg=1e4,
                      num_iters=1500,
                      verbose=True)
toc = time.time()
print 'That took %fs' % (toc - tic)

plt.plot(loss_hist)
plt.xlabel('Iteration number')
Beispiel #7
0
# 2. subtract the mean image from train and test data
X_train -= mean_image
X_val -= mean_image
X_test -= mean_image

# 3. append the bias dimension of ones (i.e. bias trick) so that our SVM
# only has to worry about optimizing a single weight matrix W.
# Also, transform data matrices so that each image is a column.
X_train = np.hstack([X_train, np.ones((X_train.shape[0], 1))]).T
X_val = np.hstack([X_val, np.ones((X_val.shape[0], 1))]).T
X_test = np.hstack([X_test, np.ones((X_test.shape[0], 1))]).T

print X_train.shape, X_val.shape, X_test.shape

W = np.random.randn(10, 3073) * 0.0001

with timer.Timer('SVM loss naive'):
    loss, grad = linear_svm.svm_loss_naive(W, X_train, y_train, 0.00001)
with timer.Timer('SVM loss vectorized'):
    loss, grad = linear_svm.svm_loss_vectorized(W, X_train, y_train, 0.00001)

classifier = linear_svm.LinearSVM()
#classifier = softmax.Softmax()
loss_hist = classifier.train(X_train, y_train, learning_rate=1e-7, reg=5e4,
                       num_iters=800, verbose=True)

y_train_pred = classifier.predict(X_train)
print 'training accuracy: %f' % (np.mean(y_train == y_train_pred), )
y_val_pred = classifier.predict(X_val)
print 'validation accuracy: %f' % (np.mean(y_val == y_val_pred), )
Beispiel #8
0
 def test_svm_loss_vectorized_loss(self):
     loss, _ = svm_loss_vectorized(self.weights, self.x, self.y, self.reg)
     np.testing.assert_allclose(loss, self.expected)
Beispiel #9
0

##################################################################################
#     create some artificial data: matrix W, input vector X. Compute scores.     #
##################################################################################

#  row i in W is the classifier for class i
W = np.array([[0.01, -0.05, 0.1,  0.05, 0.01],
              [0.7,   0.2,  0.05, 0.16, 0.7 ],
              [0.0,  -0.45, -0.2, 0.03, 0.5]])
print '\n    The weights are: \n\n{}'.format(W)
#print '\n      W.shape is {}'.format(W.shape)

# each row in X is an image
X = np.array([[-15, 22, -44, 56, 44],
              [-34, 55,  19, 22, 23]]).T; #X.shape = (2,4)
#print '\nX.shape is {}'.format(X.shape)
print '\nThe input vector(s) X: \n{}'.format(X); print '\n'

# ith element in y is the correct class label for the ith image
y = np.array([1, 0])

#loss, grad = svm2.svm_loss_naive(W, X, y, 0)
loss, grad = svm.svm_loss_naive(W.T, X.T, y, 0)
print 'loss is: {}'.format(loss)
print 'gradeint is: \n{}'.format(grad)

#loss, grad = svm2.svm_loss_vectorized(W, X, y, 0)
loss, grad = svm.svm_loss_vectorized(W.T, X.T, y, 0)
print 'loss is: {}'.format(loss)
print 'gradeint is: \n{}'.format(grad)
Beispiel #10
0
# 3. append the bias dimension of ones (i.e. bias trick) so that our SVM
# only has to worry about optimizing a single weight matrix W.
# Also, transform data matrices so that each image is a column.
X_train = np.hstack([X_train, np.ones((X_train.shape[0], 1))]).T
X_val = np.hstack([X_val, np.ones((X_val.shape[0], 1))]).T
X_test = np.hstack([X_test, np.ones((X_test.shape[0], 1))]).T

print X_train.shape, X_val.shape, X_test.shape

W = np.random.randn(10, 3073) * 0.0001

with timer.Timer('SVM loss naive'):
    loss, grad = linear_svm.svm_loss_naive(W, X_train, y_train, 0.00001)
with timer.Timer('SVM loss vectorized'):
    loss, grad = linear_svm.svm_loss_vectorized(W, X_train, y_train, 0.00001)

classifier = linear_svm.LinearSVM()

# Note: the softmax classifier works but it's slow (I only have the
# non-vectorized version implemented so far). Therefore it remains commented out
# by default.
#classifier = softmax.Softmax()

loss_hist = classifier.train(X_train,
                             y_train,
                             learning_rate=1e-7,
                             reg=5e4,
                             num_iters=800,
                             verbose=True)
Beispiel #11
0
# generate a random SVM weight matrix of small numbers
W = np.random.randn(3073, 10) * 0.0001

loss, grad = svm_loss_naive(W, X_dev, y_dev, 0.000005)
print('loss: %f' % (loss, ))

# Next implement the function svm_loss_vectorized; for now only compute the loss;
# we will implement the gradient in a moment.
tic = time.time()
loss_naive, grad_naive = svm_loss_naive(W, X_dev, y_dev, 0.000005)
toc = time.time()
print('Naive loss: %e computed in %fs' % (loss_naive, toc - tic))

from linear_svm import svm_loss_vectorized
tic = time.time()
loss_vectorized, grad_vectorized = svm_loss_vectorized(W, X_dev, y_dev,
                                                       0.000005)
toc = time.time()
print('Vectorized loss: %e computed in %fs' % (loss_vectorized, toc - tic))
print('difference: %f' % (loss_naive - loss_vectorized))

# In the file linear_classifier.py, implement SGD in the function
# LinearClassifier.train() and then run it with the code below.
svm = LinearSVM()
tic = time.time()
loss_hist = svm.train(X_train,
                      y_train,
                      learning_rate=1e-7,
                      reg=2.5e4,
                      num_iters=1500,
                      verbose=True)
toc = time.time()