def main():
    train_set, test_set = getDataSets()

    params = [[0.0001] * 3073] * 10
    loss = np.array([])

    for i in range(100):
        print("epoch: {}".format(i + 1))

        params, l = smModule.train(train_set, params, 100, 10, 10**(-9))
        loss = np.append(loss, l)
        loss = loss.flatten()
        # print(loss)
        # print(params)
        score = smModule.score(test_set[0], test_set[1], params)
        print("accuracy: {}".format(score))
        plt.plot(loss)
        plt.show()
def main():
    train_set, test_set = getDataSets()
    num_class = 10

    params = [[0] * 785] * num_class
    loss = np.array([])

    for i in range(100):
        print("epoch: {}".format(i + 1))

        params, l = smModule.train(train_set, params, 1, num_class, 0.01)
        loss = np.append(loss, l)
        loss = loss.flatten()

        score = smModule.score(test_set[0], test_set[1], params)
        print("accuracy: {}".format(score))

        plt.plot(loss)
        plt.show()
Exemple #3
0
def main():
    fnameTrainIm = sys.argv[1]
    fnameTrainLb = sys.argv[2]
    fnameTestIm = sys.argv[3]
    fnameTestLb = sys.argv[4]

    # read training data and labels
    trainData, trainLabel = read(fnameTrainIm, fnameTrainLb)

    # initialize constants and parameters
    inputSize = 28 * 28
    numClasses = 10
    lam = 1e-4
    DEBUG = 0

    if DEBUG == 1:
        # compute difference between numerical gradient and computed grad on the subset
        subData, subLabel, subTheta, subPatchsize = subsample(
            trainData, trainLabel, rows, numClasses)
        subCost, subGrad = softmax.softmaxCost(subTheta, numClasses,
                                               subPatchsize, lam, subData,
                                               subLabel)
        numGrad, diff = numericalgrad(subTheta, numClasses, subPatchsize, lam,
                                      subData, subLabel, subGrad)
        print "The diff is", diff

    # train regression code
    thetaOpt = softmax.train(numClasses, inputSize, lam, trainData, trainLabel)

    # predict labels for test data
    testData, testLabel = read(fnameTestIm, fnameTestLb)
    pred = softmax.predict(thetaOpt, testData, numClasses, inputSize)

    # report accuracy of softmax regression
    accuracy = 100 * np.sum(testLabel == pred) / len(testLabel)
    print "Accuracy is {0:.2f}".format(accuracy)
Exemple #4
0
                                 jac=True,
                                 options={'maxiter': 400})
sae2_opt_theta = result.x

print result

##======================================================================
'''
STEP 4: Train the softmax classifier
'''
# use features from the second hidden layer to train the softmax classifier
sae2_features = sparseAuto.autoencoder(sae2_opt_theta, hidden_size_L2,
                                       hidden_size_L1, sae1_features)

# optimize parameters
softmax_theta = softmax.train(num_classes, hidden_size_L2, lambda_,
                              sae2_features, train_labels)

##======================================================================
'''
STEP 5: Finetune softmax model
'''

# Initialize the stack using the parameters learned
stack = [dict() for i in range(2)]
stack[0]['w'] = sae1_opt_theta[0:hidden_size_L1 * input_size].reshape(
    hidden_size_L1, input_size)
stack[0]['b'] = sae1_opt_theta[2 * hidden_size_L1 *
                               input_size:2 * hidden_size_L1 * input_size +
                               hidden_size_L1]
stack[1]['w'] = sae2_opt_theta[0:hidden_size_L1 * hidden_size_L2].reshape(
    hidden_size_L2, hidden_size_L1)
if __name__ == '__main__':
    file_path = r"C:\Study\test\bone\2"
    out_dir = "C:\\Study\\test\\softmax_norm_test"
    # weights_file = "weights.npy"
    # w0 = loadWeights(weights_file)
    # w0 = np.load(weights_file)
    # print(np.shape(w0))
    # print(w0)
    inputfile = "data.txt"
    feature, label = loadData(inputfile)
    feature = handleHistogram(feature)
    # print(np.shape(feature), np.shape(label))
    # print(feature)
    k = 256
    w0 = train(feature, label, k, 100000, 0.1)

    fp = open("pickle_weight_test.dat", "wb")
    pickle.dump(w0, fp)
    fp.close()

    handle(file_path, out_dir, (45,-45,45,-45), w0)

    # # 分割评估
    # file_path = "C:\\Study\\test\\100-gt" # 标准分割图像目录路径
    # out_dir = "C:\\Study\\test\\est_results" #结果保存目录
    # print("softmax")
    # file_path_2 = "C:\\Study\\test\\softmax_no_norm_no_limited" # 方法一得到分割图像路径
    # res = batchProcess(file_path, file_path_2)
    # printEst(res, "softmax")
    # saveEst(res, "softmax", out_dir)
