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
import sys, os sys.path.append(os.pardir) import numpy as np from dataset.minist 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) print(img.shape) img = img.reshape(28, 28) print(img.shape) img_show(img)
# coding: utf-8 import os import sys sys.path.append(os.pardir) # 为了导入父目录的文件而进行的设定 import numpy as np + self.weight_decay_lambda * self.params['W' + str(idx)] grads['b' + str(idx)] = self.layers['Affine' + str(idx)].d import matplotlib.pyplot as plt from dataset.minist import load_mnist from commom.multi_layer_net_extend import MultiLayerNetExtend from commom.trainer import Trainer (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True) # 为了再现过拟合,减少学习数据 x_train = x_train[:300] t_train = t_train[:300] # 设定是否使用Dropuout,以及比例 ======================== use_dropout = True # 不使用Dropout的情况下为False dropout_ratio = 0.2 # ==================================================== network = MultiLayerNetExtend(input_size=784, hidden_size_list=[100, 100, 100, 100, 100, 100], output_size=10, use_dropout=use_dropout, dropout_ration=dropout_ratio) trainer = Trainer(network, x_train, t_train, x_test, t_test, epochs=301, mini_batch_size=100, optimizer='sgd', optimizer_param={'lr': 0.01}, verbose=True) trainer.train() train_acc_list, test_acc_list = trainer.train_acc_list, trainer.test_acc_list
# TODO: load mnist data import sys import os sys.path.append(os.pardir) from dataset.minist import load_mnist (x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False,one_hot_label=True) # print(x_train.shape) # print(t_train.shape) # print(x_test.shape) # print(t_test.shape) # TODO: show the first img import numpy as np from PIL import Image def img_show(img): pil_img = Image.fromarray(np.uint8(img)) pil_img.show() img = x_train[0] label = t_train[0] print(label) print(img.shape) img = img.reshape(28, 28) # 还原成原来的尺寸 print(img.shape)
# coding: utf-8 import sys, os sys.path.append(os.pardir) # 为了导入父目录而进行的设定 import numpy as np import matplotlib.pyplot as plt from dataset.minist import load_mnist from ch08.deep_convnet import DeepConvNet from commom.trainer import Trainer (x_train, t_train), (x_test, t_test) = load_mnist(flatten=False) network = DeepConvNet() trainer = Trainer(network, x_train, t_train, x_test, t_test, epochs=20, mini_batch_size=100, optimizer='Adam', optimizer_param={'lr': 0.001}, evaluate_sample_num_per_epoch=1000) trainer.train() # 保存参数 network.save_params("deep_convnet_params.pkl") print("Saved Network Parameters!")
import sys, os sys.path.append(os.pardir) import numpy as np from dataset.minist import load_mnist from ch05.two_layer_net import TwoLayerNet (x_train, t_train), (x_test, y_test) = load_mnist(normalize=True, one_hot_label=True) network = TwoLayerNet(input_size=784, hidden_size=50, output_size=10) if __name__ == '__main__': x_batch = x_train[:3] t_batch = t_train[:3] grad_numerical = network.numerical_gradient(x_batch, t_batch) grad_backprop = network.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))
def get_data(): (x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=True) return x_test, t_test