def train(args):

    X_train = args["X_train"]
    X_train = X_train.reshape((X_train.shape[0], 1, 28, 28))
    X_train /= 255
    y_train = np.asarray(args["y_train"].flatten(), dtype="int32")

    num_epochs = 1 if "num_epochs" not in args else args["num_epochs"]
    net = get_net(num_epochs)
    net.fit(X_train, y_train)
    if "vis" in args and args["vis"] == True:
        helper.plot_conv_activity(net.layers_[1], X_train[0:1])
    return net.get_all_params_values()
def train(args):

    X_train = args["X_train"]
    X_train = X_train.reshape( (X_train.shape[0], 1, 28, 28) )
    X_train /= 255
    y_train = np.asarray( args["y_train"].flatten(), dtype="int32" )

    num_epochs = 1 if "num_epochs" not in args else args["num_epochs"]
    net = get_net(num_epochs)
    net.fit(X_train, y_train)
    if "vis" in args and args["vis"] == True:
        helper.plot_conv_activity( net.layers_[1], X_train[0:1] )
    return net.get_all_params_values()
def train(arg):
    global args, symbols, best_weights, SEED
    args = arg

    filenames = [ (args["dir"] + os.path.sep + args["attr_values"]["filename"][int(x)]) \
        for x in args["X_train"].flatten().tolist() ]

    symbols = prepare()

    alpha = args["alpha"]
    momentum = 0.9

    if alpha != -1:
        if "rmsprop" not in args:
            updates = lasagne.updates.momentum(symbols.loss, symbols.all_params, alpha, momentum)
        else:
            updates = lasagne.updates.rmsprop(symbols.loss, symbols.all_params, alpha)
    else:
        updates = lasagne.updates.adagrad(symbols.loss, symbols.all_params, 1.0)

    iter_train = theano.function(
        [symbols.X],
        [symbols.loss, symbols.loss_flat],
        updates=updates
    )

    if "batch_size" in args:
        bs = args["batch_size"]
    else:
        bs = 128

    best_valid_accuracy = -1
    for e in range(0, args["epochs"]):

        np.random.seed(SEED)
        np.random.shuffle(filenames)

        SEED += 1
        np.random.seed(SEED)

        sys.stderr.write("Epoch #%i:\n" % e)
        batch_train_losses = []
        batch_train_accuracies = []
        batch_train_alt_losses = []
        for b in range(0, len(filenames)):
            if b*bs >= len(filenames):
                break
            X_train_batch = get_batch(filenames, b*bs, (b+1)*bs)
            #print (X_train_batch.shape)

            #sys.stderr.write("  Batch #%i (%i-%i)\n" % ((b+1), (b*bs), ((b+1)*bs) ))
            loss, loss_flat = iter_train( X_train_batch )
            batch_train_losses.append(loss)
            print (loss, loss_flat, sum(loss_flat > 0))
        helper.plot_conv_activity( symbols.conv_layer, X_train_batch[1:2] )

        sys.stderr.write( "  train_loss = %f\n" % \
            (np.mean(batch_train_losses)) )

    current_weights = lasagne.layers.get_all_param_values(symbols.output_layer)

    return (print_network(symbols.output_layer), current_weights)
예제 #4
0
def train(arg):
    global args, symbols, best_weights, SEED
    args = arg

    filenames = [ (args["dir"] + os.path.sep + args["attr_values"]["filename"][int(x)]) \
        for x in args["X_train"].flatten().tolist() ]

    symbols = prepare()

    alpha = args["alpha"]
    momentum = 0.9

    if alpha != -1:
        if "rmsprop" not in args:
            updates = lasagne.updates.momentum(symbols.loss,
                                               symbols.all_params, alpha,
                                               momentum)
        else:
            updates = lasagne.updates.rmsprop(symbols.loss, symbols.all_params,
                                              alpha)
    else:
        updates = lasagne.updates.adagrad(symbols.loss, symbols.all_params,
                                          1.0)

    iter_train = theano.function([symbols.X],
                                 [symbols.loss, symbols.loss_flat],
                                 updates=updates)

    if "batch_size" in args:
        bs = args["batch_size"]
    else:
        bs = 128

    best_valid_accuracy = -1
    for e in range(0, args["epochs"]):

        np.random.seed(SEED)
        np.random.shuffle(filenames)

        SEED += 1
        np.random.seed(SEED)

        sys.stderr.write("Epoch #%i:\n" % e)
        batch_train_losses = []
        batch_train_accuracies = []
        batch_train_alt_losses = []
        for b in range(0, len(filenames)):
            if b * bs >= len(filenames):
                break
            X_train_batch = get_batch(filenames, b * bs, (b + 1) * bs)
            #print (X_train_batch.shape)

            #sys.stderr.write("  Batch #%i (%i-%i)\n" % ((b+1), (b*bs), ((b+1)*bs) ))
            loss, loss_flat = iter_train(X_train_batch)
            batch_train_losses.append(loss)
            print(loss, loss_flat, sum(loss_flat > 0))
        helper.plot_conv_activity(symbols.conv_layer, X_train_batch[1:2])

        sys.stderr.write( "  train_loss = %f\n" % \
            (np.mean(batch_train_losses)) )

    current_weights = lasagne.layers.get_all_param_values(symbols.output_layer)

    return (print_network(symbols.output_layer), current_weights)