# rates and regularization strengths; if you are careful you should be able to
# get a classification accuracy of over 0.35 on the validation set.
results = {}
best_val = -1
best_softmax = None
learning_rates = [1e-7, 5e-7]
regularization_strengths = [2.5e4, 5e4]

for lr in learning_rates:
    for rs in regularization_strengths:
        softmax = liner_classifiers_total.Softmax()

        loss_history = softmax.train(X_train,
                                     Y_train,
                                     lr,
                                     rs,
                                     num_iters=1500,
                                     batch_size=100,
                                     verbose=True)
        Y_train_pred = softmax.predict(X_train)
        train_accuracy = np.mean(Y_train == Y_train_pred)

        Y_val_pred = softmax.predict(X_val)
        val_accuracy = np.mean(Y_val == Y_val_pred)

        if val_accuracy > best_val:
            best_val = val_accuracy
            best_softmax = softmax
        results[(lr, rs)] = train_accuracy, val_accuracy

# print out result
Exemple #7
0
def main():
    """
    Step 0: Initialize parameter values for sparse autoencoder and softmax regression.
    """

    row_size = 28
    visible_size = row_size**2
    num_labels = 5
    hidden_size = 200
    rho = 0.1
    lambda_ = 3e-3
    beta = 3
    max_iter = 400

    debug = 0
    """
    Step 1: Load data from the MNIST database

    Load the training set data and sort the training data into labeled and unlabeled sets.
    """

    fIm = sys.argv[1]
    fLb = sys.argv[2]

    # read training data and labels
    data, label = read(fIm, fLb)

    # simulate a labeled and unlabeled set
    # set the numbers that will be used for the unlabeled versus labeled sets
    ixu = np.argwhere(label >= 5).flatten()
    ixl = np.argwhere(label < 5).flatten()

    # build a training and test set -> separate half the labeled set to use as test data
    numTrain = round(ixl.shape[0] / 2)  # half the examples in the set

    train_val = ixl[0:numTrain]
    test_val = ixl[numTrain:]

    unlabeled_set = label[ixu]
    unlabeled_data = data[:, ixu]

    train_data = data[:, train_val]
    train_set = label[train_val]

    test_data = data[:, test_val]
    test_set = label[test_val]

    print data.shape
    # output some statistics
    print "# of examples in unlabeled set: {0:6d}".format(len(unlabeled_set))
    print "# of examples in supervised training set: {0:6d}".format(
        len(train_set))
    print "# of examples in supervised test set: {0:6d}".format(len(test_set))
    """
    Optional Step: Test Sparse Autoencoder
    """
    if debug == 1:
        subdata, sublabel, sub_visible, sub_hidden = sparseAuto.subsample(
            train_data, train_set, row_size)

        sub_theta = sparseAuto.initializeParameters(sub_hidden, sub_visible)

        sub_grad, sub_cost = sparseAuto.costfun(sub_theta, sub_visible,
                                                sub_hidden, lambda_, rho, beta,
                                                subdata)

        numGrad, diff = sparseAuto.numgrad(sub_theta, sub_grad, sub_visible,
                                           sub_hidden, lambda_, rho, beta,
                                           subdata)

        print diff
    """
    Step 2: Train Sparse Autoencoder
    """

    print 'Step 2'
    opt_theta = sparseAuto.train(visible_size, hidden_size, lambda_, rho, beta,
                                 unlabeled_data)
    """
    Step 3: Extract Features from the Supervised Data Set
    """
    print 'Step 3'

    train_features = sparseAuto.autoencoder(opt_theta, hidden_size,
                                            visible_size, train_data)

    test_features = sparseAuto.autoencoder(opt_theta, hidden_size,
                                           visible_size, test_data)
    """
    Step 4: Train the softmax classifier
    """
    print 'Step 4'
    lam = 1e-4

    thetaOpt = softmax.train(num_labels, hidden_size, lam, train_features,
                             train_set)
    """
    Step 5: Testing softmax classfier
    """
    print 'Step 5'
    pred = softmax.predict(thetaOpt, test_features, num_labels, hidden_size)

    print pred[0:15]
    print test_set[0:15]
    # report accuracy of softmax regression
    accuracy = 100 * np.sum(test_set == pred) / len(test_set)
    print "Accuracy is {0:.2f}".format(accuracy)
