def df(x,y,W,b): p = part2.forward(x, W, b) grad_w = np.matmul((p-y), x.T).T grad_b = np.sum((p-y), 1).reshape((10,1)) return grad_w, grad_b
def part5(alpha_w, alpha_b, _max_iter, printing): M = loadmat("mnist_all.mat") results_val = [] results_train = [] results_test = [] x = [] train_set, train_set_sol, val_set, val_set_sol, test_set, test_set_sol = part4.generate_sets( M, 5000) W_init = np.random.rand(784, 10) step = 20 for i in range(step, _max_iter, step): print(i) W, b = grad_descent(part3.f, part3.df, train_set, train_set_sol, W_init, alpha_w, alpha_b, i, 0.9, True) y_pred = part2.forward(val_set, W, b) results_val += [ np.mean(np.argmax(y_pred, 0) == np.argmax(val_set_sol, 0)) ] y_pred = part2.forward(train_set, W, b) results_train += [ np.mean(np.argmax(y_pred, 0) == np.argmax(train_set_sol, 0)) ] y_pred = part2.forward(test_set, W, b) results_test += [ np.mean(np.argmax(y_pred, 0) == np.argmax(test_set_sol, 0)) ] print results_test print results_train print results_val x += [i] pickle.dump(W, open("part4W_shichen.p", "wb")) plt.plot(x, results_val, 'y', label="validation set") plt.plot(x, results_train, 'g', label="training set") plt.plot(x, results_test, 'r', label="test set") plt.title("Part 5 Learning Curve") plt.ylabel("accuracy") plt.xlabel("iterations") plt.show()
def test2(database, solutions, W, b): correct = 0 database = database.T solutions = solutions.T for i in range(len(database)): guess = part2.forward(database[i], W, b) if np.argmax(guess) == np.argmax(solutions[i]): correct += 1 return correct / float(len(database))
def test(database, size, W, b): test_set = [] correct = 0 for i in range(size): rand_dgt = np.random.random_integers(0, 9) test_set += [database["test" + str(rand_dgt)][i]] guess = part2.forward(test_set[i], W, b) if guess == rand_dgt: correct += 1 return correct / size
def f(x, W, b, y): p = part2.forward(x,W,b) return -sum(y*log(p)) / x.shape[1]