def split_data_experiment():

    n_train1, n_train2 = 27500, 27500
    n_valid1, n_valid2 = 2500, 2500
    num_pruning_iter = 27  # How many iterative pruning steps to perform

    print(
        "Running a data split experiment with input_size:{}, hidden_size:{}, num_classes:{}, "
        "batch_size:{}, num_epochs:{}, num_pruning_iter:{}, pruning_rates:{}".
        format(network.input_size, network.hidden_size, network.num_classes,
               network.batch_size, network.num_epochs, num_pruning_iter,
               network.pruning_rates))

    train_loader1, val_loader1, train_loader2, val_loader2 = \
        dataset.init_data_mask_split_data_expt(n_train1, n_valid1, n_train2, n_valid2)

    model = network.MultiLayerPerceptron().to(network.device)
    model.apply(weights_init)

    presets = {}
    for name, param in model.state_dict().items():
        presets[name] = param.clone()
    model.presets = presets
    network.train(model, train_loader1, val_loader1)

    test_accuracy1 = network.test(model)
    test_accuracy_history1 = [test_accuracy1]
    test_accuracy_history2 = [test_accuracy1]

    for iter in range(num_pruning_iter):
        # This is the percentage of weights remaining in the network after pruning
        print(
            "Results for pruning round {} with percentage of weights remaining {}"
            .format(iter + 1, 100 * 0.8**(iter + 1)))
        # prune model after training with first half of data
        network.prune(model)
        # Reset, retrain on second half of data - perform testing
        network.reset_params(model)
        network.train(model, train_loader2, val_loader1)
        test_accuracy2 = network.test(model)
        test_accuracy_history2.append(test_accuracy2)
        # reset the model, retrain with first half of data - then perform testing
        network.reset_params(model)
        network.train(model, train_loader1, val_loader1)
        test_accuracy1 = network.test(model)
        test_accuracy_history1.append(test_accuracy1)

    print(
        'Test accuracy history after re-training with first half of the training dataset {}'
        .format(test_accuracy_history1))
    print(
        'Test accuracy history after re-training with second half of the training dataset {}'
        .format(test_accuracy_history2))
    visualize.plot_test_accuracy_coarse(test_accuracy_history1,
                                        test_accuracy_history2)
Beispiel #2
0
def ocr(img, img_height, img_width):
    words = []

    # 1st: segment images containing word(s) from image
    characters = s.segment_image_v2(img, img_width, img_height)
    # 2nd: get output from character cnn
    cnn_words = network.test(characters);
    return cnn_words
def base_experiment():
    n_train, n_valid = 55000, 5000
    num_pruning_iter = 15  # How many iterative pruning steps to perform

    print(
        "Running a base experiment with input_size:{}, hidden_size:{}, num_classes:{}, "
        "batch_size:{}, num_epochs:{}, num_pruning_iter:{}, pruning_rates:{}".
        format(network.input_size, network.hidden_size, network.num_classes,
               network.batch_size, network.num_epochs, num_pruning_iter,
               network.pruning_rates))

    train_loader, val_loader = dataset.init_data_mask_base_expt(
        n_train, n_valid)

    model = network.MultiLayerPerceptron().to(network.device)
    model.apply(weights_init)

    presets = {}
    for name, param in model.state_dict().items():
        presets[name] = param.clone()
    model.presets = presets

    early_stop_iteration = network.train(model, train_loader, val_loader)

    test_accuracy_history = []
    test_accuracy = network.test(model)
    test_accuracy_history.append(test_accuracy)
    early_stop_iteration_history = [early_stop_iteration]

    for iter in range(num_pruning_iter):
        # This is the percentage of weights remaining in the network after pruning
        print(
            "\tResults for pruning round {} with percentage of weights remaining {}"
            .format(iter + 1, 100 * 0.8**(iter + 1)))
        network.prune(model)
        network.reset_params(model)
        early_stop_iteration = network.train(model, train_loader, val_loader)
        early_stop_iteration_history.append(early_stop_iteration)
        test_accuracy = network.test(model)
        test_accuracy_history.append(test_accuracy)

    print('Test accuracy history {}'.format(test_accuracy_history))
    print(
        'Early stop iteration history {}'.format(early_stop_iteration_history))
Beispiel #4
0
def ocr(img, img_height, img_width):
    words = []

    # 1st: segment images containing word(s) from image
    # 2nd: segment images containing single characters from word(s)
    lines = s.segment_image_v2(img, img_width, img_height)
    # print("Segmenting Label")
    for line in lines:
        line_letters = s.segment_line_v2(line, img_width, img_height)
        if line_letters is None:
            continue
        words.append(line_letters)

    # get output from character cnn
    cnn_words = network.test(words)
    return cnn_words
    net = n.NeuralNetwork([
        l.InputLayer(height=28, width=28),
        conv,
        l.MaxPoolingLayer(pool_size=3),
        fcl
    ], f.categorical_crossentropy)

    conv.w = weights["w"][0][0]
    conv.b = np.expand_dims(weights["w"][0][1], 1)
    fcl.w = np.swapaxes(weights["w"][1][0], 0, 1)
    fcl.b = np.expand_dims(weights["w"][1][1], 1)

    return net

