text_plain_output_1.png
from hyperopt import hp, fmin, tpe, space_eval, STATUS_OK, Trials

from keras.layers import Dense, Activation, Dropout, Conv2D, MaxPooling2D, Flatten, BatchNormalization

from keras.models import Sequential, model_from_json

from sklearn.preprocessing import MinMaxScaler

import keras

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import keras
from keras.models import Sequential, model_from_json
from keras.layers import Dense, Activation, Dropout, Conv2D, MaxPooling2D, Flatten, BatchNormalization
from sklearn.preprocessing import MinMaxScaler
from hyperopt import hp, fmin, tpe, space_eval, STATUS_OK, Trials
from keras import backend as K
K.image_data_format()

def plot_history(history):
    pass
def get_data():
    Xtrain = np.load('../input/k49-train-imgs.npz')['arr_0']
    ytrain = np.load('../input/k49-train-labels.npz')['arr_0']
    Xtest = np.load('../input/k49-test-imgs.npz')['arr_0']
    ytest = np.load('../input/k49-test-labels.npz')['arr_0']
    train_one_hot_labels = keras.utils.to_categorical(ytrain, num_classes=49)
    test_one_hot_labels = keras.utils.to_categorical(ytest, num_classes=49)
    n_train = ytrain.shape[0]
    n_test = ytest.shape[0]
    npix = Xtrain.shape[1]
    Xtrain1 = Xtrain.reshape(n_train, -1)
    Xtest1 = Xtest.reshape(n_test, -1)
    scaler = MinMaxScaler()
    Xtrain1 = scaler.fit_transform(Xtrain1).astype('float32')
    Xtest1 = scaler.fit_transform(Xtest1).astype('float32')
    if K.image_data_format() == 'channels_last':
        Xtrain2d = Xtrain.reshape(n_train, npix, npix, 1).astype('float32') / 255
        Xtest2d = Xtest.reshape(n_test, npix, npix, 1).astype('float32') / 255
        input_shape = (npix, npix, 1)
    return (Xtrain2d, train_one_hot_labels, Xtest2d, test_one_hot_labels, input_shape)

def objective(args):
    kernelsize = args['ksize']
    poolsize = args['psize']
    stridesize = args['ssize']
    model3 = Sequential()
    model3.add(Conv2D(32, kernel_size=kernelsize, strides=stridesize, activation='relu', input_shape=input_shape))
    model3.add(Conv2D(64, kernel_size=kernelsize, strides=stridesize, activation='relu'))
    model3.add(MaxPooling2D(pool_size=poolsize))
    model3.add(BatchNormalization())
    model3.add(Dropout(0.25))
    model3.add(Flatten())
    model3.add(BatchNormalization())
    model3.add(Dense(128, activation='relu'))
    model3.add(BatchNormalization())
    model3.add(Dense(49, activation='softmax'))
    model3.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy'])
    history3 = model3.fit(Xtrain, ytrain, epochs=25, batch_size=256, validation_data=(Xtest, ytest), verbose=0)
    return_object = {}
    return_object['status'] = STATUS_OK
    return_object['loss'] = history3.history['loss'][0]
    return_object['deets'] = {'loss': history3.history['loss'][0], 'val_loss': history3.history['val_loss'][0], 'acc': history3.history['acc'][0], 'val_acc': history3.history['val_acc'][0], 'args': args, 'model': model3}
    return return_object

space = hp.choice('a', [{'ksize': hp.choice('ksize_val', [2, 3, 4]), 'psize': hp.choice('psize_val', [2, 3, 4]), 'ssize': hp.choice('stridesize_val', [1, 2])}])
trials = Trials()
best = fmin(objective, space, algo=tpe.suggest, max_evals=10, trials=trials)