예제 #1
0
def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')

x_train, y_train, x_valid, y_valid, x_test, y_test = mnist("./")

lr = id2config[incumbent]['config']["learning_rate"]
num_filters = int(id2config[incumbent]['config']["num_filters"])
batch_size = int(id2config[incumbent]['config']["batch_size"])
filter_size = int(id2config[incumbent]['config']["filter_size"])
epochs=6

print(lr)
print(num_filters)
print(batch_size)
print(filter_size)


x_image = tf.placeholder(tf.float32, shape=[None,28,28,1], name='x')
y_ = tf.placeholder(tf.float32, shape=[None, 10], name='y_')
예제 #2
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.x_train, self.y_train, self.x_valid, self.y_valid, self.x_test, self.y_test = mnist("./")
예제 #3
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.x_train, self.y_train, self.x_valid, self.y_valid, self.x_test, self.y_test = mnist(
            "../exercise1/data")
예제 #4
0
# Plots the performance of the best found validation error over time
all_runs = res.get_all_runs()
print('All runs:')
print(all_runs)
# Let's plot the observed losses grouped by budget,
import hpbandster.visualization as hpvis

hpvis.losses_over_time(all_runs)

import matplotlib.pyplot as plt
plt.savefig("random_search.png")

# TODO: retrain the best configuration (called incumbent) and compute the test error
# get the best hyperparameters
learning_rate = id2config[incumbent]['config']['learning_rate']
batch_size = id2config[incumbent]['config']['batch_size']
filter_size = id2config[incumbent]['config']['filter_size']
num_filters = id2config[incumbent]['config']['num_filters']

# load data
x_train, y_train, x_valid, y_valid, x_test, y_test = mnist('../exercise1/data')
# train the best model agian
learning_curve, model, _ = train_and_validate(x_train, y_train, x_valid,
                                              y_valid, 12, learning_rate,
                                              num_filters, batch_size,
                                              filter_size)

test_err = test(x_test, y_test, model)
print(test_err)
예제 #5
0
def plot_learning_curves(lcurves, curve_labels, title):
    fig, ax = plt.subplots()
    ax.set(title=title, xlabel='Epoch', ylabel='Accuracy')
    plots = ax.plot(lcurves)
    ax.legend(plots, curve_labels, loc=4)
    fig.savefig(title + '.pdf', format='pdf')
    #plt.show()


if __name__ == "__main__":
    # hyperparameters
    lr = 1e-3
    num_filters = 32
    batch_size = 128
    epochs = 12

    # train and test datasets
    x_train, y_train, x_valid, y_valid, x_test, y_test = cnn.mnist()

    # trying different learning rates
    learning_rates = [0.1, 0.01, 0.001, 0.0001]
    kernel_sizes = [3]
    lcs = try_hyperparameters(learning_rates, kernel_sizes)
    plot_learning_curves(lcs, learning_rates, 'Learning rates')

    # trying different filter sizes
    learning_rates = [0.001]
    kernel_sizes = [1, 3, 5, 7]
    lcs = try_hyperparameters(learning_rates, kernel_sizes)
    plot_learning_curves(lcs, kernel_sizes, 'kernel sizes')
예제 #6
0
    plt.title(legend['title'], size=22)
    plt.xlabel('Epoch', size=18)
    plt.ylabel('Validation error', size=18)
    # plt.xlim(0,len(learning_curves_lst[0]))
    for i in range(len(learning_curves_lst)):
        plt.plot(learning_curves_lst[i], label=legend['graph_labels'][i])

    plt.legend(fontsize=16)

    plt.savefig('%s.pdf' % legend['title'])


if __name__ == "__main__":

    # get data
    train_x, train_y, val_x, val_y, test_x, test_y = mnist('../exercise1/data')

    # 2: test the effect of the learning rate
    learning_rates = [0.1, 0.01, 0.001, 0.0001]

    learning_curves_lr = []
    for lr in learning_rates:
        learning_curve, model, _ = train_and_validate(train_x,
                                                      train_y,
                                                      val_x,
                                                      val_y,
                                                      num_epochs=NUM_EPOCHS,
                                                      lr=lr,
                                                      num_filters=16,
                                                      kernel_size=3,
                                                      batch_size=64)
예제 #7
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # <JAB>
        self.x_train, self.y_train, self.x_valid, self.y_valid, self.x_test, self.y_test = mnist(
            "./")

        #(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
        #self.x_train = train_images[:50000]
        #self.y_train = train_labels[:50000]
        #self.x_valid = train_images[50000:]
        #self.y_valid = train_labels[50000:]
        #self.x_test = test_images
        #self.y_test = test_labels
        #
        #
        print('Training Data Shapes (x, y): (', self.x_train.shape, ', ',
              self.y_train.shape, ')')
        print('Validation Data Shapes (x, y): (', self.x_valid.shape, ', ',
              self.y_valid.shape, ')')
        print('Test Data Shapes (x, y): (', self.x_test.shape, ', ',
              self.y_test.shape, ')')