Edit: This list is now maintained in the rust-anthology repo.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def nnCostFunc(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lmbda): | |
| theta1 = np.reshape(nn_params[:hidden_layer_size*(input_layer_size+1)], (hidden_layer_size, input_layer_size+1), 'F') | |
| theta2 = np.reshape(nn_params[hidden_layer_size*(input_layer_size+1):], (num_labels, hidden_layer_size+1), 'F') | |
| m = len(y) | |
| ones = np.ones((m,1)) | |
| a1 = np.hstack((ones, X)) | |
| a2 = sigmoid(a1 @ theta1.T) | |
| a2 = np.hstack((ones, a2)) | |
| h = sigmoid(a2 @ theta2.T) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def nnGrad(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lmbda): | |
| initial_theta1 = np.reshape(nn_params[:hidden_layer_size*(input_layer_size+1)], (hidden_layer_size, input_layer_size+1), 'F') | |
| initial_theta2 = np.reshape(nn_params[hidden_layer_size*(input_layer_size+1):], (num_labels, hidden_layer_size+1), 'F') | |
| y_d = pd.get_dummies(y.flatten()) | |
| delta1 = np.zeros(initial_theta1.shape) | |
| delta2 = np.zeros(initial_theta2.shape) | |
| m = len(y) | |
| for i in range(X.shape[0]): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def checkGradient(nn_initial_params,nn_backprop_Params,input_layer_size, hidden_layer_size, num_labels,myX,myy,mylambda=0.): | |
| myeps = 0.0001 | |
| flattened = nn_initial_params | |
| flattenedDs = nn_backprop_Params | |
| n_elems = len(flattened) | |
| #Pick ten random elements, compute numerical gradient, compare to respective D's | |
| for i in range(10): | |
| x = int(np.random.rand()*n_elems) | |
| epsvec = np.zeros((n_elems,1)) | |
| epsvec[x] = myeps |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| theta_opt = opt.fmin_cg(maxiter = 50, f = nnCostFunc, x0 = nn_initial_params, fprime = nnGrad, \ | |
| args = (input_layer_size, hidden_layer_size, num_labels, X, y.flatten(), lmbda)) | |
| theta1_opt = np.reshape(theta_opt[:hidden_layer_size*(input_layer_size+1)], (hidden_layer_size, input_layer_size+1), 'F') | |
| theta2_opt = np.reshape(theta_opt[hidden_layer_size*(input_layer_size+1):], (num_labels, hidden_layer_size+1), 'F') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def predict(theta1, theta2, X, y): | |
| m = len(y) | |
| ones = np.ones((m,1)) | |
| a1 = np.hstack((ones, X)) | |
| a2 = sigmoid(a1 @ theta1.T) | |
| a2 = np.hstack((ones, a2)) | |
| h = sigmoid(a2 @ theta2.T) | |
| return np.argmax(h, axis = 1) + 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def randInitializeWeights(L_in, L_out): | |
| epsilon = 0.12 | |
| return np.random.rand(L_out, L_in+1) * 2 * epsilon - epsilon | |
| initial_theta1 = randInitializeWeights(input_layer_size, hidden_layer_size) | |
| initial_theta2 = randInitializeWeights(hidden_layer_size, num_labels) | |
| # unrolling parameters into a single column vector | |
| nn_initial_params = np.hstack((initial_theta1.ravel(order='F'), initial_theta2.ravel(order='F'))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| import numpy as np | |
| from utils import plot_images | |
| from torchvision import datasets | |
| from torchvision import transforms | |
| from torch.utils.data.sampler import SubsetRandomSampler | |
| def get_train_valid_loader(data_dir, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def make_weights_for_balanced_classes(images, nclasses): | |
| count = [0] * nclasses | |
| for item in images: | |
| count[item[1]] += 1 | |
| weight_per_class = [0.] * nclasses | |
| N = float(sum(count)) | |
| for i in range(nclasses): | |
| weight_per_class[i] = N/float(count[i]) | |
| weight = [0] * len(images) | |
| for idx, val in enumerate(images): |