Exemple #8
0
def main():
    """
    Step 0: Initialization
    """
    # Initialize parameters for the exercise

    image_dim = 64
    image_channels = 3

    patch_dim = 8
    num_patches = 50000

    visible_size = patch_dim * patch_dim * image_channels
    output_size = visible_size
    hidden_size = 400

    epsilon = 0.1
    pool_dim = 19

    debug = 0
    """
    Step 1: Train a sparse autoencoder (with a linear decoder)
    """
    # load data from linear decoder exercise
    opt_theta = np.load('opt_theta.npy')
    zca_white = np.load('zca_white.npy')
    mean_patch = np.load('mean_patch.npy')

    # unpack W and b
    W = opt_theta[0:hidden_size * visible_size].reshape(
        hidden_size, visible_size)
    b = opt_theta[2 * hidden_size *
                  visible_size:2 * hidden_size * visible_size + hidden_size]
    """
    Step 2a: Implement convolution
    """
    # read in train data from mat file
    mat_contents = sio.loadmat('stlTrainSubset.mat')
    train_images = mat_contents['trainImages']
    train_labels = mat_contents['trainLabels']
    num_train_images = mat_contents['numTrainImages'][0][0]

    # read in test data from mat file
    mat_contents = sio.loadmat('stlTestSubset.mat')
    test_images = mat_contents['testImages']
    test_labels = mat_contents['testLabels']
    num_test_images = mat_contents['numTestImages'][0][0]

    # use only the first 8 images for testing
    conv_images = train_images[:, :, :, 0:8]

    # use only the first 8 images to test
    convolved_features = cnn.convolve(patch_dim, hidden_size, conv_images, W,
                                      b, zca_white, mean_patch)
    if debug == 1:
        """
        Step 2b: Check your convolution
        """

        for i in range(1000):
            feature_num = np.random.randint(0, hidden_size)
            image_num = np.random.randint(0, 8)
            image_row = np.random.randint(0, image_dim - patch_dim + 1)
            image_col = np.random.randint(0, image_dim - patch_dim + 1)
            patch = conv_images[image_row:image_row + patch_dim,
                                image_col:image_col + patch_dim, :, image_num]
            patch = np.concatenate(
                (patch[:, :,
                       0].flatten(), patch[:, :,
                                           1].flatten(), patch[:, :,
                                                               2].flatten()))
            patch = np.reshape(patch, (patch.size, 1))
            patch = patch - np.tile(mean_patch,
                                    (patch.shape[1], 1)).transpose()
            patch = zca_white.dot(patch)
            features = sparseAuto.autoencoder(opt_theta, hidden_size,
                                              visible_size, patch)

            if abs(features[feature_num, 0] -
                   convolved_features[feature_num, image_num, image_row,
                                      image_col]) > 1e-9:
                print 'convolved feature does not activation from autoencoder'
                print 'feature number:', feature_num
                print 'image number:', image_num
                print 'image row:', image_row
                print 'image column:', image_col
                print 'convolved feature:', convolved_features[feature_num,
                                                               image_num,
                                                               image_row,
                                                               image_col]
                print 'sparse AE feature:', features[feature_num, 0]
                sys.exit(
                    'convolved feature does not match activation from autoencoder'
                )

        print('congrats! convolution code passed the test')
        """
        Step 2c: Implement Pooling
        """
        # pooled_features = cnn.pooling(pool_dim, convolved_features)
        """
        Step 2d: Checking your pooling
        """
        test_matrix = np.arange(64).reshape(8, 8)
        expected_matrix = np.array(
            [[np.mean(test_matrix[0:4, 0:4]),
              np.mean(test_matrix[0:4, 4:8])],
             [np.mean(test_matrix[4:8, 0:4]),
              np.mean(test_matrix[4:8, 4:8])]])
        test_matrix = test_matrix.reshape(1, 1, 8, 8)
        pooled_features = cnn.pooling(4, test_matrix)

        if not (pooled_features == expected_matrix).all():
            print 'pooling incorrect'
            print 'expected:'
            pprint.pprint(expected_matrix)
            print 'got:'
            pprint.pprint(pooled_features)
        else:
            print "congrats! pooling code passed the test"
    """
    Step 3: Convolve and pool with the data set
    """
    step_size = 50
    assert hidden_size % step_size == 0

    pooled_features_train = np.zeros(
        (hidden_size, num_train_images,
         int(np.floor((image_dim - patch_dim + 1) / pool_dim)),
         int(np.floor((image_dim - patch_dim + 1) / pool_dim))))

    pooled_features_test = np.zeros(
        (hidden_size, num_test_images,
         int(np.floor((image_dim - patch_dim + 1) / pool_dim)),
         int(np.floor((image_dim - patch_dim + 1) / pool_dim))))

    start_time = time.time()

    for conv_part in range(hidden_size / step_size):

        feature_start = conv_part * step_size
        feature_end = (conv_part + 1) * step_size

        print 'Step:', conv_part, 'Features', feature_start, 'to', feature_end
        Wt = W[feature_start:feature_end, :]
        bt = b[feature_start:feature_end]

        print 'Convolving and pooling train images'
        convolved_features_this = cnn.convolve(patch_dim, step_size,
                                               train_images, Wt, bt, zca_white,
                                               mean_patch)
        pooled_features_this = cnn.pooling(pool_dim, convolved_features_this)
        pooled_features_train[
            feature_start:feature_end, :, :, :] = pooled_features_this

        print 'Elapsed time is', str(
            datetime.timedelta(seconds=time.time() - start_time))
        print 'Convolving and pooling test images'

        convolved_features_this = cnn.convolve(patch_dim, step_size,
                                               test_images, Wt, bt, zca_white,
                                               mean_patch)
        pooled_features_this = cnn.pooling(pool_dim, convolved_features_this)
        pooled_features_test[
            feature_start:feature_end, :, :, :] = pooled_features_this

        print 'Elapsed time is', str(
            datetime.timedelta(seconds=time.time() - start_time))

    np.save('pooled_features_train.npy', pooled_features_train)
    np.save('pooled_features_test.npy', pooled_features_test)
    print 'Elapsed time is', str(
        datetime.timedelta(seconds=time.time() - start_time))
    """
    Step 4: Use pooled features for classification
    """
    # set up parameters for softmax
    softmax_lambda = 1e-4
    num_classes = 4

    # reshape the pooled_features to form an input vector for softmax
    softmax_x = np.transpose(pooled_features_train, [0, 2, 3, 1])
    softmax_x = softmax_x.reshape(
        (pooled_features_train.size / num_train_images, num_train_images))
    softmax_y = train_labels.flatten() - 1

    softmax_opt_theta = softmax.train(
        num_classes, pooled_features_train.size / num_train_images,
        softmax_lambda, softmax_x, softmax_y)
    np.save('theta_opt_theta.npy', opt_theta)
    """
    Step 5: Test Classifier
    """
    softmax_x = np.transpose(pooled_features_test, [0, 2, 3, 1])
    softmax_x = softmax_x.reshape(
        (pooled_features_test.size / num_test_images, num_test_images))
    softmax_y = test_labels.flatten() - 1
    pred = softmax.predict(softmax_opt_theta, softmax_x, num_classes,
                           pooled_features_train.size / num_train_images)

    accuracy = 100 * np.sum(softmax_y == pred) / len(softmax_y)
    print "Accuracy is {0:.2f}".format(accuracy)
