コード例 #1
0
# 3. Multilayer Perceptrons

import mnist
from ImageData import ImageData
from Model import Model

# Preprocess the images and labels using the ImageData class
# For this task the image will need to be in the format
# (image, example) with flattened images and one-hot-encoded
# labels (of size (n_examples, 10) as there are 10 possible labels 0-9)
# MNIST has 60,000 training examples and 10,000 validation examples
data = ImageData()
data.get_data(dataset=mnist)
data.data_preprocess(pre_shuffle=False,
                     normalize=True,
                     flatten=True,
                     pad=False,
                     img_first_format=True,
                     one_hot_encode=True)

# Define and use a multi layer perceptrons (MLP) model for learning the MNIST
# data, then plot the training and validation accuracy as a function of epoch
# and save this data to a csv file
input_size = data.train_data.shape[0]
hidden_size = 20
output_size = data.train_labels.shape[1]
save_dir = "C:/Users/Jason/Documents/"
save_name = "save_name"
graph_save_path = save_dir + save_name + "-graph.png"
data_save_path = save_dir + save_name + "-data.csv"

multi_layer_perceptrons = Model(input_size=input_size,
コード例 #2
0
zero_labels = np.array([[-1], [-1], [-1], [-1], [-1], [-1]])

# Preprocess the images and labels using the ImageData class
# For this task the image will need to be in the format
# (image, example) with flattened images
# The training set will have 8 examples (4 1s, 4 0s) and the
# validation set will have 4 examples (2 1s, 2 0s)
data = ImageData()
data.get_data(images1=ones,
              image_labels1=one_labels,
              images2=zeros,
              image_labels2=zero_labels)
data.data_preprocess(split_data=True,
                     normalize=False,
                     flatten=True,
                     img_first_format=True,
                     one_hot_encode=False)

# Define and train a single perceptron model for learning this dataset, then
# plot the training and validation accuracy as a function of epoch and save
# this data to a csv file
input_size = data.train_data.shape[0]
output_size = data.train_labels.shape[1]
save_dir = "C:/Users/Jason/Documents/"
graph_save_path = save_dir + "graph.png"
data_save_path = save_dir + "data.csv"

single_layer_perceptrons = Model(input_size,
                                 output_size,
                                 hidden_size=None,