コード例 #1
0
ファイル: test_linear_svm.py プロジェクト: lkuligin/cs231n
 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)
コード例 #2
0
X_test -= mean_image
X_dev -= 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.
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))])

print(X_train.shape, X_val.shape, X_test.shape, X_dev.shape)

# generate a random SVM weight matrix of small numbers
W = np.random.randn(3073, 10) * 0.0001

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

# 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 = linear_svm.svm_loss_naive(W, X_dev, y_dev, 0.0)

# Numerically compute the gradient along several randomly chosen dimensions, and
# compare them with your analytically computed gradient. The numbers should match
# almost exactly along all dimensions.
f = lambda w: linear_svm.svm_loss_naive(w, X_dev, y_dev, 0.0)[0]
grad_numerical = gradient_check.grad_check_sparse(f, W, grad)

# do the gradient check once again with regularization turned on
コード例 #3
0
# second: subtract the mean image from train and test data
X_train=X_train-mean_image
X_val=X_val-mean_image
X_test=X_test-mean_image
X_dev=X_dev-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.
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))))
print(X_train.shape,X_val.shape,X_test.shape,X_dev.shape)

# 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,))


# 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_naive(W,X_dev,y_dev,0.0)
# Numerically compute the gradient along several randomly chosen dimensions, and
# compare them with your analytically computed gradient. The numbers should match
# almost exactly along all dimensions.
f = lambda w: svm_loss_naive(w, X_dev, y_dev, 0.0)[0]
grad_numerical = grad_check_sparse(f, W, grad)

# do the gradient check once again with regularization turned on
コード例 #4
0
ファイル: svm.py プロジェクト: wli33/cs231n
# second: subtract the mean image from train and test data
X_train -= mean_image
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)
コード例 #5
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), )
コード例 #6
0
ファイル: test_linear_svm.py プロジェクト: lkuligin/cs231n
 def test_svm_loss_naive_gradient(self):
     loss, grad = svm_loss_naive(self.weights, self.x, self.y, self.reg)
     f = lambda w: svm_loss_naive(self.weights, self.x, self.y, self.reg)[0]
     grad_numerical = test_utils.grad_check_sparse(f, self.weights, grad)
コード例 #7
0
ファイル: test_linear_svm.py プロジェクト: lkuligin/cs231n
 def test_svm_loss_naive_loss(self):
     loss, _ = svm_loss_naive(self.weights, self.x, self.y, self.reg)
     np.testing.assert_allclose(loss, self.expected)
コード例 #8
0
ファイル: test_other.py プロジェクト: david-dobor/cs231n

##################################################################################
#     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)
コード例 #9
0
ファイル: run_svm.py プロジェクト: LiuFang816/SALSTM_py_data
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()

# 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,
コード例 #10
0
X_dev -= 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.
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))])
print(X_train.shape, X_val.shape, X_test.shape, X_dev.shape)

# Evaluate the naive implementation of the loss we provided for you:

# 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))