Exemple #9
0
  print 'Iter : %s   Training error = %s' % (iter+1, trainError)
  
  testError = evaluatePredictor(testExamples, lambda(x) : (1 if dotProduct(featureExtractor(x), weights) >= 0 else -1))
  print "=" * 30
  print "Result"
  print 'Iter : %s   Test error = %s' % (iter+1, testError)
  return weights

def evaluatePredictor(examples, predictor):
    '''
    predictor: a function that takes an x and returns a predicted y.
    Given a list of examples (x, y), makes predictions based on |predict| and returns the fraction
    of misclassiied examples.
    '''
    error = 0
    for x, y in examples:
        if predictor(x) != y:
            error += 1
    return 1.0 * error / len(examples)

if __name__ == '__main__':
  # Load training data
  trainingData = loadTrainingData()
  testData = loadTestData()
  trainExamples = [(data, 1 if data['stars'] > 3 else -1) for data in trainingData]
  testExamples = [(data, 1 if data['stars'] > 3 else -1) for data in testData]
  perceptron(trainExamples, testExamples, extractWordAndIdFeatures)
  trainExamples = [(data, data['stars']) for data in trainingData]
  testExamples = [(data, data['stars']) for data in testData]
  softmax.train(trainExamples, testExamples, extractWordFeatures)