################################################################################

parser = argparse.ArgumentParser()
parser.add_argument("data", help="the path to the MNIST data set in .npz format (generated using utils.py)")
parser.add_argument("func", help="the function name of the test to be run")
args = parser.parse_args()

trn_set, tst_set = u.load_mnist_npz(args.data)

weights = np.load("weights.npz")
net = locals()[args.func](weights)

print("Testing network...")
accuracy = n.test(net, tst_set)
print("Test accuracy:", accuracy)
Beispiel #6
0
test_input[:, :2] /= np.sum(arm_length)
training_input[:, 2:] /= max_delta
test_input[:, 2:] /= max_delta

# Baseline MF input to PN: required to map network response to target range
pn_baselines = np.random.normal(1.2, 0.1, number_pn)
net = create_network(pn_baselines)

# Train with algorithm by Bouvier et al. 2018
print("Train network...")
training_errors, error_estimates, training_responses = train(
    net, training_input, training_targets, pn_baselines, arm_length)

# Test network performance
print("Test network performance...")
test_errors, test_responses, pn_activity = test(net, test_input, test_targets,
                                                pn_baselines, arm_length)

filename = time.strftime("%Y%m%d%H%M")
filename = filename + "_results.npz"
filename = os.path.join(sys.argv[2], filename)

np.savez_compressed(filename,
                    training_errors=training_errors,
                    error_estimates=error_estimates,
                    training_angles=training_set,
                    training_targets=training_targets,
                    responses=training_responses,
                    pn_activity=pn_activity,
                    test_angles=test_set,
                    test_targets=test_targets,
                    test_responses=test_responses,
def ticket_transfer_experiment():
    n_train_letter, n_valid_letter = 55000, 5000
    n_train_digit, n_valid_digit = 55000, 8880
    num_pruning_iter = 1  # How many iterative pruning steps to perform

    print(
        "Running a digit training experiment with digit_size:{}, input_size:{}, hidden_size:{}, num_classes:{}, "
        "batch_size:{}, num_epochs:{}, num_pruning_iter:{}, pruning_rates:{}".
        format(digit_size, network.input_size, network.hidden_size,
               network.num_classes, network.batch_size, network.num_epochs,
               num_pruning_iter, network.pruning_rates))

    train_loader_letter, val_loader_letter = dataset.init_data_mask_base_expt(
        n_train_letter, n_valid_letter)

    train_loader_digit, val_loader_digit = dataset.init_data_mask_base_expt(
        n_train_digit, n_valid_digit)

    model = network.MultiLayerPerceptron().to(network.device)
    model.apply(weights_init)

    presets = {}
    for name, param in model.state_dict().items():
        presets[name] = param.clone()
    model.presets = presets

    network.train(model, train_loader_digit, val_loader_digit)

    # test_accuracy_digit = network.test(model)
    test_accuracy_history_letter = []
    test_accuracy_history_digit = []

    for iter in range(num_pruning_iter):
        # This is the percentage of weights remaining in the network after pruning
        print(
            "\tResults for pruning round {} with percentage of weights remaining {}"
            .format(iter + 1, 100 * 0.8**(iter + 1)))
        # prune model after training with digit
        network.prune(model)
        # Reset, retrain the winning ticket on letter dataset - perform testing
        network.reset_params(model)
        print(
            "\tRetraining the winning ticket on whole dataset - to evaluate trainability and performance "
            "of sparse network ")
        network.train(model, train_loader_letter, val_loader_letter)
        test_accuracy_letter = network.test(model)
        test_accuracy_history_letter.append(test_accuracy_letter)
        # reset the model, retrain with digit of data - to identify further winning tickets - then perform testing
        print(
            "\tRetraining the winning ticket on digit of training dataset - to identify sparse network in next "
            "pruning cycle ")
        network.reset_params(model)
        network.train(model, train_loader_digit, val_loader_digit)
        test_accuracy_digit = network.test(model)
        test_accuracy_history_digit.append(test_accuracy_digit)

    print(
        'Test accuracy history of winning ticket on digit of training data {}'.
        format(test_accuracy_history_digit))
    print(
        'Test accuracy history of winning ticket after re-training with letter training dataset {}'
        .format(test_accuracy_history_letter))
Beispiel #8
0
	elif(command=="savemodel"):
		network.save_model()
	elif(command=="resetmodel"):
		network.reset_model()
	elif(command=="dull"):
		network.set_smart(0)
	elif(command=="smart"):
		network.set_smart(1)
	elif(command=="trainall"):
		for i in range(get_int(0,1)):			
			network.train(True,True)
	elif(command=="lr"):
		network.set_learning_rate(get_float(0,network.learning_rate))
	else:
		try:
			c=json.loads(line)				
			action=c["action"]
			if action=="playmove":
				fens=c["fens"]
				network.play_move(fens)
			elif action=="epoch":
				network.epoch(c["n"])
			elif action=="test":			
				network.test()
			else:
				pf("unknown command")
		except Exception:
			pf("command error")

################################################