<jupyter_start><jupyter_text>准备数据<jupyter_code>%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
from time import time

DATA_PATH = "../data/cifar-10-batches-py/"

def unpack(fname):
    import pickle
    with open(fname, "rb") as fin:
        dumped = pickle.load(fin, encoding="bytes")
    return dumped

train_input = []
train_oh_output = []
train_output = []
for i in range(5):
    raw_train_data = unpack(DATA_PATH + "data_batch_" + str(i+1))
    train_input.append(np.array(raw_train_data[b"data"], dtype=float) / 255)
    train_oh_output.append(np.array([[1 if x == label else 0 for x in range(10)] for label in raw_train_data[b"labels"]]))
    train_output.append(np.array(raw_train_data[b"labels"]))
train_input = np.concatenate(train_input)
train_oh_output = np.concatenate(train_oh_output)
train_output = np.concatenate(train_output)

raw_test_data = unpack(DATA_PATH + "test_batch")
test_input = np.array(raw_test_data[b"data"], dtype=float) / 255
test_oh_output = np.array([[1 if x == label else 0 for x in range(10)] for label in raw_test_data[b"labels"]])
test_output = np.array(raw_test_data[b"labels"])
print(train_input.shape)
print(train_oh_output.shape) # one-hot output train data
print(train_output.shape)
print(test_input.shape)
print(test_oh_output.shape) # ont-hot output test data
print(test_output.shape)<jupyter_output>(50000, 3072)
(50000, 10)
(50000,)
(10000, 3072)
(10000, 10)
(10000,)<jupyter_text>建立模型<jupyter_code>from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import SGD
from sklearn.model_selection import StratifiedKFold
def create_model(hidden_layer_size=200, hidden_layer_depth=1, activation="tanh", dropout=0.2):
    model = Sequential()
    model.add(Dense(hidden_layer_size, activation=activation, input_shape=(3072,)))
    for i in range(hidden_layer_depth-1):
        model.add(Dropout(dropout))
        model.add(Dense(hidden_layer_size, activation=activation))
    model.add(Dropout(dropout))
    model.add(Dense(10, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer=SGD(), metrics=["accuracy"])
    return model

# model must be fitted with one-hot output train data

def calc_accuracy(model, test_input, test_output):
    predict = np.array([np.argmax(x) for x in model.predict(test_input)])
    return np.sum(predict == test_output) / len(predict)<jupyter_output><empty_output><jupyter_text>超参交叉验证<jupyter_code>n_splits = 5
skf = StratifiedKFold(n_splits=n_splits)<jupyter_output><empty_output><jupyter_text>隐藏层单元数<jupyter_code>hidden_layer_sizes = np.linspace(10, 1000, 10, dtype=int)
accuracies = np.zeros((len(hidden_layer_sizes), n_splits))
for i, hidden_layer_size in enumerate(hidden_layer_sizes):
    print("cross validating", "hidden layer size", hidden_layer_size)
    accuracies[i] = np.zeros(n_splits)
    for j, (train_indices, test_indicies) in enumerate(skf.split(test_input, test_output)):
        model = create_model(hidden_layer_size = hidden_layer_size)
        model.fit(test_input[train_indices], test_oh_output[train_indices], verbose=0)
        accuracies[i][j] = calc_accuracy(model, test_input[test_indicies], test_output[test_indicies])
    print(accuracies[i])
accuracies_mean = [np.mean(a) for a in accuracies]
accuracies_std = [np.std(a) for a in accuracies]
plt.errorbar(hidden_layer_sizes, accuracies_mean, yerr=accuracies_std)
plt.xlabel("hidden layer size")
plt.ylabel("epoch 1 accuracy")
plt.show()<jupyter_output><empty_output><jupyter_text>隐藏层层数<jupyter_code>hidden_layer_depths = np.linspace(1, 5, 5, dtype=int)
accuracies = np.zeros((len(hidden_layer_depths), n_splits))
for i, hidden_layer_depth in enumerate(hidden_layer_depths):
    print("cross validating", "hidden layer depth", hidden_layer_depth)
    accuracies[i] = np.zeros(n_splits)
    for j, (train_indices, test_indicies) in enumerate(skf.split(test_input, test_output)):
        model = create_model(hidden_layer_depth = hidden_layer_depth)
        model.fit(test_input[train_indices], test_oh_output[train_indices], verbose=0)
        accuracies[i][j] = calc_accuracy(model, test_input[test_indicies], test_output[test_indicies])
    print(accuracies[i])
accuracies_mean = [np.mean(a) for a in accuracies]
accuracies_std = [np.std(a) for a in accuracies]
plt.errorbar(hidden_layer_depths, accuracies_mean, yerr=accuracies_std)
plt.xlabel("hidden layer depth")
plt.ylabel("epoch 1 accuracy")
plt.show()<jupyter_output><empty_output><jupyter_text>激活函数<jupyter_code>activations = ["softmax", "elu", "selu", "softplus", "softsign", "relu", "tanh", "sigmoid", "hard_sigmoid", "linear"]
accuracies = np.zeros((len(activations), n_splits))
for i, activation in enumerate(activations):
    print("cross validating", "activation function", repr(activation))
    accuracies[i] = np.zeros(n_splits)
    for j, (train_indices, test_indicies) in enumerate(skf.split(test_input, test_output)):
        model = create_model(activation = activation)
        model.fit(test_input[train_indices], test_oh_output[train_indices], verbose=0)
        accuracies[i][j] = calc_accuracy(model, test_input[test_indicies], test_output[test_indicies])
    print(accuracies[i])
accuracies_mean = [np.mean(a) for a in accuracies]
accuracies_std = [np.std(a) for a in accuracies]
plt.errorbar(activations, accuracies_mean, yerr=accuracies_std)
plt.xlabel("activation function")
plt.ylabel("epoch 1 accuracy")
plt.show()<jupyter_output><empty_output><jupyter_text>dropout率<jupyter_code>dropouts = np.linspace(0, 0.9, 10)
accuracies = np.zeros((len(dropouts), n_splits))
for i, dropout in enumerate(dropouts):
    print("cross validating", "dropout", dropout)
    accuracies[i] = np.zeros(n_splits)
    for j, (train_indices, test_indicies) in enumerate(skf.split(test_input, test_output)):
        model = create_model(dropout = dropout)
        model.fit(test_input[train_indices], test_oh_output[train_indices], verbose=0)
        accuracies[i][j] = calc_accuracy(model, test_input[test_indicies], test_output[test_indicies])
    print(accuracies[i])
accuracies_mean = [np.mean(a) for a in accuracies]
accuracies_std = [np.std(a) for a in accuracies]
plt.errorbar(dropouts, accuracies_mean, yerr=accuracies_std)
plt.xlabel("dropout")
plt.ylabel("epoch 1 accuracy")
plt.show()<jupyter_output><empty_output><jupyter_text>效果测试<jupyter_code>model = create_model(hidden_layer_size=200, hidden_layer_depth=1, activation="tanh", dropout=0.3)
model.fit(train_input, train_oh_output, epochs=50)
accuracy = calc_accuracy(model, test_input, test_output)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12757575757575756, 'fraction_numerical': 0.01606060606060606, 'mean_word_length': 6.802600472813239, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '110848', 'license': 'MIT', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2405, 'n_words': 429, 'path': 'assignment2/keras_nn.ipynb', 'repo_name': 'andyroiiid/digital_image_processing_course_project', 'size': '6600'}