Exemple #10
0
        rel_error = abs(grad_numerical - grad_analytic) / (
            abs(grad_numerical) + abs(grad_analytic))
        print('numerical: %f analytic: %f, relative error: %e' %
              (grad_numerical, grad_analytic, rel_error))


#现在我们对加入了正则项的梯度进行检验
loss, grad = softmax.softmax_loss_vectorized(w, x_dev, y_dev, 0.0)
f = lambda w: softmax.softmax_loss_vectorized(w, x_dev, y_dev, 0.0)[0]
grad_numerical = grad_check_sparse(f, w, grad)

softmax = Softmax()  #创建对象,此时W为空
tic = time.time()
loss_hist = softmax.train(x_train,
                          y_train,
                          learning_rate=1e-7,
                          reg=2.5e4,
                          num_iters=1500,
                          verbose=True)  #此时svm对象中有W
toc = time.time()
print('that took %fs' % (toc - tic))
plt.plot(loss_hist)
plt.xlabel('iteration number')
plt.ylabel('loss value')
plt.show()
#训练完成之后,将参数保存,使用参数进行预测,计算准确率
y_train_pred = softmax.predict(x_train)
print('training accuracy: %f' % (np.mean(y_train == y_train_pred)))  #返回条件成立的占比
y_val_pred = softmax.predict(x_val)
print('validation accuracy: %f' % (np.mean(y_val == y_val_pred)))

#在拿到一组数据时一般分为训练集,开发集(验证集),测试集。训练和测试集都知道是干吗的,验证集在除了做验证训练结果外
Exemple #11
0
regularization_strengths = [7.5e3, 1e4, 2.5e4, 5e4, 7.5e4, 1e5]

################################################################################
# TODO:                                                                        #
# Use the validation set to set the learning rate and regularization strength. #
# This should be identical to the validation that you did for the SVM; save    #
# the best trained softmax classifer in best_softmax.                          #
################################################################################
# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

for learning_rate in learning_rates:
    for regularization_strength in regularization_strengths:
        softmax = linear_classifier.Softmax()
        softmax.train(X_train,
                      y_train,
                      learning_rate=learning_rate,
                      reg=regularization_strength,
                      num_iters=1500)
        y_train_pred = softmax.predict(X_train)
        y_val_pred = softmax.predict(X_val)

        train_acc = np.mean(y_train == y_train_pred)
        val_acc = np.mean(y_val == y_val_pred)

        results[learning_rate, regularization_strength] = train_acc, val_acc

        if best_val < val_acc:
            best_val = val_acc
            best_softmax = softmax
# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
Exemple #12
0
    print "Result"
    print 'Iter : %s   Test error = %s' % (iter + 1, testError)
    return weights


def evaluatePredictor(examples, predictor):
    '''
    predictor: a function that takes an x and returns a predicted y.
    Given a list of examples (x, y), makes predictions based on |predict| and returns the fraction
    of misclassiied examples.
    '''
    error = 0
    for x, y in examples:
        if predictor(x) != y:
            error += 1
    return 1.0 * error / len(examples)


if __name__ == '__main__':
    # Load training data
    trainingData = loadTrainingData()
    testData = loadTestData()
    trainExamples = [(data, 1 if data['stars'] > 3 else -1)
                     for data in trainingData]
    testExamples = [(data, 1 if data['stars'] > 3 else -1)
                    for data in testData]
    perceptron(trainExamples, testExamples, extractWordAndIdFeatures)
    trainExamples = [(data, data['stars']) for data in trainingData]
    testExamples = [(data, data['stars']) for data in testData]
    softmax.train(trainExamples, testExamples, extractWordFeatures)