def show_mnist(): 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) print(img.shape) img = img.reshape(28, 28) print(img.shape) img_show(img)
# coding: utf-8 import sys, os sys.path.append(os.pardir) # 为了导入父目录的文件而进行的设定 import numpy as np from dplearning.dataset.mnist import load_mnist from dplearning.common.multi_layer_net_extend import MultiLayerNetExtend # 读入数据 (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, one_hot_label=True) network = MultiLayerNetExtend(input_size=784, hidden_size_list=[100, 100], output_size=10, use_batchnorm=True) x_batch = x_train[:1] t_batch = t_train[:1] grad_backprop = network.gradient(x_batch, t_batch) grad_numerical = network.numerical_gradient(x_batch, t_batch) for key in grad_numerical.keys(): diff = np.average( np.abs(grad_backprop[key] - grad_numerical[key]) ) print(key + ":" + str(diff))
# coding: utf-8 import sys, os sys.path.append(os.pardir) # 为了导入父目录而进行的设定 import numpy as np import matplotlib.pyplot as plt from dplearning.ch08.deep_convnet import DeepConvNet from dplearning.dataset.mnist import load_mnist (x_train, t_train), (x_test, t_test) = load_mnist(flatten=False) network = DeepConvNet() network.load_params("deep_convnet_params.pkl") print("calculating test accuracy ... ") #sampled = 1000 #x_test = x_test[:sampled] #t_test = t_test[:sampled] classified_ids = [] acc = 0.0 batch_size = 100 for i in range(int(x_test.shape[0] / batch_size)): tx = x_test[i * batch_size:(i + 1) * batch_size] tt = t_test[i * batch_size:(i + 1) * batch_size] y = network.predict(tx, train_flg=False) y = np.argmax(y, axis=1) classified_ids.append(y) acc += np.sum(y == tt)
# coding: utf-8 import sys, os sys.path.append(os.pardir) # 为了导入父目录的文件而进行的设定 import numpy as np import matplotlib.pyplot as plt from dplearning.dataset.mnist import load_mnist from dplearning.common.multi_layer_net_extend import MultiLayerNetExtend from dplearning.common.optimizer import SGD, Adam (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True) # 减少学习数据 x_train = x_train[:1000] t_train = t_train[:1000] max_epochs = 20 train_size = x_train.shape[0] batch_size = 100 learning_rate = 0.01 def __train(weight_init_std): bn_network = MultiLayerNetExtend(input_size=784, hidden_size_list=[100, 100, 100, 100, 100], output_size=10, weight_init_std=weight_init_std, use_batchnorm=True) network = MultiLayerNetExtend(input_size=784, hidden_size_list=[100, 100, 100, 100, 100], output_size=10, weight_init_std=weight_init_std) optimizer = SGD(lr=learning_rate) train_acc_list = [] bn_train_acc_list = []
# coding: utf-8 import sys, os sys.path.append(os.pardir) # 为了导入父目录的文件而进行的设定 import numpy as np from dplearning.dataset.mnist import load_mnist 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)
def get_data(): (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, flatten=True, one_hot_label=True) return x_test, t_test
def get_data(): (train_img, train_label), (test_img, test_label) = load_mnist() return test_img, test_label