def load_all_data(): X_train, y_train = load_mnist('training') X_test, y_test = load_mnist('testing') # Reshape the image data into rows X_train = np.reshape(X_train, (X_train.shape[0], -1)) X_test = np.reshape(X_test, (X_test.shape[0], -1)) return X_train, y_train, X_test, y_test
import numpy as np import tensorflow as tf from load_mnist import * from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # Load Mnist Data u_bound = 10 mnist_input_size = 784 ## 28 * 28 X_train, y_train = load_mnist('training', range(0, u_bound)) X_test, y_test = load_mnist('testing', range(0, u_bound)) # Get and Scramble training set, with n items per category training_set = [] training_labels = [] n = 5 for j in range(0, u_bound): hld = np.array(list(np.where(y_train == np.float64(j)))).flatten() training_set = training_set + list( np.random.choice(hld, size=n, replace=False)) training_labels = training_labels + [np.float64(j)] * n training_set = np.array(training_set) training_labels = np.array(training_labels) p = np.random.permutation( len(training_set) ) ## This scrambles the training set/labels, which would otherwise be in order training_set = training_set[p] ## this is a list of indices training_labels = training_labels[p] ## this is a list of generated labels
history = best_network.evaluate(X_test,Y_test) fig = plt.figure() plt.plot(loss_log) plt.title('Final Training Loss over interation') plt.xlabel('Iternation') plt.ylabel('Loss') plt.savefig('./training_loss_figure.png') plt.show() print("Final Testing accuracy: "+ str(history[1])) if __name__ == '__main__': # load dataset X_train, Y_train = load_mnist("training") X_test, Y_test = load_mnist("testing") # normalize the input data X_train = X_train / 255. X_test = X_test / 255. # reshape image img_rows = 28 img_cols = 28 X_train = np.reshape(X_train, [np.shape(X_train)[0],img_rows, img_cols,1 ]) X_test = np.reshape(X_test, [np.shape(X_test)[0],img_rows, img_cols,1 ]) # one-hot encoding for labels Y_train = tf.keras.utils.to_categorical(Y_train, 10) Y_test = tf.keras.utils.to_categorical(Y_test, 10)
# -*- coding: utf-8 -*- """ Created on @author: fame """ from load_mnist import * import hw1_linear as mlBasics import numpy as np # Read in training and test data X_train, y_train = load_mnist('training', [0, 1]) X_train = np.reshape(X_train, (X_train.shape[0], -1)) X_train = np.divide(X_train, 256) X_test, y_test = load_mnist('training', [0, 1]) X_test = np.reshape(X_test, (X_test.shape[0], -1)) X_test = np.divide(X_test, 256) # Starting values for weights W and bias b W0 = np.zeros(X_train.shape[1]) b0 = 0 # Optimization num_iters = 100 eta = 0.001 pdb.set_trace() W, b = mlBasics.train(X_train, y_train, W0, b0, num_iters, eta) # Test on test data yhat = mlBasics.predict(X_test, W, b) >= .5
import numpy as np import chainer #from chainer import serializers import chainer.functions as F from chainer import FunctionSet, Variable, optimizers, cuda from load_mnist import * cuda.get_device(0).use() xp = cuda.cupy N_train_labeled = 100 N_train_unlabeled = 60000 N_test = 10000 train_l, train_ul, test_set = load_mnist(scale=1.0/256.0, shift=0.0, N_train_labeled=N_train_labeled, N_train_unlabeled=N_train_unlabeled, N_test=N_test) class VanillaComb(chainer.Chain): def __init__(self, n): super(VanillaComb, self).__init__( b0 = F.Parameter(xp.zeros((1, n)).astype(np.float32)), w0z = F.Parameter(xp.ones((1, n)).astype(np.float32)), w0u = F.Parameter(xp.zeros((1, n)).astype(np.float32)), w0zu = F.Parameter(xp.zeros((1, n)).astype(np.float32)), ws = F.Parameter(xp.ones((1, n)).astype(np.float32)), b1 = F.Parameter(xp.zeros((1, n)).astype(np.float32)), w1z = F.Parameter(xp.ones((1, n)).astype(np.float32)), w1u = F.Parameter(xp.zeros((1, n)).astype(np.float32)), w1zu = F.Parameter(xp.zeros((1, n)).astype(np.float32)), )
fh.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.INFO) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) logger.addHandler(fh) logger.addHandler(ch) dim_in = 28 * 28 dim_hidden = 1024 dim_out = 10 dims = [dim_in, dim_hidden, dim_out] train_x_total, train_y_total = load_mnist('training') train_x_total = train_x_total.reshape([-1, dim_in]) train_y_total = train_y_total.reshape([-1, 10]) labels = ['CP-ALT', 'CP-ALT-lambda', 'BP', 'BP-adam', 'BP-rms', 'BP-mom'] def draw(learning_rate, n_examples, repeats): gate = Tanh() runs = 1 cp_alt = CrossPropAlt(dim_in, dim_hidden, dim_out, learning_rate, gate, output_layer='CE',
# -*- coding: utf-8 -*- import sys, os sys.path.append(os.pardir) # 親ディレクトリのファイルをインポートするための設定 import numpy as np import matplotlib.pyplot as plt from 3-2_data_mnist import load_mnist from 4-3_two_layer_net import TwoLayerNet # MNISTの訓練データとテストデータ読み込み (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, one_hot_label=True) # 2層のニューラルワーク生成 network = TwoLayerNet(input_size=784, hidden_size=50, output_size=10) # ハイパーパラメーター設定 iters_num = 10000 # 繰り返しの回数を適宜設定する train_size = x_train.shape[0] # 訓練データのサイズ設定 batch_size = 100 # バッチサイズ learning_rate = 0.1 # 学習率 # 結果の記録リスト train_loss_list = [] train_acc_list = [] test_acc_list = [] iter_per_epoch = max(train_size / batch_size, 1) # 1エポック当たりの繰り返し数 for i in range(iters_num): batch_mask = np.random.choice(train_size, batch_size) x_batch = x_train[batch_mask] t_batch = t_train[batch_mask]
# -*- coding: utf-8 -*- import sys sys.path.append("C:/Study/Python/Deep-learning from scratch") import numpy as np from 3-2_data_mnist import load_mnist # データの読み込み (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, flatten=True, one_hot_label=False) #要素数を表示 print(x_train.shape) print(t_train.shape) #1行目の要素数をsizeに設定 train_size = x_train.shape[0] print("train_size: " +str(train_size)) batch_size = 10 batch_mask = np.random.choice(train_size, batch_size) print("batch_mask: ") print(batch_mask) x_batch = x_train[batch_mask] t_batch = t_train[batch_mask]
from keras.utils import to_categorical import numpy as np import time from tf_nn import NeuralNetwork from load_mnist import * import pandas as pd mnist_path = 'Data\\MNIST' mnist_tr = load_mnist(dataset='training', path = mnist_path) mnist_test = load_mnist(dataset='testing', path = mnist_path) n_feature = 784 n_class = 10 # shrink_order = 2 # lambda_weight = .01 # n_hlayer = 2 # n_hidden = [100, 100] # lr = 0.001 # activation = 'sigmoid' # summary = pd.DataFrame(columns=['shrink', 'lambda', 'n_layer', 'n_hidden', 'learning rate', 'decay', 'activation']) summary = pd.DataFrame() start_time = time.time() for i in range(20): shrink_order = np.random.choice([1])
tag = 'online_MNIST' logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) fh = logging.FileHandler('log/%s.txt' % tag) fh.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.INFO) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) logger.addHandler(fh) logger.addHandler(ch) datapath = './dataset/mnist' train_x, train_y = load_mnist('training', path=datapath) dim_in = 28 * 28 dim_hidden = 1024 dim_out = 10 train_examples = 5000 train_x = train_x[:train_examples, :].reshape([-1, dim_in]) train_y = np.argmax(train_y[:train_examples, :], axis=1) def bp_network(lr): return NaiveNet(dim_in, dim_hidden, dim_out, lr, gpu=True) def smd_network(lr):
# -*- coding: utf-8 -*- import numpy as np #data mnistをインポート from 3-2_data_mnist import load_mnist #リサイズやトリミング、回転、反転など基本的な処理はPillow from PIL import Image def img_show(img): pil_img = Image.fromarray(np.uint8(img)) pil_img.show() #「(訓練画像、訓練ラベル)、(テスト画像、テストラベル)」という形式、引数は「一次配列にして、正規化はせず (x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False) img = x_train[0] label = t_train[0] print(label) # 5 print(img.shape) # (784,) img = img.reshape(28, 28) print(img.shape) # (28, 28) img_show(img) print(x_train.shape) #(60000,784) print(t_train.shape) #(60000,) print(x_test.shape) #(10000, 784) print(t_test.shape) #(10000,)
def get_data(): (x_train, t_train), (x_test, t_test)= load_mnist(normalize=True, flatten=True, one_hot_label=False) return x_test, t_test
from datetime import datetime import os.path import time import numpy as np from six.moves import xrange # pylint: disable=redefined-builtin import math #from nets import * from random import * from dl_feed import * from load_mnist import * import spams if __name__ == "__main__": images, labels = load_mnist() psize = 6 X = (get_all_patches_as_matrix(images, psize)[0:2000] - 177.5) / 177.5 param = { 'K': 100, # learns a dictionary with 100 elements 'lambda1': 0.15, 'numThreads': 4, 'batchsize': 400, 'iter': 1000 } ########## FIRST EXPERIMENT ########### tic = time.time() D = spams.trainDL(np.transpose(X), **param) tac = time.time() t = tac - tic
import numpy as np import tensorflow as tf from load_mnist import * import matplotlib.pyplot as pl # Load Mnist Data u_bound = 10 #mnist_input_size = 784 ## 28 * 28 Train_X, Train_Y = load_mnist('training', range(0, u_bound)) Test_X, Test_Y = load_mnist('testing', range(0, u_bound)) print(Train_X.shape, Train_Y.shape) print(Test_X.shape, Test_Y.shape) def getDataSubset(X_train, y_train, X_test, y_test, n=100, u_bound=10, mnist_input_size=784): # Get and Scramble training set, with n items per category training_set, test_set = [], [] training_labels, test_labels = [], [] for j in range(0, u_bound): hld = np.array(list(np.where(y_train == np.float64(j)))).flatten() training_set = training_set + list( np.random.choice(hld, size=n, replace=False)) training_labels = training_labels + [np.float64(j)] * n
logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) fh = logging.FileHandler('log/%s_%s_multi_gpu.txt' % (DATA_SET, NN_ARCH)) fh.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.INFO) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) logger.addHandler(fh) logger.addHandler(ch) if DATA_SET == 'MNIST': train_x, train_y = load_mnist('training') test_x, test_y = load_mnist('testing') elif DATA_SET == 'CIFAR': train_x, train_y, test_x, test_y = load_cifar10('cifar10') else: logger.error('No Dataset!') train_examples = train_x.shape[0] test_examples = test_x.shape[0] # train_examples = 1000 # test_examples = 200 train_x = train_x[:train_examples, :, :, :] train_y = train_y[:train_examples, :] test_x = test_x[:test_examples, :, :, :] test_y = test_y[:test_examples, :]
def main(): # Load data - ALL CLASSES X_train, y_train = load_mnist('training') X_test, y_test = load_mnist('testing') # collect all the sample indices samind = np.zeros(0) for i in range(0, 10): # get the 100 training samples from the 10 classes # filter out the indices, where label is i, gives an array with boolean values false and true val = np.isin(y_train, i) # filters the corresponding indices ind = np.array(np.where(val)) # make it 1 dimensional ind = np.reshape(ind, (ind.shape[1])) # randomly sample 100 indices s = np.random.choice(ind, 100, replace=False) # concatenate new sample results to existing ones samind = np.concatenate((samind, s), axis=0) samind = samind.astype(int) # Reshape the image data into rows X_train = np.reshape(X_train, (X_train.shape[0], -1)) X_test = np.reshape(X_test, (X_test.shape[0], -1)) # get the 1000 samples and corresponding labels samples_X = X_train[samind] samples_y = y_train[samind] # calculate predictions/accuracy tuples for k=1 and k=5 pred_acc_one = pred_accuracy(samples_X, samples_y, X_test, y_test, 1) pred_acc_five = pred_accuracy(samples_X, samples_y, X_test, y_test, 5) # Report results print("Accuracy for k=1: ", '{0:0.02f}'.format(pred_acc_one[1]), "of test examples classified correctly.") print("Accuracy for k=5: ", '{0:0.02f}'.format(pred_acc_five[1]), "of test examples classified correctly.") # calculate the confusion matrix cmat_k1 = confusion_matrix(y_test, pred_acc_one[0]) cmat_k5 = confusion_matrix(y_test, pred_acc_five[0]) print("Confusion matrix for k=1: ") print(cmat_k1) print("Confusion matrix for k=5: ") print(cmat_k5) # c) # Dict to receive the averages for the different k's avg_dict = {} # randomize the order in the sample set (currently it is sorted by label) #permutation = np.random.permutation(samples_X.shape[0]) #samples_X = samples_X[permutation] #samples_y = samples_y[permutation] # commented because the result stays the same for the 5 fold cv. # Do five fold cross validation for k in range(1, 16): # calculate the average accuracy of knn using five fold cross validation for current k avg_dict[k] = five_fold_cv(samples_X, samples_y, k) print("Accuracies for k from 1 to 15 using 5 fold CV:") pprint(avg_dict) best = max(avg_dict, key=avg_dict.get) print( "As can be seen k={} performs the best with an average accuracy of {}%." .format(best, avg_dict[best]))
from load_mnist import * import hw1_knn as mlBasics import numpy as np import pdb # Load data - ALL CLASSES #2.a X_train, y_train = load_mnist('training') X_test, y_test = load_mnist('testing') # 2.b label_sample_idx = {} for idx, label in enumerate(y_train): if label in label_sample_idx: label_sample_idx[label].append(idx) else: label_sample_idx[label] = [idx] sample_idx = np.empty(1000, dtype='int') i, j = 0, 100 for label in label_sample_idx: sample_idx[i:j] = np.random.choice(label_sample_idx[label], size=100) i, j = j, j + 100 x_train_sample, y_train_sample = X_train[sample_idx], y_train[sample_idx] # Reshape images x_train_sample = np.reshape(x_train_sample, (x_train_sample.shape[0], -1)) X_test = np.reshape(X_test, (X_test.shape[0], -1)) print('Compute Distances')
], } model = NNetVAT(options_dict, njobs=1) model.set_optimizer(optimizers.Adam()) model.initOptimizer() alpha_plan = [0.002] * 100 for i in range(1,100): alpha_plan[i] = alpha_plan[i-1]*0.9 batchsize_l = 100 batchsize_ul = 250 N_train_labeled = 100 N_train_unlabeled = 60000 N_test = 10000 train_l, train_ul, test_set = \ load_mnist(scale=1.0/128.0, shift=-1.0, N_train_labeled=N_train_labeled, \ N_train_unlabeled=N_train_unlabeled, N_test=N_test) for epoch in range(len(alpha_plan)): # print epoch sum_loss_l = 0 sum_loss_ul = 0 for it in range(60000/batchsize_ul): x,t = train_l.get(batchsize_l) loss_l = model.loss_labeled(x, t) model.zero_grads() loss_l.backward() model.update() x,_ = train_ul.get(batchsize_ul) loss_ul = model.loss_unlabeled(x)