def get_data(): ((train_image, train_label), (test_image, test_label)) = load_mnist_dataset.load_mnist_dataset(one_hot=False, normarize=True, flatten=True) return test_image, test_label
# official import sys import os import numpy as np from PIL import Image # made by me import load_mnist_dataset sys.path.append(os.pardir) def show_img(img): pil_img = Image.fromarray(img) pil_img.show() ((x_train, t_train), (x_test, t_test)) = load_mnist_dataset.load_mnist_dataset(one_hot=False, normarize=False, flatten=True) img = x_train[0] label = t_train[0] print(label) print(img.shape) img = img.reshape((28, 28)) print(img.shape) show_img(img)
import numpy as np from load_mnist_dataset import load_mnist_dataset from twolayernet import TwoLayersNet import matplotlib.pyplot as plt (train_images, train_label), (test_images, test_label) =\ load_mnist_dataset(flatten=False) train_loss_list = [] iters_num = 10000 train_size = train_images.shape[0] batch_size = 100 learning_rate = 0.1 network = TwoLayersNet(input_size=784, hidden_size=100, output_size=10) for i in range(iters_num): batch_mask = np.random.choice(train_size, batch_size) image_batch = train_images[batch_mask] label_batch = train_label[batch_mask] grad = network.numerical_gradient(image_batch, label_batch) for key in ("w1", "b1", "w2", "b2"): network.parameters[key] -= grad[key] * learning_rate loss = network.loss(image_batch, label_batch) train_loss_list.append(loss)
def random_batch_choice(x, y, batch_size): data_size = x.shape[0] batch_filter = np.random.choice(data_size, batch_size) x_batch = x[batch_filter] y_batch = y[batch_filter] return x_batch, y_batch if __name__ == "__main__": ((train_images, train_labels), (test_images, test_labels)) = load_mnist_dataset.load_mnist_dataset() print("Non-batch") print(train_images.shape) print(train_labels.shape) print(test_images.shape) print(test_labels.shape) (x_batch, y_batch) = random_batch_choice(train_labels, train_labels, batch_size=100) print("batch") print(x_batch.shape) print(y_batch.shape)
#def load_mnist(): # # # 学習データ # x_train = np.load('/root/userspace/public/lesson1/data/x_train.npy') # y_train = np.load('/root/userspace/public/lesson1/data/y_train.npy') # # テストデータ # x_test = np.load('/root/userspace/public/lesson1/data/x_test.npy') # x_train = x_train.reshape(-1, 784).astype('float32') / 255 # x_test = x_test.reshape(-1, 784).astype('float32') / 255 # y_train = np.eye(10)[y_train.astype('int32').flatten()] # return (x_train, x_test, y_train) (x_train, y_train), (x_test, y_test) = load_mnist_dataset() model = Sequential() model.add( Dense(512, input_shape=(784, ), activation='relu', kernel_initializer='he_normal')) model.add(Dropout(0.2)) model.add( Dense(256, activation='relu', kernel_initializer='he_normal', kernel_regularizer=None)) model.add(Dropout(0.5))