def train(imgData, labData, resultFolder): inputSize = 28 * 28; numClasses = 10; lamb = 1e-4; checkGrad = False; images = MNIST.loadImages(imgData); labels = MNIST.loadLabels(labData); thetas = 0.005 * np.random.random(inputSize * numClasses); # 是否进行梯度检验 if checkGrad: grads = softmaxCost(thetas, numClasses, inputSize, lamb, images, labels)[1]; numgrad = computeNumericalGradient(softmaxCost, thetas,numClasses, inputSize, lamb, images, labels); for i in range(50): print grads[i], ' ', numgrad[i]; # 使用LBFGS进行优化 optimThetas = fmin_l_bfgs_b(func=softmaxCost, x0=thetas, args=(numClasses, inputSize, lamb, images, labels), maxiter=100)[0]; # 保存数据 saveResult(resultFolder + "thetas.txt", optimThetas);
def main(): imgData = "E:/TestDatas/MLStudy/UFLDL/train-images.idx3-ubyte"; labData = "E:/TestDatas/MLStudy/UFLDL/train-labels.idx1-ubyte"; inputSize = 28 * 28; numLabels = 5; hiddenSize = 200; sparsityParam = 0.1; lamb = 3e-3; beta = 3; images = MNIST.loadImages(imgData); labels = MNIST.loadLabels(labData); labelSet = []; unlabeledSet = []; for i in range(len(labels)): if(labels[i] >= 5): unlabeledSet.append(i); else: labelSet.append(i); numTrain = len(labelSet) / 2; trainSet = labelSet[:numTrain]; testSet = labelSet[numTrain:]; unlabeledData = np.mat(np.zeros((inputSize, len(unlabeledSet)))); for i in range(len(unlabeledSet)): unlabeledData[:, i] = images[:, unlabeledSet[i]];
def train(imgData, labData, resultFolder): inputSize = 28 * 28 numClasses = 10 lamb = 1e-4 checkGrad = False images = MNIST.loadImages(imgData) labels = MNIST.loadLabels(labData) thetas = 0.005 * np.random.random(inputSize * numClasses) # 是否进行梯度检验 if checkGrad: grads = softmaxCost(thetas, numClasses, inputSize, lamb, images, labels)[1] numgrad = computeNumericalGradient(softmaxCost, thetas, numClasses, inputSize, lamb, images, labels) for i in range(50): print grads[i], ' ', numgrad[i] # 使用LBFGS进行优化 optimThetas = fmin_l_bfgs_b(func=softmaxCost, x0=thetas, args=(numClasses, inputSize, lamb, images, labels), maxiter=100)[0] # 保存数据 saveResult(resultFolder + "thetas.txt", optimThetas)
def train1(): print("train1........................................") print("\t训练代码demo") with fluid.dygraph.guard(): epoch_num = 5 BATCH_SIZE = 64 train_reader = paddle.batch(paddle.dataset.mnist.train(), batch_size=32, drop_last=True) mnist = MNIST() adam = fluid.optimizer.AdamOptimizer(learning_rate=0.001, parameter_list=mnist.parameters()) for epoch in range(epoch_num): for batch_id, data in enumerate(train_reader()): dy_x_data = np.array([x[0].reshape(1, 28, 28) for x in data]).astype('float32') y_data = np.array([x[1] for x in data ]).astype('int64').reshape(-1, 1) img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) cost = mnist(img) loss = fluid.layers.cross_entropy(cost, label) avg_loss = fluid.layers.mean(loss) if batch_id % 100 == 0 and batch_id is not 0: print("epoch: {}, batch_id: {}, loss is: {}".format( epoch, batch_id, avg_loss.numpy())) avg_loss.backward() adam.minimize(avg_loss) mnist.clear_gradients()
def train_and_evaluate(): last_error_ratio = 1.0 epoch = 0 train_data_set, train_labels = MNIST.get_training_data_set(6000,True) # 加载训练样本数据集,和one-hot编码后的样本标签数据集 test_data_set, test_labels = MNIST.get_test_data_set(1000,True) # 加载测试特征数据集,和one-hot编码后的测试标签数据集 train_data_set=np.array(train_data_set) train_labels=np.array(train_labels) test_data_set=np.array(test_data_set) test_labels=np.array(test_labels) print('样本数据集的个数:%d' % len(train_data_set)) print('测试数据集的个数:%d' % len(test_data_set)) network = DNN.Network([784, 300, 10],0.3) # 定义一个输入节点784+1,神经元300,输出10,0.3为学习速率, while True: # 迭代至准确率开始下降 epoch += 1 # 记录迭代次数 network.train(train_labels, train_data_set, 1) # 使用训练集进行训练。1为迭代次数 print('%s epoch %d finished' % (datetime.datetime.now(), epoch)) # 打印时间和迭代次数 if epoch % 10 == 0: # 每训练10次,就计算一次准确率 error_ratio = DNN.evaluate(network, test_data_set, test_labels) # 计算准确率 print('%s after epoch %d, error ratio is %f' % (datetime.datetime.now(), epoch, error_ratio)) # 打印输出错误率 if error_ratio < 0.1: # 如果错误率开始上升就不再训练了。 break else: print('错误率:', last_error_ratio) last_error_ratio = error_ratio # 否则继续训练 index=0 for layer in network.layers: np.savetxt('MNIST—W'+str(index),layer.W) np.savetxt('MNIST—b' + str(index), layer.b) index+=1 print(layer.W) print(layer.b)
def train4(use_cudnn, model_file): with fluid.dygraph.guard(): epoch_num = 5 BATCH_SIZE = 64 mnist = MNIST(use_cudnn=use_cudnn) adam = fluid.optimizer.Adam(learning_rate=0.001, parameter_list=mnist.parameters()) train_reader = paddle.batch(paddle.dataset.mnist.train(), batch_size=BATCH_SIZE, drop_last=True) test_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=BATCH_SIZE, drop_last=True) np.set_printoptions(precision=3, suppress=True) dy_param_init_value = {} start_time = time.time() for epoch in range(epoch_num): for batch_id, data in enumerate(train_reader()): # step 1 : 处理输入 dy_x_data = np.array([x[0].reshape(1, 28, 28) for x in data]).astype('float32') y_data = np.array([x[1] for x in data ]).astype('int64').reshape(BATCH_SIZE, 1) img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True # step 2 : 前向传播&&损失函数 cost = mnist(img) loss = fluid.layers.cross_entropy(cost, label) avg_loss = fluid.layers.mean(loss) dy_out = avg_loss.numpy() # step 3 : 反向传播&&最优化 avg_loss.backward() adam.minimize(avg_loss) mnist.clear_gradients() # step 4 : 测试模型 if batch_id % 100 == 0 and batch_id is not 0: mnist.eval() test_cost, test_acc = test(test_reader, mnist, BATCH_SIZE) mnist.train() print( "epoch {}, batch_id {}, train loss is {}, test cost is {}, test acc is {}" .format(epoch, batch_id, avg_loss.numpy(), test_cost, test_acc)) fluid.dygraph.save_dygraph(mnist.state_dict(), model_file) end_time = time.time() print("training model has finished! time=%.2fs" % (end_time - start_time))
def main(): dataFile = "E:/TestDatas/MLStudy/UFLDL/train-images.idx3-ubyte"; resultFolder = "E:/TestDatas/MLStudy/UFLDL/result/Vectorization/"; visibleSize = 28 * 28; hiddenSize = 196; sparsityParam = 0.1; lamb = 3e-3; beta = 3; images = MNIST.loadImages(dataFile, 10000); patches = images / 255.0; # 画图元数据 plotDatas(resultFolder, patches); # 初始化数据集 thetas = initializeParameters(hiddenSize, visibleSize); # 使用LBFGS进行优化 optimThetas = fmin_l_bfgs_b(func=sparseAutoencoderCost, x0=thetas, args=(visibleSize, hiddenSize, lamb, sparsityParam, beta, patches), maxiter=500)[0]; # 保存数据 saveResult(resultFolder + "thetas.txt", optimThetas); # 可视化结果 W1 = np.reshape(optimThetas[0 : hiddenSize * visibleSize], (hiddenSize, visibleSize)); displayNetwork(W1.T, 14, 14, 28, 28, resultFolder + "results.png");
def MNIST(): import MNIST as pretrain MNIST_PATH = '../0.data/MNIST' VALIDATION_SIZE = 5000 train_images = pretrain.MNIST_download(MNIST_PATH, 'train', 'images') train_labels = pretrain.MNIST_download(MNIST_PATH, 'train', 'labels') test_images = pretrain.MNIST_download(MNIST_PATH, 'test', 'images') test_labels = pretrain.MNIST_download(MNIST_PATH, 'test', 'labels') validation_images = train_images[:VALIDATION_SIZE] validation_labels = train_labels[:VALIDATION_SIZE] train_images = train_images[VALIDATION_SIZE:] train_labels = train_labels[VALIDATION_SIZE:] #print('train-labels is ', train_labels); class DataSets(object): pass data_sets = DataSets() data_sets.test = pretrain.DataSet(test_images, test_labels, dtype=tf.float32, one_hot=True) data_sets.train = pretrain.DataSet(train_images, train_labels, dtype=tf.float32, one_hot=True) data_sets.validation = pretrain.DataSet(validation_images, validation_labels, dtype=tf.float32, one_hot=True) return data_sets
def f(indexes, is_test=False): """ input: indexes to train the model on is_test: false: returns 'training' model accuracy on the validation set true: returns 'test' model accuracy on the test set """ gc.collect() import MNIST return MNIST.f(indexes, is_test)
def main(): print("Neural Network") nn = neural_network.NeuralNetwork([3, 5, 2]) data = MNIST.MNIST("Dataset/") X = np.array([0.5, 1, 0.25]).T out = nn.predict(X) print(out)
def train3(use_cudnn, model_file): print("train3........................................") print("\t多卡训练(paddle有bug,没有调试通)") place = fluid.CUDAPlace(fluid.dygraph.parallel.Env().dev_id) with fluid.dygraph.guard(place): strategy = fluid.dygraph.parallel.prepare_context() epoch_num = 5 BATCH_SIZE = 64 mnist = MNIST(use_cudnn=use_cudnn) adam = fluid.optimizer.AdamOptimizer(learning_rate=0.001, parameter_list=mnist.parameters()) mnist = fluid.dygraph.parallel.DataParallel(mnist, strategy) train_reader = paddle.batch(paddle.dataset.mnist.train(), batch_size=BATCH_SIZE, drop_last=True) train_reader = fluid.contrib.reader.distributed_batch_reader( train_reader) start_time = time.time() for epoch in range(epoch_num): for batch_id, data in enumerate(train_reader()): dy_x_data = np.array([x[0].reshape(1, 28, 28) for x in data]).astype('float32') y_data = np.array([x[1] for x in data ]).astype('int64').reshape(-1, 1) img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True cost, acc = mnist(img, label) loss = fluid.layers.cross_entropy(cost, label) avg_loss = fluid.layers.mean(loss) avg_loss = mnist.scale_loss(avg_loss) avg_loss.backward() mnist.apply_collective_grads() adam.minimize(avg_loss) mnist.clear_gradients() if batch_id % 100 == 0 and batch_id is not 0: print("epoch: {}, batch_id: {}, loss is: {}".format( epoch, batch_id, avg_loss.numpy())) fluid.dygraph.save_dygraph(mnist.state_dict(), model_file) end_time = time.time() print("training model has finished! time=%.2fs" % (end_time - start_time))
def predict(imgData, labData, resultFolder): inputSize = 28 * 28 numClasses = 10 images = MNIST.loadImages(imgData) labels = MNIST.loadLabels(labData) thetas = np.array(loadThetas(resultFolder + "thetas.txt")) thetas = thetas.reshape(numClasses, inputSize) pred = np.argmax(thetas * images, 0) pred = np.array(pred)[0] error = 0 m = np.shape(labels)[0] for i in range(m): if pred[i] != labels[i]: error += 1 print "Accuracy: ", (m - error * 1.0) / m
def main(): parser = argparse.ArgumentParser() parser.add_argument('files', nargs='*', help="list of files to recognize") parser.add_argument('--mnist', help="count of random MNIST images", type=int) args = parser.parse_args(sys.argv[1:]) images = [np.flipud(1 - bmp.load(k, floatize=True)) for k in args.files] if args.mnist: m_images, m_labels = MNIST.get_data() random.shuffle(m_images) images.extend(m_images[:args.mnist]) drg = DigitRecognizerGui(images=images) drg.show()
def main(): #Task 1 runs the first task about EM in the report #tasks.task1() # Task 2 runs the second task about EM in the report #tasks.task2() #Task 3 runs the third task about EM in the report #tasks.task3() #Task 4 runs the first task about regularized EM in the report #To run task 4, line 109 in functions should be replaced by line 108 (different removing criterion) #tasks.task4() #Task 5 runs the second task about regularized EM in the report #Remember to change back to line 109 in functions file #tasks.task5() #The MNIST code uses PCA with the algorithm from task 5 on the MNIST-data MNIST.main()
def predict(imgData, labData, resultFolder): inputSize = 28 * 28; numClasses = 10; images = MNIST.loadImages(imgData); labels = MNIST.loadLabels(labData); thetas = np.array(loadThetas(resultFolder + "thetas.txt")); thetas = thetas.reshape(numClasses, inputSize); pred = np.argmax(thetas * images, 0); pred = np.array(pred)[0]; error = 0; m = np.shape(labels)[0]; for i in range(m): if pred[i] != labels[i]: error += 1; print "Accuracy: ", (m - error * 1.0) / m;
def checkData(): print("checkData......................................") with fluid.dygraph.guard(): mnist = MNIST() train_reader = paddle.batch(paddle.dataset.mnist.train(), batch_size=32, drop_last=True) _, data = list(enumerate(train_reader()))[0] dy_x_data = np.array([x[0].reshape(1, 28, 28) for x in data]).astype("float32") img = fluid.dygraph.to_variable(dy_x_data) #print("img = %s" % (str(img.numpy()))) print("img.shape = %s" % (str(img.numpy().shape))) print("MNIST(img).shape = %s" % (str(mnist(img).numpy().shape)))
def train_and_evaluate(): last_error_ratio = 1.0 epoch = 0 train_data_set, train_labels = MNIST.get_training_data_set(6000,True) # 加载训练样本数据集,和one-hot编码后的样本标签数据集 test_data_set, test_labels = MNIST.get_test_data_set(1000,True) # 加载测试特征数据集,和one-hot编码后的测试标签数据集 train_data_set=np.array(train_data_set) train_labels=np.array(train_labels) test_data_set=np.array(test_data_set) test_labels=np.array(test_labels) print('样本数据集的个数:%d' % len(train_data_set)) print('测试数据集的个数:%d' % len(test_data_set)) network = DNN.Network([784, 300, 10]) # 定义一个输入节点784+1,神经元300,输出10 error_ratios = [] while True: # 迭代至准确率开始下降 epoch += 1 # 记录迭代次数 network.train(train_labels, train_data_set, 0.005, 1) # 使用训练集进行训练。0.3为学习速率,1为迭代次数 print('%s epoch %d finished' % (datetime.datetime.now(), epoch)) # 打印时间和迭代次数 # if epoch == 0: # 每训练10次,就计算一次准确率 error_ratio = DNN.evaluate(network, test_data_set, test_labels) # 计算准确率 error_ratios.append(error_ratio) print('%s after epoch %d, error ratio is %f' % (datetime.datetime.now(), epoch, error_ratio)) # 打印输出错误率 if error_ratio < 0.1: # 设置终止条件,正确率大于90%时停止 break index=0 for layer in network.layers: np.savetxt('MNIST—W'+str(index),layer.W) np.savetxt('MNIST—b' + str(index), layer.b) index+=1 # 把模型参数存储起来 # print(layer.W) # print(layer.b) plt.plot(list(range(len(error_ratios))), error_ratios) plt.show()
def main_ncc(): mnist = MNIST.MNISTData('MNIST_Light/*/*.png') train_features, test_features, train_labels, test_labels = mnist.get_data() ncc = NearestCentriodClassifier() ncc.fit(train_features, train_labels, 10) #ncc.visaulizeLabel(3, (20,20)) y_pred = ncc.predict(test_features) print("Classification report SKLearn GNB:\n%s\n" % (metrics.classification_report(test_labels, y_pred))) print("Confusion matrix SKLearn GNB:\n%s" % metrics.confusion_matrix(test_labels, y_pred))
def main(): mnist = MNIST.MNISTData('MNIST_Light/*/*.png') train_features, test_features, train_labels, test_labels = mnist.get_data() mnist.visualize_random() gnb = GaussianNB() gnb.fit(train_features, train_labels) y_pred = gnb.predict(test_features) print("Classification report SKLearn GNB:\n%s\n" % (metrics.classification_report(test_labels, y_pred))) print("Confusion matrix SKLearn GNB:\n%s" % metrics.confusion_matrix(test_labels, y_pred)) mnist.visualize_wrong_class(y_pred, 8)
def testMNIST(): mnist = MNIST("/media/WindowsE/Data/MNIST", normalize=True, flatten=False) model = createMNISTNN() lossFunc = SoftmaxWithCrossEntropyLoss() optimizer = Adam() trainIterator = DataIterator([mnist.trainX, mnist.trainY], batchSize=2**9) testIterator = DataIterator([mnist.testX, mnist.testY], batchSize=2**9, shuffle=False) evaluator = ClassifierAccuracyEvaluator() # filter_show(model.modules[0].weight.get()); trainer = NetTrainer(model, lossFunc, optimizer, evaluator) trainer.train(20, trainIterator, testIterator) trainer.plot()
def main(): mnist = MNIST.MNISTData( "/Users/duy/Documents/code/lund/EDAN95_applied_ai_lund/lab_5_nb/MNIST_Light/*/*.png" ) train_features, test_features, train_labels, test_labels = mnist.get_data() mnist.visualize_random() gnb = GaussianNB() gnb.fit(train_features, train_labels) y_pred = gnb.predict(test_features) print("Classification report SKLearn GNB:\n%s\n" % (metrics.classification_report(test_labels, y_pred))) print("Confusion matrix SKLearn GNB:\n%s" % metrics.confusion_matrix(test_labels, y_pred)) mnist.visualize_wrong_class(y_pred, 8)
def train2(): print("train2........................................") print("\t获取神经网络中的参数") with fluid.dygraph.guard(): epoch_num = 5 BATCH_SIZE = 64 mnist = MNIST() adam = fluid.optimizer.AdamOptimizer(learning_rate=0.0001, parameter_list=mnist.parameters()) train_reader = paddle.batch(paddle.dataset.mnist.train(), batch_size=BATCH_SIZE, drop_last=True) np.set_printoptions(precision=3, suppress=True) for epoch in range(epoch_num): for batch_id, data in enumerate(train_reader()): dy_x_data = np.array([x[0].reshape(1, 28, 28) for x in data]).astype("float32") y_data = np.array([x[1] for x in data ]).astype("int64").reshape(BATCH_SIZE, 1) img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True cost = mnist(img) loss = fluid.layers.cross_entropy(cost, label) avg_loss = fluid.layers.mean(loss) dy_out = avg_loss.numpy() avg_loss.backward() adam.minimize(avg_loss) mnist.clear_gradients() dy_param_value = {} for param in mnist.parameters(): print("%s = %s" % (param.name, str(param.numpy()))) dy_param_value[param.name] = param.numpy() if batch_id % 20 == 0: print("loss at step {}: {}".format(batch_id, avg_loss.numpy())) break break print("Final loss : {}".format(avg_loss.numpy()))
import numpy as np import MNIST np.random.seed(1337) # for reproducibility from keras.datasets import mnist from keras.models import Model # 泛型模型 from keras.layers import Dense, Input import matplotlib.pyplot as plt X_train, Y_train = MNIST.get_training_data_set(60000, True,False) # 加载训练样本数据集,和one-hot编码后的样本标签数据集。最大60000 X_test, Y_test = MNIST.get_test_data_set(10000, True,False) # 加载测试特征数据集,和one-hot编码后的测试标签数据集,最大10000 X_train = np.array(X_train).astype(bool) # 转化为黑白图 Y_train = np.array(Y_train) X_test = np.array(X_test).astype(bool) # 转化为黑白图 Y_test = np.array(Y_test) print('样本数据集的维度:', X_train.shape,Y_train.shape) # (600, 784) (600, 10) print('测试数据集的维度:', X_test.shape,Y_test.shape) # (100, 784) (100, 10) # 压缩特征维度至2维 encoding_dim = 2 # this is our input placeholder input_img = Input(shape=(784,)) # 编码层 encoded = Dense(128, activation='relu')(input_img) encoded = Dense(64, activation='relu')(encoded) encoded = Dense(10, activation='relu')(encoded) encoder_output = Dense(encoding_dim)(encoded)
def __init__(self): ob = MNIST.Mnist() self.trainingImgs = ob.trainingImgs self.trainingLabels = ob.trainingLabels
self.pl1.backward( self.cl1.output_array, self.cl2.delta_array) # 计算第一采样层的输入误差。参数为第一采样层的 1、输入,2、输出误差 # print('第一采样层的输入误差:', self.pl1.delta_array.shape) self.cl1.backward(onepic, self.pl1.delta_array, Activators.SigmoidActivator() ) # 计算第一卷积层的输入误差。参数为第一卷积层的 1、输入,2、输出误差,3、激活函数 self.cl1.update() # 更新权重w和偏量b # print('第一卷积层的输入误差:', self.cl1.delta_array.shape) # 由于使用了逻辑回归函数,所以只能进行分类识别。识别ont-hot编码的结果 if __name__ == '__main__': # =============================加载数据集============================= train_data_set, train_labels = MNIST.get_training_data_set( 60000, False) # 加载训练样本数据集,和one-hot编码后的样本标签数据集 test_data_set, test_labels = MNIST.get_test_data_set( 10000, False) # 加载测试特征数据集,和one-hot编码后的测试标签数据集 train_data_set = np.array(train_data_set).astype(bool).astype( int) #可以将图片简化为黑白图片 train_labels = np.array(train_labels) test_data_set = np.array(test_data_set).astype(bool).astype( int) #可以将图片简化为黑白图片 test_labels = np.array(test_labels) print('样本数据集的个数:%d' % len(train_data_set)) print('测试数据集的个数:%d' % len(test_data_set)) # =============================构造网络结构============================= mynetwork = MNISTNetwork() # 打印输出每层网络
@author: nickwang """ import numpy as np import MNIST import cv2 def imshow(img): cv2.imshow("img", img) cv2.waitKey() cv2.destroyAllWindows() train_data, train_labels, test_data, test_labels = MNIST.load() img = train_data[0] img_avg = np.average(img, axis=(0, 1)) img_std = np.std(img, axis=(0, 1)) img_norm = (img - img_avg) / img_std img_cov = np.zeros((3, 3)) for data in img_norm.reshape(-1, 3): img_cov += data.reshape(3, 1) * data.reshape(1, 3) img_cov /= len(img_norm.reshape(-1, 3)) eig_values, eig_vectors = np.linalg.eig(img_cov) for _ in range(100): alphas = np.random.normal(0, 0.1, 3) img_reconstruct_norm = img_norm + np.sum( (eig_values + alphas) * eig_vectors, axis=1)
def teacher(clear=False, config_file='digit_recognizer.pkl', iterations=1000000000, save=True, ignore_interrupt=True): config = {} if not clear: try: with open(config_file, 'rb') as fd: config = pickle.load(fd) print("config file loaded") except FileNotFoundError: pass config['layers'] = config.get('layers', list(it.chain((28*28, ), HIDDEN_LAYERS, (10, )))) print("layers : {}".format(config['layers'])) network = Bpn.NeuralNetwork(config['layers']) if 'weights' in config: network.weights = config['weights'] print("weights restored") images, labels = MNIST.get_data() data_size = len(labels) def generate_training_data(size=TRAINING_DATA_SIZE): t_set = [rnd.randint(0, data_size-1) for k in range(size)] t_images = np.array([ images[k].reshape((functools.reduce(mul, images[k].shape, 1))) for k in t_set ], dtype=np.float) for i in range(size): t_images /= 255 t_labels = np.zeros((size, 10)) for i, k in enumerate(t_set): t_labels[i][labels[k]] = 1 return t_images, t_labels train_images = None train_labels = None avg_error = None tested_labels = [0]*10 def save_config(cfg): config['weights'] = network.weights with open(cfg, 'wb') as fd: pickle.dump(config, fd) print("config file saved ({})".format(cfg)) try: for i in range(iterations): if i % TRAINING_RESET == 0: train_images, train_labels = generate_training_data() for label in train_labels: for idx, k in enumerate(label): if k: tested_labels[idx] += 1 print("New training data loaded: {}".format(tested_labels)) error = network.train_epoch(train_images, train_labels, training_rate=TRAINING_RATE) if avg_error is None: avg_error = error / TRAINING_DATA_SIZE else: avg_error = (avg_error * (SAVE_FREQUENCY - 1) + error / TRAINING_DATA_SIZE) / SAVE_FREQUENCY if i % 1 == 0: print("Iteration: {:10} Error: {:10.6f} Average: {:10.10f}".format(i, error, avg_error)) if i % SAVE_FREQUENCY == 0: save_config("backup_{}.pkl".format(avg_error)) except KeyboardInterrupt as e: if not ignore_interrupt: raise e if save: save_config(config_file)
from keras import backend as K import MNIST from keras.optimizers import Adam from keras.models import load_model # 全局变量 batch_size = 128 # 批处理样本数量 nb_classes = 10 # 分类数目 epochs = 6000 # 迭代次数 img_rows, img_cols = 28, 28 # 输入图片样本的宽高 pool_size = (2, 2) # 池化层的大小 kernel_size = (3, 3) # 卷积核的大小 input_shape = (img_rows, img_cols, 1) # 输入图片的维度 X_train, Y_train = MNIST.get_training_data_set( 6000, False) # 加载训练样本数据集,和one-hot编码后的样本标签数据集。最大60000 X_test, Y_test = MNIST.get_test_data_set( 1000, False) # 加载测试特征数据集,和one-hot编码后的测试标签数据集,最大10000 X_train = np.array(X_train).astype(bool).astype(float) / 255 #数据归一化 X_train = X_train[:, :, :, np.newaxis] # 添加一个维度,代表图片通道。这样数据集共4个维度,样本个数、宽度、高度、通道数 Y_train = np.array(Y_train) X_test = np.array(X_test).astype(bool).astype(float) / 255 #数据归一化 X_test = X_test[:, :, :, np.newaxis] # 添加一个维度,代表图片通道。这样数据集共4个维度,样本个数、宽度、高度、通道数
def test_get_empty_data(self): images, labels = MNIST.get_data({}) self.assertEqual(len(images), 0) self.assertEqual(len(labels), 0)
def test_test_data(self): requested = {1, 8, 9, 0} images, labels = MNIST.get_data(requested, dataset='test') self.assertNotEqual(len(images), 0) self.assertEqual(len(images), len(labels)) self.assertEqual(set(labels), set(requested))
def test_get_some_data(self): requested = {1, 3} images, labels = MNIST.get_data(requested) self.assertNotEqual(len(images), 0) self.assertEqual(len(images), len(labels)) self.assertEqual(set(labels), set(requested))
def __init__(self, arch, ngpu, loss='BCE'): """ DCGAN object. This class is a wrapper of a generalized DCGAN as explained in the paper: UNSUPERVISED REPRESENTATION LEARNING WITH DEEP CONVOLUTIONAL GENERATIVE ADVERSARIAL NETWORKS by Alec Radford et.al. Instance of this class initializes the Generator and the Discriminator. Arguments: arch = Architecture to use: "CIFAR10" for CIFAR10 dataset "MNIST" for MNIST dataset For CIFAR10/MNIST: Need to input n_z also. For Generic: Need to input image_size, n_z, n_chan, hiddens {'arch_type': <arch_type>, 'params' : <params> as above } Example: {'arch_type': "CIFAR10", 'params' : {'n_z' : 128 } } {'arch_type': "Generic", 'params' : {'image_size' : 32, 'n_z' : 128, 'n_chan' : 3, 'hiddens' : <see below> } } image_size = Height / width of the real images n_z = Dimensionality of the latent space n_chan = Number of channels of the real images hiddens = Number of feature maps in the first layer of the generator and discriminator Format: hiddens = {'gen': n_gen_hidden, 'dis': n_dis_hidden } ngpu = Number of gpus to be allocated, if to be run on gpu loss = The loss function to be used """ super(DCGAN, self).__init__() if arch['arch_type'] == 'MNIST': import MNIST as DG self.Gen_net = DG.Generator(n_z=arch['params']['n_z'], ngpu=ngpu) self.Dis_net = DG.Discriminator(ngpu=ngpu) self.image_size = 28 self.n_chan = 1 elif arch['arch_type'] == 'CIFAR10': import CIFAR10 as DG self.Gen_net = DG.Generator(n_z=arch['params']['n_z'], ngpu=ngpu, gen_type=arch['params']['gen_type']) self.Dis_net = DG.Discriminator( ngpu=ngpu, dis_type=arch['params']['dis_type']) self.image_size = 32 self.n_chan = 3 self.ngpu = ngpu self.n_z = arch['params']['n_z'] if loss == 'BCE': self.loss = nn.BCELoss() elif loss == 'MSE': self.loss = nn.MSELoss()
# -*- coding: utf-8 -*- import os os.chdir('d:/workspace') import MNIST import numpy as np import tensorflow as tf import matplotlib.pyplot as plt train_labels, train_images = MNIST.read_data('train-labels.idx1-ubyte', 'train-images.idx3-ubyte') test_labels, test_images = MNIST.read_data('t10k-labels.idx1-ubyte', 't10k-images.idx3-ubyte') train_labels = train_labels.astype(np.int8) test_labels = test_labels.astype(np.int8) x = tf.placeholder(tf.float32, [None, 28, 28]) x_image = tf.reshape(x, [-1, 28, 28, 1]) y = tf.placeholder(tf.float32, [None, 10]) weights = { 'conv1': tf.Variable(tf.truncated_normal([5, 5, 1, 32])), 'conv2': tf.Variable(tf.truncated_normal([5, 5, 32, 64])), 'hidden1': tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024])), 'output': tf.Variable(tf.truncated_normal([1024, 10])) } biases = { 'conv1': tf.constant(0.1, shape=[32]),
self.pl1.backward( self.cl1.output_array, self.cl2.delta_array) # 计算第一采样层的输入误差。参数为第一采样层的 1、输入,2、输出误差 # print('第一采样层的输入误差:', self.pl1.delta_array.shape) self.cl1.backward(onepic, self.pl1.delta_array, Activators.SigmoidActivator() ) # 计算第一卷积层的输入误差。参数为第一卷积层的 1、输入,2、输出误差,3、激活函数 self.cl1.update() # 更新权重w和偏量b # print('第一卷积层的输入误差:', self.cl1.delta_array.shape) # 由于使用了逻辑回归函数,所以只能进行分类识别。识别ont-hot编码的结果 if __name__ == '__main__': # =============================加载数据集============================= train_data_set, train_labels = MNIST.get_training_data_set( 600, False) # 加载训练样本数据集,和one-hot编码后的样本标签数据集。样本数量越大,训练时间越久,也越准确 test_data_set, test_labels = MNIST.get_test_data_set( 100, False) # 加载测试特征数据集,和one-hot编码后的测试标签数据集。训练时间越久,也越准确 train_data_set = np.array(train_data_set).astype(bool).astype( int) # 可以将图片简化为黑白图片 train_labels = np.array(train_labels) test_data_set = np.array(test_data_set).astype(bool).astype( int) # 可以将图片简化为黑白图片 test_labels = np.array(test_labels) print('样本数据集的个数:%d' % len(train_data_set)) print('测试数据集的个数:%d' % len(test_data_set)) # =============================构造网络结构============================= mynetwork = MNISTNetwork() # 打印输出每层网络
import os import tensorflow.keras as keras import numpy as np # Local files import ICVL import MNIST from unpack_files import unpackFiles from shared_vae_class import shared_vae_class from model_objects import model_parameters # MNIST 28x28 # ICVL 60x80 dataSetInfo = MNIST.dataInfo() # if not os.path.exists(os.path.join('Data', 'Training', # '{}_Training.pkl'.format(dataSetInfo. # name))): # unpackFiles(dataSetInfo.name) (x_train, a_train, x_test, a_test) = dataSetInfo.load() if not os.path.exists(os.path.join('Output', dataSetInfo.name)): os.makedirs(os.path.join('Output', dataSetInfo.name)) print((x_train.shape, a_train.shape)) # second layer size, third layer size, encoded size, input size model_parameters = model_parameters(batchSize=256, numEpochs=1,
def evaluate_loss(self, test_data): y = sum(self.loss_fn(self.model(x), MNIST.vectorized_result(y.item())).item() for x, y in test_data) return y / len(test_data)
from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation, Flatten from keras.layers.advanced_activations import PReLU from keras.layers.convolutional import Conv2D, MaxPooling2D from keras.optimizers import SGD, Adadelta, Adagrad from keras.utils import np_utils, generic_utils from six.moves import range import os from PIL import Image import numpy as np import MNIST #加载数据。 train_data,train_label = MNIST.get_training_data_set(100) train_data = np.array(train_data) train_label = np.array(train_label) print(train_data.shape) ############### #开始建立CNN模型 ############### #生成一个model model = Sequential() #第一个卷积层,4个卷积核,每个卷积核大小5*5。1表示输入的图片的通道,灰度图为1通道。 #border_mode可以是valid或者full,具体看这里说明:http://deeplearning.net/software/theano/library/tensor/nnet/conv.html#theano.tensor.nnet.conv.conv2d