def main(): # ----------------------------------------- # Prepare Folders and load mnist # ----------------------------------------- inputs_folder = 'inputs/' outputs_folder = 'outputs/' for f in [inputs_folder, outputs_folder]: os.makedirs(f, exist_ok=True) # Load mnist into tensors X_train, y_train, X_valid, y_valid, X_test, y_test = load_mnist( inputs_folder) # ----------------------------------------- # Initial linear model # ----------------------------------------- basic_arquitecture(X_train, y_train, X_valid, y_valid) # ----------------------------------------- # Initial linear model # ----------------------------------------- # y = A * X + B weights = torch.randn(784, 10) bias = torch.zeros(10) plot_flat_digit(X_train[0], y_train[0])
def main(): log_dir = '/tmp/mnist/nn_keras' train_data, validate_data, test_data = load_mnist('mnist.pkl.gz') input_width = 28 K = 10 # number of classes # test data test_x = np.reshape(test_data[0], (-1, input_width, input_width, 1)) test_c = test_data[1] test_t = keras.utils.to_categorical(test_c, K) # Load the trained model. print('-----------------------------') print('loading the model ...') model = keras.models.load_model('cnn_2_model') # Evaluate the model print('-----------------------------') print('evaluating on the test set') score = model.evaluate(test_x, test_t, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1])
def main(): log_dir = '/tmp/mnist/nn_keras' train_data, validate_data, test_data = load_mnist('mnist.pkl.gz') # design matrix of shape (num_examples, dim_x); dim_x = 784 x_all = train_data[0] num_examples = x_all.shape[0] dim_x = x_all.shape[1] # label matrix (N x 1) c_all = train_data[1] K = 10 # number of classes # target variable (num_examples, K) t_all = keras.utils.to_categorical(c_all) # the same for the test data test_x = test_data[0] test_c = test_data[1] test_t = keras.utils.to_categorical(test_c, K) batch_size = 600 # learning rate eta = 0.01 max_epochs = 1000 num_neurons = 30 # the network layers x = Input(shape=(784, )) h1 = Dense(num_neurons, activation=keras.activations.relu)(x) h2 = Dense(num_neurons, activation=keras.activations.relu)(h1) h3 = Dense(num_neurons, activation=keras.activations.relu)(h2) h4 = Dense(num_neurons, activation=keras.activations.relu)(h3) y = Dense(10, activation=keras.activations.softmax)(h4) # Define the model and create the computational graph. model = Model(inputs=x, outputs=y) model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.SGD(lr=eta), metrics=[keras.metrics.categorical_accuracy]) # Train the model. model.fit(x_all, t_all, batch_size=batch_size, epochs=max_epochs, validation_data=(test_x, test_t), callbacks=[ keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=10) ]) # Evaluate the model score = model.evaluate(test_x, test_t, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1])
def main(): train_data, validate_data, test_data = load_mnist('mnist.pkl.gz') # design matrix of shape (num_examples, dim_x); dim_x = 784 x_all = train_data[0] num_examples = x_all.shape[0] # label matrix (N x 1) c_all = train_data[1] # feature mapping phi(x) resulting in shape (num_examples, dim_phi); dim_phi = dim_x + 1 phi_all = features(x_all) dim_phi = phi_all.shape[1] K = 10 # number of classes # target variable (num_examples, K) t_all = one_hot_coding(c_all, K) # the same for the test data test_x = test_data[0] test_c = test_data[1] test_t = one_hot_coding(test_c, K) batch_size = 600 # learning rate eta = 0.13 max_epochs = 100 # weight matrix (dim_phi, K); initialized with 0 W = np.zeros((dim_phi, K)) # report initial accuracy print("initial, test_accuracy: ", accuracy(W, test_x, test_t)) # training loop for epoch in xrange(max_epochs): # in each new epoch randomly shuffle the training data perm = np.random.permutation(num_examples) phi_all = phi_all[perm] t_all = t_all[perm] # run through the mini batches and update gradient for each for end_index in xrange(batch_size, num_examples, batch_size): start_index = end_index - batch_size phi_batch = phi_all[start_index:end_index] t_batch = t_all[start_index:end_index] # activity or predicted probability (batch_size, K) y = activity(W, phi_batch) # gradient of the cost (dim_phi, K) dc = dcost(y, t_batch, phi_batch) # wheight update W = W - eta * dc print("epoch: {0}, test_accuracy: {1}".format( epoch, accuracy(W, test_x, test_t)))
def __init__(self, path, dataset, classes, imgs=None, labels=None): if (path): imgs, labels = load_mnist(dataset=dataset, path=path) self.imgs = imgs.reshape(list(imgs.shape)[0], -1) self.labels = labels.type(torch.LongTensor) self.imgs, self.labels = self.dataFilter(classes) self.mean = self.getMean() self.std = self.getStd()
def onClick(self, select): #reset state if select == 0: self.clearValues() self.ui.paintBoard.Clear() #training models elif select == 1: dataset = lm.load_mnist() x_train = dataset['x_train'].round(0) y_train = dataset['y_train'].round(0) x_test = dataset['x_test'].round(0) y_test = dataset['y_test'].round(0) w_list, b_list = nl.make_params([784, 100, 10]) epoches = int(self.ui.epochEdit.text()) for epoch in range(epoches): ra = np.random.randint(60000, size=60000) for i in range(60): # if i % 10 == 0: # print(r"batch from {} to batch {}".format(i*1000, (i+1)*1000)) x_batch = x_train[ra[i * 1000:(i + 1) * 1000], :] y_batch = y_train[ra[i * 1000:(i + 1) * 1000], :] w_list, b_list = nl.update(x_batch, w_list, b_list, y_batch, eta=2.0) (train_acc, train_loss) = self.showAccuracy(x_train, y_train, w_list, b_list, epoch) self.ui.progress_label.setText("Train Acc: %f, Loss: %f" % (train_acc, train_loss)) QApplication.processEvents() self.ui.progressBar.setValue(((epoch + 1) / epoches) * 100) self.ui.epoch_label.setText("Epoch: %d/%d" % (epoch + 1, epoches)) (test_acc, test_loss) = self.showAccuracy(x_test, y_test, w_list, b_list, epoch) self.ui.test_label.setText("| Test Acc: %f, Loss: %f" % (test_acc, test_loss)) # self.show # self.ui.test_label.setText("Test Acc: %f, Loss: %f"%(--, --)) # test_label #save models np.savetxt('w_list0', w_list[0]) np.savetxt('w_list1', w_list[1]) np.savetxt('b_list0', b_list[0]) np.savetxt('b_list1', b_list[1])
def train_mnist(model, save_file): (x_train, t_train), (x_test, t_test) = load_mnist() # 加载训练集、测试集 start_time = time.time() print("开始训练模型..") model.fit(x_train, t_train) # 训练模型 end_time = time.time() print("训练结束! 耗时:", end_time-start_time, "s") joblib.dump(model, save_file) # 保存模型
def train_mnist(model, save_file): (x_train, t_train), (x_test, t_test) = load_mnist() # 加载训练集、测试集 start_time = time.time() print("开始训练模型..") model.fit(x_train, t_train) # 训练模型 end_time = time.time() print("训练结束! 耗时:", end_time - start_time, "s") joblib.dump(model, save_file) # 保存模型 # 查看测试集识别精度 print(model.predict(x_test)) print(accuracy_score(t_test, model.predict(x_test)))
def train_network(self): batch_size = input("Batch size: ") epoch = input("Number of epochs to perform: ") learning_rate = input("Learning rate: ") # Loading data (training_set, valid_set, test_set) = lm.load_mnist() print "MNIST data set sucessfully loaded" # Epoch Iteration starts current_epoch = 0 while(current_epoch < epoch): # Shuffle data # Have to disassemble and reassemble training set to shuffle paired_set = [] for single_training, single_target in zip(training_set[0], training_set[1]): paired_set.append((single_training, single_target)) # training set is now disassembled into pairs np.random.shuffle(paired_set) # Now reassemble the shuffled set shuffled_training = [] shuffled_target = [] for single_pair in paired_set: shuffled_training.append(single_pair[0]) shuffled_target.append(single_pair[1]) training_set = (np.array(shuffled_training), np.array(shuffled_target)) print "Set sucessfully shuffled" # Reformatting data batches = [ self.make_batch((training_set[0][k:k+batch_size], training_set[1][k:k+batch_size])) for k in xrange(0, len(training_set[0]), batch_size)] # At this point our batch and data sets are correctly formatted. # Ready for us to propagate through the neural network # Perform stochastic gradient descent for single_batch in batches: self.stochastic_gradient_descent(single_batch, learning_rate) # Going to next epoch current_epoch = current_epoch + 1
def train(outdir, batch_size, n_epochs, lr, embedding_size, loss_weights): print("#" * 100) print("Training with Center Loss....") print("#" * 100) outdir = outdir + "/center_loss/" if not os.path.isdir(outdir): os.makedirs(outdir) x_train, y_train, y_train_onehot, x_test, y_test, y_test_onehot = load_mnist() x_input = Input(shape=(28, 28, 1)) softmax, pre_logits = cnn(x_input, embedding_size) target_input = Input((1,), name='target_input') center = Embedding(10, embedding_size)(target_input) l2_loss = Lambda(lambda x: K.sum(K.square(x[0] - x[1][:, 0]), 1, keepdims=True), name='l2_loss')( [pre_logits, center]) model = tf.keras.models.Model(inputs=[x_input, target_input], outputs=[softmax, l2_loss]) model.compile(loss=["categorical_crossentropy", lambda y_true, y_pred: y_pred], optimizer=tf.keras.optimizers.Adam(lr=lr), metrics=["accuracy"], loss_weights=loss_weights) model.fit([x_train, y_train], y=[y_train_onehot, y_train], batch_size=batch_size, epochs=n_epochs, callbacks=[TensorBoard(log_dir=outdir)], validation_split=0.2) model.save(outdir + "center_loss_model.h5") model = Model(inputs=[x_input, target_input], outputs=[softmax, l2_loss, pre_logits]) model.load_weights(outdir + "center_loss_model.h5") _, _, X_train_embed = model.predict([x_train[:512], y_train[:512]]) _, _, X_test_embed = model.predict([x_test[:512], y_test[:512]]) from TSNE_plot import tsne_plot tsne_plot(outdir, "center_loss", X_train_embed, X_test_embed, y_train, y_test)
from load_mnist import load_mnist X_train, y_train = load_mnist('training') X_test, y_test = load_mnist('testing') from scipy.io import savemat import numpy as np import os X = np.vstack((X_train, X_test)) y = np.vstack((y_train, y_test)) wd = os.path.dirname(__file__) savemat(os.path.join(wd, 'MNIST.mat'), {'X' : X, 'y' : y})
model.summary() return model class LossHistory(Callback): def on_train_begin(self, logs={}): self.losses = [] def on_batch_end(self, batch, logs={}): self.losses.append(logs.get('loss')) # show the SGD progress: def movingaverage(x, window_size): window = np.ones(int(window_size))/float(window_size) return np.convolve(x, window, 'same') # the data, shuffled and split between train and test sets X_train, y_train = load_mnist("training", asbytes=False) #X_test, y_test = load_mnist('testing', digits=[0,1]) X_test, y_test = load_mnist('testing') #, digits=[0,1]) # Get the last 2000 for validation X_train, X_val = X_train[:-2000], X_train[-2000:] y_train, y_val = y_train[:-2000], y_train[-2000:] train_size = X_train.shape[0] X_train = X_train.reshape(train_size, 784) test_size = X_test.shape[0] X_test = X_test.reshape(test_size, 784) X_val = X_val.reshape(2000, 784) print "X shape", X_train.shape idx = np.random.randint(train_size, size=9) idx.sort()
accuracies = [] losses = [] total_data = np.arange(len(X)) for indexes in np.array_split(total_data, num_batches): X_batch = cuda.to_gpu(X[indexes]) T_batch = cuda.to_gpu(T[indexes]) loss, accuracy = model.loss_and_accuracy(X_batch, T_batch, train) accuracy_cpu = cuda.to_cpu(accuracy.data) loss_cpu = cuda.to_cpu(loss.data) accuracies.append(accuracy_cpu) losses.append(loss_cpu) return np.mean(accuracies), np.mean(losses) if __name__ == '__main__': X_train, T_train, X_test, T_test = load_mnist.load_mnist() # データを0~1に変換 X_train = X_train / 255.0 X_test = X_test / 255.0 # 適切なdtypeに変換 X_train = X_train.astype(np.float32) X_test = X_test.astype(np.float32) T_train = T_train.astype(np.int32) T_test = T_test.astype(np.int32) # 訓練データを分割 X_train, X_valid, T_train, T_valid = train_test_split(X_train, T_train, test_size=0.1, random_state=10) num_train = len(X_train)
import plot_results # Things to try: # Change random seed to get different random numbers: seed (integer) # Change number of training data samples: ntrain up to 60000 # Change number of validation data samples: nvalid up to 10000 # Change learning rate for optimization: learning_rate >0 # Change number of iterations: niterations seed = 10 ntrain = 6000 nvalid = 1000 learning_rate = 0.02 niteration = 40 # (1) Set up data nclass = 10 Xtrain, Ytrain, Xvalid, Yvalid = load_mnist.load_mnist(ntrain, nvalid) # (2) Define model nfeature = Xtrain.shape[0] np.random.seed(seed) model = NeuralNetwork.NeuralNetwork(nfeature) model.add_layer(128, "relu") model.add_layer(nclass, "softmax") # (3) Compile model optimizer = Optimizer.Adam(learning_rate, 0.9, 0.999, 1e-7) model.compile("crossentropy", optimizer) model.summary() # (4) Train model history = model.fit(Xtrain, Ytrain, niteration) # (5) Predictions and plotting # plot data, loss, and animation of results Yvalid_pred = model.predict(Xvalid)
# -*- coding: utf-8 -*- """ Created on @author: fame """ from load_mnist import load_mnist import hw1_knn as mlBasics import numpy as np # Load data - two class X_train, y_train = load_mnist('training', [0, 1]) X_test, y_test = load_mnist('testing', [0, 1]) # Load data - ALL CLASSES #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)) # Test on test data #1) Compute distances: dists = mlBasics.compute_euclidean_distances(X_train, X_test) #2) Run the code below and predict labels: y_test_pred = mlBasics.predict_labels(dists, y_train) #3) Report results
x = y = np.ones((1)) * 36 image, tx, ty = batch_pad_mnist(image, out_dim=100) tx = np.expand_dims(tx, axis=0).repeat( sequence_length, axis=0) + 14 ty = np.expand_dims(ty, axis=0).repeat( sequence_length, axis=0) + 14 image = np.expand_dims(image, axis=1) image = image.repeat(sequence_length, axis=1) glimpses = [] frames = [] reset = True for t in range(sequence_length): prediction, attention = model.predict(image[:, t, :, :], reset=reset) reset = False correct = prediction.argmax() == label glimpse = attention[0].reshape((N, N)) glimpses.append(glimpse) frame = visualize_glimpse( image[0, t, :, :], attention[1], attention[2], attention[3], attention[4], correct=correct) frames.append(frame) plot_glimpse_vis(frames, glimpses) if __name__ == '__main__': images, labels = load_mnist(dataset="testing", path="mnist") images = images / 255. for i in range(3): test_image(images[23 + 3*i], labels[23 + 3*i])
model = MLPClassifier(hidden_layer_sizes=(100, 100), activation='logistic', solver='sgd', learning_rate_init=0.001, max_iter=200, verbose=True) dataset_dir = os.path.abspath(os.path.join(os.getcwd(), "..")) + "/model" save_dir = dataset_dir + '/model.pkl' if (os.path.isfile(save_dir)): # 读取模型 model = joblib.load(save_dir) # 加载模型 else: train_mnist(model, save_dir) # 训练模型 (x_train, t_train), (x_test, t_test) = load_mnist() # 加载训练集、测试集 print("mnist 测试集识别精确率: ", accuracy_score(t_test, model.predict(x_test))) # 读取labels.xlsx data = xlrd.open_workbook('../result/excel/labels.xlsx') table = data.sheets()[0] #通过索引顺序获取 #labels = table.row_values(1) #print(str(int(labels[0]))) #print(str(int(labels[1]))) #print(labels[2]) #----------------------------------------------------- workbook = xlwt.Workbook(encoding='utf-8') worksheet = workbook.add_sheet('Predict') #worksheet.write(0, 0, label = 'Row 0, Column 0 Value') worksheet.write(0, 0, "文件名")
h = F.max_pooling_2d(h, 2) h = F.relu(h) h = model.conv_3(h) h = F.relu(h) h = model.linear_1(h) h = F.relu(h) a_y = model.linear_2(h) loss = F.softmax_cross_entropy(a_y, t) accuracy = F.accuracy(a_y, t) return loss, cuda.to_cpu(accuracy.data) * 100 if __name__ == '__main__': x_train, t_train, x_test, t_test = load_mnist.load_mnist() t_train = t_train.astype(np.int32) t_test = t_test.astype(np.int32) plt.matshow(x_train[0].reshape(28, 28), cmap=plt.cm.gray) plt.show() print "x_train.shape:", x_train.shape print "t_train.shape:", t_train.shape # 60000ある訓練データセットを50000と10000の評価のデータセットに分割する x_train, x_valid, t_train, t_valid = train_test_split(x_train, t_train, test_size=0.1, random_state=100) print "x_train.shape:", x_train.shape
import time import copy import chainer import chainer.functions as F import chainer.links as L import chainer.optimizers from chainer import cuda from chainer import Variable, Chain, optimizers from chainer.cuda import cupy import MNIST_convnet as M from sklearn.metrics import pairwise_distances from MNIST_convnet import make_n_pair from MNIST_convnet import n_pair_mc_loss from MNIST_convnet import n_pair_metric_loss_average if __name__ == '__main__': X_train, T_train, X_test, T_test = load_mnist.load_mnist() T_train = T_train.astype(np.int32) T_test = T_test.astype(np.int32) # 60000ある訓練データセットを54000と6000の評価のデータセットに分割する X_train, X_valid, T_train, T_valid = train_test_split(X_train, T_train, test_size=0.1, random_state=100) num_train = len(X_train) num_valid = len(X_valid) num_test = len(X_test) classes = np.unique(T_train) # 定義されたクラスラベル num_classes = len(classes) # クラス数 dim_features = X_train.shape[-1] # xの次元
#-*- coding: utf-8 -*- import numpy as np import ctypes import sys sys.path.append('./mnist') from load_mnist import load_mnist print("read dataset...") images, labels = load_mnist("testing", path="./mnist") images = images.astype(np.float32)/255. NUM_TEST_IMAGES = 100 class Network(object): def __init__(self, net_path): self.lib = ctypes.cdll.LoadLibrary("./build/libpylib.so") self.net = self.lib.getNet(ctypes.c_char_p(net_path.encode('utf-8'))) self.out_buf = np.zeros((10), dtype = np.float32) def __del__(self): self.lib.delNet(self.net) def inference(self, input): self.lib.inference(self.net, input.ctypes.data, self.out_buf.ctypes.data) return self.out_buf print("create network...") net = Network("./mnist_model/mnist.caffemodel") net_256 = Network("./mnist_model/mnist_256.caffemodel")
import numpy as np import pandas as pd import tensorflow as tf from scipy.stats import pearsonr from load_mnist import load_mnist import influence.experiments as experiments import influence.dataset as dataset from influence.dataset import DataSet from influence.smooth_hinge import SmoothHinge from influence.binaryLogisticRegressionWithLBFGS import BinaryLogisticRegressionWithLBFGS from tensorflow.contrib.learn.python.learn.datasets import base data_sets = load_mnist('data') pos_class = 1 neg_class = 7 X_train = data_sets.train.x Y_train = data_sets.train.labels X_test = data_sets.test.x Y_test = data_sets.test.labels X_train, Y_train = dataset.filter_dataset(X_train, Y_train, pos_class, neg_class) X_test, Y_test = dataset.filter_dataset(X_test, Y_test, pos_class, neg_class) # Round dataset size off to the nearest 100, just for batching convenience num_train = int(np.floor(len(Y_train) / 100) * 100)
args_dict = vars(args) args_string = ''.join('{}_{}_'.format(key, val) for key, val in sorted(args_dict.items()) if key not in ['experiment_dir'])[:-1] # Create datasets and experiments folders is needed. dataset_dir = mkdirs("./datasets") mkdirs(args.experiment_dir) if args.dataset == 'mnist': dataset = pjoin(dataset_dir, args.dataset + ".pkl") print("Datasets dir: {}".format(os.path.abspath(dataset_dir))) print("Experiment dir: {}".format(os.path.abspath(args.experiment_dir))) if "mnist" in dataset: # We follow the approach used in [2] to split the MNIST dataset. datasets = load_mnist(dataset, target_as_one_hot=False, flatten=True, split=(45000, 5000, 10000), drop_percentage=args.label_drop_percentage) inputs,labels = datasets unlabeleld_data = inputs[2] labeled_data = inputs[0] y_labeled = labels[0] y_unlabeled = labels[2] train_x = pd.DataFrame(labeled_data) test_x = pd.DataFrame(unlabeleld_data) train_y = pd.DataFrame(y_labeled, columns = ['labels']) test_y = pd.DataFrame(y_unlabeled, columns = ['labels']) features = train_x.columns target = 'labels'
#code is from 'Python Maching Learning' by Raschka and Mirialili ################################################################################# # import warnings import numpy as np with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=FutureWarning) import tensorflow as tf from tflinreg import TfLinreg from load_mnist import load_mnist X_train, y_train = load_mnist('./mnist/', kind='train') print('Rows: %d, Columns: %d' % (X_train.shape[0], X_train.shape[1])) X_test, y_test = load_mnist('./mnist', kind='t10k') print('Rows: %d, Columns: %d' % (X_test.shape[0], X_test.shape[1])) # mean centering and normalization: mean_vals = np.mean(X_train, axis=0) std_val = np.std(X_train) X_train_centered = (X_train - mean_vals) / std_val X_test_centered = (X_test - mean_vals) / std_val del X_train, X_test n_features = X_train_centered.shape[1] n_classes = 10 random_seed = 123 np.random.seed(random_seed) g = tf.Graph() with g.as_default(): tf.set_random_seed(random_seed)
def main(): train_data, validate_data, test_data = load_mnist('mnist.pkl.gz') # design matrix of shape (num_examples, dim_x); dim_x = 784 x_all = train_data[0] num_examples = x_all.shape[0] # label matrix (N x 1) c_all = train_data[1] # feature mapping phi(x) resulting in shape (num_examples, dim_phi); dim_phi = dim_x + 1 phi_all = features(x_all) dim_phi = phi_all.shape[1] K = 10 # number of classes # target variable (num_examples, K) t_all = one_hot_coding(c_all, K) # the same for the test data test_x = test_data[0] test_phi = features(test_x) test_c = test_data[1] test_t = one_hot_coding(test_c, K) batch_size = 600 # learning rate eta = 0.13 max_epochs = 100 # nodes for input/output of the tensorflow computation graph phi = tf.placeholder(tf.float32, [None, dim_phi]) t = tf.placeholder(tf.float32, [None, K]) # the weights are now a variable node which the graph can update; initialized with 0 W = tf.Variable(tf.zeros([dim_phi, K])) # define the computation nodes y = activity(W, phi) cost = cost_fun(y, t) # add subgraph to compute gradient descent update step for all variables for # cost objective. train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cost) accuracy = accuracy_fun(y, t) # open tf session and initialize variables (none in this example) sess = tf.InteractiveSession() tf.global_variables_initializer().run() # training loop for epoch in xrange(max_epochs): # in each new epoch randomly shuffle the training data perm = np.random.permutation(num_examples) phi_all = phi_all[perm] t_all = t_all[perm] # run through the mini batches and update gradient for each for end_index in xrange(batch_size, num_examples, batch_size): start_index = end_index - batch_size phi_batch = phi_all[start_index:end_index] t_batch = t_all[start_index:end_index] # run the graph to compute one step of the gradient descent sess.run([train_step], feed_dict={phi: phi_batch, t: t_batch}) # run the graph to compute the accuracy for the test set naccuracy = sess.run(accuracy, feed_dict={phi: test_phi, t: test_t}) print("epoch: {0}, test_accuracy: {1}".format(epoch, naccuracy))
@author: matsumi """ import load_mnist import numpy as np from sklearn.cross_validation import train_test_split from sklearn.neighbors import KNeighborsClassifier from sklearn import metrics from matplotlib.colors import ListedColormap from pandas import Series from sklearn.metrics import f1_score import matplotlib.pyplot as plt if __name__ == '__main__': x_train, t_train, x_test, t_test = load_mnist.load_mnist() t_train = t_train.astype(np.int32) t_test = t_test.astype(np.int32) plt.matshow(x_train[0].reshape(28, 28), cmap=plt.cm.gray) plt.show() # print ("x_train.shape:", x_train.shape) # print ("t_train.shape:", t_train.shape) # 60000ある訓練データセットを54000と6000の評価のデータセットに分割する x_train, x_valid, t_train, t_valid = train_test_split( x_train, t_train, test_size=0.1, random_state=100) print ("x_train.shape:", x_train.shape) print ("t_train.shape:", t_train.shape) print ("x_valid.shape:", x_valid.shape)
y_: batch_y, keep_prob: dropout }) if step % 10 == 0: # Calculate batch loss and accuracy loss, acc = sess.run([cost, accuracy], feed_dict={ x: batch_x, y_: batch_y, keep_prob: 1. }) print "Iter " + str(step*batch_size) + ", Minibatch Loss= " + \ "{:.6f}".format(loss) + ", Training Accuracy= " + \ "{:.5f}".format(acc) step += 1 print "Optimization Finished!" # Calculate accuracy for 256 mnist test images print "Testing Accuracy:", \ sess.run(accuracy, feed_dict={x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.}) if __name__ == '__main__': images, labels = load_mnist(dataset='training') images = np.reshape(images, images.shape + (1, )) print images.shape, labels.shape # should print (60000, 28, 28, 1) (60000, 1) labels = one_hot_vector(labels, 10) dropout = 0.75 lenet_network(images, labels, dropout, mode='train', n_classes=10)
from sklearn import preprocessing from sklearn.naive_bayes import GaussianNB from sklearn.model_selection import learning_curve from sklearn.model_selection import ShuffleSplit import numpy as np from pylab import * from numpy import * from load_mnist import load_mnist from plotting import plot_learning_curve trainLimit = 20000#20000 testLimit = 5000#5000 images_train, labels_train = load_mnist('training', path='data/') images_test, labels_test = load_mnist('testing', path='data/') labels_train = pd.DataFrame(data=labels_train[:trainLimit]) images_train = pd.DataFrame(data=images_train[:trainLimit]) labels_test = pd.DataFrame(data=labels_test[:testLimit]) images_test = pd.DataFrame(data=images_test[:testLimit]) X_train = images_train y_train = labels_train.values.ravel() X_test = images_test y_test = labels_test.values.ravel() #mlp = MLPClassifier(hidden_layer_sizes = (300,300,), activation='relu' , random_state=0, early_stopping=False) # mlp.fit(X_train, y_train) #Trains the classifier # print(mlp.score(X_test, y_test)) #Gives accuracy score on test set
def main(): print('Loading Model...') model = load_model(model_name) print('Loading Data...') images, labels = load_mnist(dataset="testing", path="mnist") images = images / 255. total_images = images.shape[0] print('Begining Tests...') # Initialize statistics placeholders correct_stats = np.zeros((total_images, sequence_length)) * -1 IOU_stats = np.zeros((total_images, sequence_length)) * -1 dist_stats = np.zeros((total_images, sequence_length)) * -1 for i in range(0, total_images, batch_size): if i % 250 == 0: print('On example %d' % i) batch_images = images[i:i + batch_size] batch_labels = labels[i:i + batch_size] # generate test sequences if sequence_type == 'movie': movie_gen = movie_mnist(batch_images) plc = np.zeros((sequence_length, batch_size, 100, 100)) tx = np.zeros((sequence_length, batch_size)) ty = np.zeros((sequence_length, batch_size)) for j in range(sequence_length): plc[j], tx[j], ty[j] = movie_gen.next() batch_images = np.swapaxes(plc, 0, 1) tx += 14 ty += 14 elif sequence_type == 'still': x = y = np.ones((batch_size)) * 36 batch_images, tx, ty = batch_pad_mnist(batch_images, out_dim=100) tx = np.expand_dims(tx, axis=0).repeat( sequence_length, axis=0) + 14 ty = np.expand_dims(ty, axis=0).repeat( sequence_length, axis=0) + 14 batch_images = np.expand_dims(batch_images, axis=1) batch_images = batch_images.repeat(sequence_length, axis=1) # Now we loop for all time-steps reset = True # reset is true for the first timestep for t in range(sequence_length): # OK now make a prediction prediction, attention_params = model.predict(batch_images[:, t, :, :], reset=reset) reset = False prediction = np.argmax(prediction, axis=1) correct_stats[i:i + batch_size, t] = prediction == batch_labels px = attention_params[1] py = attention_params[2] pdel = attention_params[3] * 6 # half the number of gaussians distance = get_dist(tx[t], ty[t], px, py) dist_stats[i:i + batch_size, t] = distance IOU_stats[i:i + batch_size, t] = get_IOU(tx[t], ty[t], 14, px, py, pdel) mean_dist = dist_stats.mean(axis=0) mean_correct = correct_stats.mean(axis=0) mean_IOU = IOU_stats.mean(axis=0) print(str(mean_dist)) print(str(mean_correct)) print(str(mean_IOU))
if old_loss is not None: improvement = old_loss / loss old_loss = loss if patience <= epoch and improvement < improvement_threshold: print('Breaking due to low improvement') done_looping = True break print('Optimization complete.') return train_error if __name__ == '__main__': train_images, train_labels = load_mnist(dataset="training", path="mnist") # convert labels to one-hot encoding targets = np.zeros((len(train_labels), 10), dtype=np.uint8) for i, label in enumerate(train_labels): targets[i, label] = 1 model = build_model() train_error = train_model(model, train_images, targets) pickle.dump(model, open(file_name, "wb")) pdb.set_trace()
import time import numpy as np from sklearn.preprocessing import StandardScaler # # Standardization # Determine the mean and standard deviation for each feature, then subtract the mean from each feature,divide the values by corresponding standard deviation. # \begin{equation} # {x}' = \frac{x-\bar{x}}{\delta } # \end{equation} # In[ ]: train_data, train_label = load_mnist("","train") test_data,test_label = load_mnist("","t10k") sc = StandardScaler() X_main_std = sc.fit(train_data) train_data = X_main_std.transform(train_data) test_data = X_main_std.transform(test_data) # # Cross Validation # To choose appropriate max_depth, we divide the training set into 10 sections, train 10 times on it and each time use 9 sections as training data, 1 section as validation set, compare the results. # In[ ]:
plt.axis('off') for k in range(self.k): ax[i, k + 1].imshow( self.train_imgs[similar_indices[k, i]].data.numpy().reshape( 28, 28).astype('uint8')) plt.show() if __name__ == '__main__': # get training set with 1000 samples : 100 per class dataset_path = "dataset" train_imgs, train_labels = load_mnist(dataset="training", path=dataset_path) # GET 100 PER CLASS # load test label and check for accuracy test_imgs, test_labels = load_mnist(dataset="testing", path=dataset_path) ##### TASK a.1 ##### knn_classifier = KNNCLassifier(1, train_imgs[0:1000, :, :], train_labels[0:1000]) klist = range(1, 6) accuracy_list = knn_classifier.get_accuracy(klist, test_imgs, test_labels) ##### TASK a.2 ##### plot_test = test_imgs[:10, :, :] plot_test_labels = test_labels[:10]
import numpy as np from load_mnist import load_mnist from matplotlib import pyplot as plt x_train, y_train, x_test, y_test = load_mnist() M = 10 n_train = x_train.shape[0] # number of training examples - 60000 p = x_train.shape[1] # number of input pixels - 784 (flattened 28x28 image) n_test = x_test.shape[0] def softmax_gd(xtrain, ytrain, xtest, ytest, n_train, n_test, lr, maxit): w_mj = np.random.normal(scale=0.01, size=(M, p)) # weight matrix b_m = np.zeros(shape=(1, M)) # offset vector z_im = np.zeros(shape=(n_train, M)) # model in (n x M) dJdbm = np.zeros(shape=(1, M)) dJdwmj = np.zeros(shape=(M, p)) J = np.zeros(shape=(maxit, 1)) # cost vector acc_train = np.zeros(shape=(maxit, 1)) # classifcation accuracy J_test = np.zeros(shape=(maxit, 1)) # cost vector acc_test = np.zeros(shape=(maxit, 1)) # classifcation accuracy it = 0 while it != maxit:
from keras.layers.core import Dense from keras.optimizers import SGD from keras.utils import np_utils from load_mnist import load_mnist import numpy as np # This program creaes an artificial neural network with # several hidden layers, each consisting of a relatively # large amount of neurons. Though this can be run on a # standard CPU, it will likely take ages to complete. It # is recommended to set your Theano library flags for # theano.device='gpu' in order to have the code execute # on a CUDA enabled Nvidia GPU. # Load the training and testing datasets X_train, y_train = load_mnist('') X_test, y_test = load_mnist('', kind='t10k') # Transform the label set from integer values to a one-vs- # all vector representation y_train_ohe = np_utils.to_categorical(y_train) # np.random.seed(1) # Create a sequential neural network, IE all values only # move forward to subsequent layers, no feedback model = Sequential() # Add the first layer, 784 (28x28 pixel images) inputs, # 2500 outputs model.add(Dense(input_dim=X_train.shape[1],
# coding = utf-8 import numpy as np from load_mnist import load_mnist from model.initial_LeNet import initial_LeNet train_data, train_label, valid_data, valid_label = load_mnist() cnn = initial_LeNet() lr = [0.01, 0.001] cnn.train(train_data, train_label, lr, epoch=2, batch_size=100) res = cnn.predict(valid_data) res = res.reshape(valid_label.shape) print 'Accuracy is: %f' % (np.sum(res == valid_label) / float(np.max(valid_label.shape)))
x = mu + sigma * m num_bins = 50 # the histogram of the data plt.figure('PDF' + str(cl)) n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5) # add a 'best fit' line y = mlab.normpdf(bins, mu, sigma) plt.plot(bins, y, 'r--') #plt.xlabel('Smarts') #plt.ylabel('Probability') # Tweak spacing to prevent clipping of ylabel plt.subplots_adjust(left=0.15) # the data, shuffled and split between train and test sets # For training, get the digits from 0 to 8 X_train, y_train = load_mnist("training", asbytes=False, digits=[0,1,2,3,4,5,6,7,8]) #X_test, y_test = load_mnist('testing', digits=[0,1]) # For testing, get the digit 9 X_test9, y_test9 = load_mnist('testing', digits=[9]) # For testing, get all digits X_test, y_test = load_mnist('testing') # Get the last 2000 for validation X_train, X_val = X_train[:-2000], X_train[-2000:] y_train, y_val = y_train[:-2000], y_train[-2000:] train_size = X_train.shape[0] X_train = X_train.reshape(train_size, 784) test_size = X_test.shape[0] X_test = X_test.reshape(test_size, 784) X_val = X_val.reshape(2000, 784)
import numpy as np from load_mnist import load_mnist from train import gradient_descent path = '/opt/data/pzq/MNIST' X_train, y_train = load_mnist(path) m, n = X_train.shape k = np.unique(y_train).size # Initialize parameters Theta = np.random.randn(n + 1, k) # n+1 X k # Normalization X_train = X_train / 255 # train alpha = np.ones(k).astype(np.float) * 3 # k cost, Theta = gradient_descent(y_train, X_train, Theta, alpha) with open("theta", "w") as theta: Theta.tofile(theta)
if classname.find('Conv') != -1: n = m.kernel_size[0] * m.kernel_size[1] * m.in_channels m.weight.data.normal_(0, math.sqrt(2. / n)) if m.bias is not None: m.bias.data.zero_() elif classname.find('BatchNorm') != -1: m.weight.data.fill_(1) m.bias.data.zero_() elif classname.find('Linear') != -1: n = m.weight.size(1) m.weight.data.normal_(0, 0.01) m.bias.data = torch.ones(m.bias.data.size()) if __name__ == '__main__': data = load_mnist() # plt.imshow(data[0][0]) # plt.show() device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print(device) img_p = img_processing() train_data_set = my_dataset(data, transform=img_p) test_data_set = my_dataset(data, ctype='test', transform=img_p) net = my_net() net.apply(init_weight) net = net.to(device) optimizer = optim.SGD(net.parameters(), lr=1e-5, momentum=0.9)
:return: features """ hidden_layer = self.autoencoders.get_layer(name='encoder_%d' % (self.n_stacks - 1)) feature_model = Model(self.autoencoders.input, hidden_layer.output) return feature_model.predict(x, batch_size=self.batch_size) if __name__ == "__main__": """ An example for how to use SAE model on MNIST dataset. In terminal run python3 SAE.py to see the result. """ import numpy as np from load_mnist import load_mnist x,y=load_mnist(sample_size=10000,seed=0) db = 'mnist' n_clusters = 10 # define and train SAE model sae = SAE(dims=[x.shape[-1], 64,32]) sae.fit(x=x, epochs=400) sae.autoencoders.save_weights('weights_%s.h5' % db) # extract features print ('Finished training, extracting features using the trained SAE model') features = sae.extract_feature(x) print ('performing k-means clustering on the extracted features') from sklearn.cluster import KMeans km = KMeans(n_clusters, n_init=20) y_pred = km.fit_predict(features)
N = 10 batch_size_tr = 100 batch_size_te = 50 epochs = 250 n_n = 25 number_filters = 3 number_neurons = 50 alpha = 1.01 number_neurons = 50 all_costs, all_scores, mi_list = [], [], [] all_scores = [] optimizers = [th.optim.SGD, th.optim.Adagrad, th.optim.Adam] x_tr, y_tr, x_te, y_te = load_mnist(path, 'full') for n in range(N): temp_cost, temp_score, temp_mi = [], [], [] for optimizer in optimizers: cost, score, mi_sample = [], [], [] if gpu: model = CNN5L(number_filters, number_neurons, nn.ReLU()).cuda() else: model = CNN5L(number_filters, number_neurons, nn.ReLU()) for epoch in range(epochs): cost.append(
# -*- coding: utf-8 -*- """ Created on Thu Sep 12 16:05:04 2019 @author: Dell """ from my_net import my_network from load_mnist import load_mnist import numpy as np X_train, Y_train, X_test, Y_test = load_mnist() X_subtrain = X_train[:1000] Y_subtrain = Y_train[:1000] #test lamda and epsilon(粗略估计范围版本) #max_count=50 #for i in range(max_count): # lamda=10**np.random.uniform(-7,5) # epsilon=10**np.random.uniform(-3,-6) # # net=my_network(lamda=lamda,epsilon=epsilon) # for j in range(20): # loss,y_pred=net.forward(X_subtrain,Y_subtrain) # acc=(y_pred==Y_subtrain).sum()/Y_subtrain.shape[0] # dW1,db1,dW2,db2,dW3,db3,dW4,db4=net.backward() # net.update([dW1,db1,dW2,db2,dW3,db3,dW4,db4],mode='sgd') # # #testing # loss,y_pred=net.forward(X_test[:1000],Y_test[:1000]) # test_acc=(y_pred==Y_test[:1000]).sum()/Y_subtrain.shape[0]
import numpy as np import neuralnet as nl import load_mnist as lm np.random.seed(21) dataset = lm.load_mnist() x_train = dataset['x_train'] y_train = dataset['y_train'] x_test = dataset['x_test'] y_test = dataset['y_test'] w_list, b_list = nl.make_params([784, 100, 10]) for epoch in range(1): ra = np.random.randint(60000, size=60000) for i in range(60): x_batch = x_train[ra[i * 1000:(i + 1) * 1000], :] y_batch = y_train[ra[i * 1000:(i + 1) * 1000], :] w_list, b_list = nl.update(x_batch, w_list, b_list, y_batch, eta=2.0) #驗證成效 val_dict = nl.calculate(x_test, w_list, b_list) print(val_dict['y_2'][0:10].round(2))