Beispiel #1
0
def main_rain():
    trX, teX, trY, teY = mnist(onehot=True)

    trX = trX.reshape(-1, 1, 28, 28)
    teX = teX.reshape(-1, 1, 28, 28)
    X = T.ftensor4()
    Y = T.fmatrix()

    w = init_weights((32, 1, 3, 3))
    w2 = init_weights((64, 32, 3, 3))
    w3 = init_weights((128, 64, 3, 3))
    w4 = init_weights((128 * 3 * 3, 625))
    w_o = init_weights((625, 10))
    params = [w, w2, w3, w4, w_o]

    noise_l1, noise_l2, noise_l3, noise_l4, noise_py_x = model(X, params, 0.2, 0.5)
    l1, l2, l3, l4, py_x = model(X, params, 0., 0.)
    y_x = T.argmax(py_x, axis=1)

    cost = T.mean(T.nnet.categorical_crossentropy(noise_py_x, Y))

    updates = RMSprop(cost, params, lr=0.001)

    train = theano.function(inputs=[X, Y], outputs=cost, updates=updates, allow_input_downcast=True)
    predict = theano.function(inputs=[X], outputs=y_x, allow_input_downcast=True)
    for i in range(100):
        for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)):
            cost = train(trX[start:end], trY[start:end])
        print np.mean(np.argmax(teY, axis=1) == predict(teX))
        if i % 10 == 0:
            name = 'media/model/conv-{0}.model'.format(str(i))
            save_model(name, params)
    name = 'media/model/conv-final.model'
    save_model(name, params)
Beispiel #2
0
def main_train():
    trX, teX, trY, teY = mnist(onehot=True)

    X = T.fmatrix()
    Y = T.fmatrix()

    w_h = init_weights((784, 625))
    w_o = init_weights((625, 10))

    params = [w_h, w_o]
    py_x = model(X, params)
    y_x = T.argmax(py_x, axis=1)

    cost = T.mean(T.nnet.categorical_crossentropy(py_x, Y))
    updates = sgd(cost, params)

    train = theano.function(inputs=[X, Y],
                            outputs=cost,
                            updates=updates,
                            allow_input_downcast=True)
    predict = theano.function(inputs=[X],
                              outputs=y_x,
                              allow_input_downcast=True)

    for i in range(100):
        for start, end in zip(range(0, len(trX), 128),
                              range(128, len(trX), 128)):
            cost = train(trX[start:end], trY[start:end])
        print np.mean(np.argmax(teY, axis=1) == predict(teX))
        if i % 10 == 0:
            name = 'media/model/net-{0}.model'.format(str(i))
            save_model(name, params)
    name = 'media/model/net-final.model'
    save_model(name, params)
def main_train():
    trX, teX, trY, teY = mnist(onehot=True)

    X = T.fmatrix()
    Y = T.fmatrix()

    w_h = init_weights((784, 625))
    w_h2 = init_weights((625, 625))
    w_o = init_weights((625, 10))
    params = [w_h, w_h2, w_o]

    noise_h, noise_h2, noise_py_x = model(X, params, 0.2, 0.5)
    h, h2, py_x = model(X, params, 0., 0.)
    y_x = T.argmax(py_x, axis=1)

    cost = T.mean(T.nnet.categorical_crossentropy(noise_py_x, Y))
    updates = RMSprop(cost, params, lr=0.001)

    train = theano.function(inputs=[X, Y], outputs=cost, updates=updates, allow_input_downcast=True)
    predict = theano.function(inputs=[X], outputs=y_x, allow_input_downcast=True)

    for i in range(100):
        for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)):
            cost = train(trX[start:end], trY[start:end])
        print np.mean(np.argmax(teY, axis=1) == predict(teX))
        if i % 10 == 0:
            name = 'media/model/modnet-{0}.model'.format(str(i))
            save_model(name, params)
    name = 'media/model/modnet-final.model'
    save_model(name, params)
Beispiel #4
0
                         action='store',
                         default=0.001,
                         type=float)
hyper_param.add_argument('-u',
                         '--hidden_units',
                         default=3136,
                         dest='u',
                         type=int)
hyper_param.add_argument('-e', '--epochs', dest='e', default=8, type=int)

args = parser.parse_args()

data_d = args.inputDirectory
architec = args.arch
lr = args.l
hidden_units = args.u
epochs = args.e
gpu = args.gpu
path = args.save_dir

train_image_datasets, test_image_data_sets, valid_image_datasets, trainloaders, testloaders, validloaders = load.arg_inputdir(
    args.inputDirectory)
pretrained_model, output_size, i = model.arch(architec)
pretrained_model, criterion, optimizer = model.classifier(
    pretrained_model, output_size, i, lr, hidden_units)
load.training_model(pretrained_model, criterion, optimizer, epochs, gpu)
load.validation_test(pretrained_model, testloaders)
complete_path = load.set_checkpoint_path(path)
load.save_model(pretrained_model, complete_path, architec)
load.model(architec)
# pretrained_model = load.load_model(pretrained_model,'checkpoint.pth')