def cpu_single(P, num_of_hidden, pop_size=1):
    # number of input covariates
    num_of_inputs = len(P[0])

    # Tanh to keep implementation details easy in opencl
    net = build_feedforward(num_of_inputs, num_of_hidden, 1, output_function="tanh")

    @benchmark
    def many_sim():
        for n in xrange(pop_size):
            net.sim(P)

    many_sim()
Beispiel #2
0
def find_solution(P, T):
                
    #test, validation = get_validation_set(P, T, validation_size = 0.33)
    net = build_feedforward(input_number = len(P[0]), hidden_number = 4, output_number = len(T[0]))
    #com = build_feedforward_committee(size = 4, input_number = len(P[0]), hidden_number = 6, output_number = len(T[0]))
    
    epochs = 1000
    
    testset, valset = get_validation_set(P, T, validation_size = 0.01)
    
    print("Training...")
    net = benchmark(train_evolutionary)(net, testset, valset, 100, random_range = 1)
    net = benchmark(traingd)(net, testset, valset, epochs, learning_rate = 0.1, block_size = 1)
    
    #benchmark(train_committee)(com, train_evolutionary, P, T, 100, random_range = 1)
    #benchmark(train_committee)(com, traingd, P, T, epochs, learning_rate = 0.1, block_size = 30)
    
    #P, T = test
    Y = net.sim(P)
    area, best_cut = plotroc(Y, T, 1)
    plot2d2c(net, P, T, figure = 2, cut = best_cut)
    
    #P, T = validation
    #Y = com.sim(P)
    #plotroc(Y, T, 2)
    
#    print("")
#    print("Stats for cut = 0.5")
#    [num_correct_first, num_correct_second, total_performance, num_first, num_second, missed] = stat(Y, T)
    
    #save_network(best, "/export/home/jonask/Projects/aNeuralN/ANNs/classification_gdblock20_rocarea" + str(area) + ".ann")
    #save_network(best, "/export/home/jonask/Projects/aNeuralN/ANNs/classification_genetic_rocarea" + str(area) + ".ann")
    #save_committee(com, "/export/home/jonask/Projects/aNeuralN/ANNs/classification_gdblock30_rocarea" + str(area) + ".anncom")
    #save_committee(com, "/export/home/jonask/Projects/aNeuralN/ANNs/classification_genetic_rocarea" + str(area) + ".anncom")
    
    plt.show()
Beispiel #3
0
Created on Jun 7, 2011

@author: jonask
'''
from kalderstam.neural.error_functions.sum_squares import total_error
from kalderstam.neural.network import build_feedforward
from kalderstam.util.filehandling import parse_data
from kalderstam.neural.training.gradientdescent import traingd
from kalderstam.neural.training.davis_genetic import train_evolutionary
import numpy

xor_set = [[0, 0, 0],
           [0, 1, 1],
           [1, 0, 1],
           [1, 1, 0]]

xor_set = numpy.array(xor_set)

P, T = parse_data(xor_set, targetcols = 2, inputcols = [0, 1], normalize = False)

net = build_feedforward(2, 4, 1)

print("Error before training: " + str(total_error(T, net.sim(P))))
net = traingd(net, (P, T), (None, None), epochs = 1000, learning_rate = 0.1, block_size = 0)
print("Error after training: " + str(total_error(T, net.sim(P))))

net = build_feedforward(2, 4, 1)
print("Error before genetic training: " + str(total_error(T, net.sim(P))))
net = train_evolutionary(net, (P, T), (None, None), epochs = 100, population_size = 100)
print("Error after genetic training: " + str(total_error(T, net.sim(P))))
        print('Aaawww....')
    outputs = net.sim(P)

    plot_network_weights(net)

    plt.figure()
    plt.title('Scatter plot sum square error\n' + filename)
    plt.xlabel('Survival time years')
    plt.ylabel('Network output')
    try:
        plt.scatter(T.flatten(), outputs.flatten(), c = 'g', marker = 's')
        plt.plot(T.flatten(), T.flatten(), 'r-')
    except:
        pass

if __name__ == "__main__":
    logging.basicConfig(level = logging.INFO)
    glogger.setLoggingLevel(glogger.info)

    p = 4 #number of input covariates
    #net = load_network('/home/gibson/jonask/Projects/aNeuralN/ANNs/PERCEPTRON.ann')
    net = build_feedforward(p, 10, 1, output_function = "linear")
    lineartarget_nn = '/home/gibson/jonask/Dropbox/Ann-Survival-Phd/fake_data_set/lineartarget_no_noise.txt'
    nonlineartarget_nn = '/home/gibson/jonask/Dropbox/Ann-Survival-Phd/fake_data_set/nonlineartarget_no_noise.txt'
    lineartarget_wn = '/home/gibson/jonask/Dropbox/Ann-Survival-Phd/fake_data_set/lineartarget_with_noise.txt'
    nonlineartarget_wn = '/home/gibson/jonask/Dropbox/Ann-Survival-Phd/fake_data_set/nonlineartarget_with_noise.txt'

    while True:
        experiment(net, nonlineartarget_wn, 500)
        plt.show()
Beispiel #5
0
    filename = "/home/gibson/jonask/Dropbox/Ann-Survival-Phd/Two_thirds_of_SA_1889_dataset.txt"
    P, T = parse_file(filename, targetcols = [4, 5], inputcols = [2, -4, -3, -2, -1], ignorerows = [0], normalize = True)
    #P, T = parse_file(filename, targetcols = [4, 5], inputcols = [2, -3], ignorerows = [0], normalize = True)

    #Remove tail censored
    P, T = copy_without_tailcensored(P, T)

    #Limit to incourage overtraining!
    #rows = sample(range(len(T)), 100)
    #P = P[rows]
    #T = T[rows]

    p = len(P[0]) #number of input covariates

    #net = load_network('/home/gibson/jonask/Projects/aNeuralN/ANNs/4x10x10x1.ann')
    net = build_feedforward(p, 30, 1, output_function = 'linear')

    #Initial state
    outputs = net.sim(P)
    orderscatter(outputs, T, filename, 's')

    for var in xrange(len(P[0, :])):
        plot_input(P[:, var])

    glogger.show()

    epochs = 20000
    rate = 1
    block_size = 0

    for times in range(100):
Beispiel #6
0
 kwargs['one'] = 1
 queue.put((car(), args, kwargs))
 w.join()
 
 #Now try with a neural network
 
 P1, T1 = loadsyn1(100000)
 P2, T2 = loadsyn1(100000)
 P3, T3 = loadsyn1(100000)
 P4, T4 = loadsyn1(100000)
 P5, T5 = loadsyn1(100000)
 P6, T6 = loadsyn1(100000)
 P7, T7 = loadsyn1(100000)
 P8, T8 = loadsyn1(100000)
             
 net = build_feedforward(2, 1, 1)
 
 nw1 = Neural_Worker(queue)
 nw1.start()
 nw2 = Neural_Worker(queue)
 nw2.start()
 
 queue.put((net, [P1], {}))
 queue.put((net, [P2], {}))
 
 nw1.join()
 nw2.join()
 
 print "Testing pool!"
 
 p = Pool()
def train_single():
    try:
        netsize = input('Number of hidden nodes? [3]: ')
    except SyntaxError as e:
        netsize = 3

    try:
        pop_size = input('Population size? [50]: ')
    except SyntaxError as e:
        pop_size = 50

    try:
        mutation_rate = input('Please input a mutation rate (0.25): ')
    except SyntaxError as e:
        mutation_rate = 0.25

    SB22 = "/home/gibson/jonask/Dropbox/Ann-Survival-Phd/Two_thirds_of_SA_1889_dataset_SB22.txt"
    Benmargskohorten = "/home/gibson/jonask/Dropbox/Ann-Survival-Phd/Two_thirds_of_SA_1889_dataset_Benmargskohorten.txt"
    SB91b = "/home/gibson/jonask/Dropbox/Ann-Survival-Phd/Two_thirds_of_SA_1889_dataset_SB91b.txt"
    all_studies = "/home/gibson/jonask/Dropbox/Ann-Survival-Phd/Two_thirds_of_SA_1889_dataset.txt"

    #Real data
    print("Studies to choose from:")
    print("1: SB22")
    print("2: Benmargskohorten")
    print("3: SB91b")
    print("0: All combined (default)")

    try:
        study = input("Which study to train on? [0]: ")
    except SyntaxError as e:
        study = 0

    if study == 1:
        filename = SB22
    elif study == 2:
        filename = Benmargskohorten
    elif study == 3:
        filename = SB91b
    else:
        filename = all_studies

    try:
        columns = input("Which columns to include? (Do NOT forget trailing comma if only one column is used, e.g. '3,'\nAvailable columns are: 2, -4, -3, -2, -1. Just press ENTER for all columns.\n")
    except SyntaxError:
        columns = (2, -4, -3, -2, -1)
    #P, T = parse_file(filename, targetcols = [4, 5], inputcols = [2, -4, -3, -2, -1], ignorerows = [0], normalize = True)
    P, T = parse_file(filename, targetcols = [4, 5], inputcols = columns, ignorerows = [0], normalize = True)

    #Used for output comparison
    studies = {}
    studies[SB22] = parse_file(SB22, targetcols = [4, 5], inputcols = columns, ignorerows = [0], normalize = True)
    studies[Benmargskohorten] = parse_file(Benmargskohorten, targetcols = [4, 5], inputcols = columns, ignorerows = [0], normalize = True)
    studies[SB91b] = parse_file(SB91b, targetcols = [4, 5], inputcols = columns, ignorerows = [0], normalize = True)
    studies[all_studies] = parse_file(all_studies, targetcols = [4, 5], inputcols = columns, ignorerows = [0], normalize = True)

    #remove tail censored
    #P, T = copy_without_tailcensored(P, T)

    #Divide into validation sets
    #((tP, tT), (vP, vT)) = get_validation_set(P, T, validation_size = 0.25, binary_column = 1)
    TandV = get_cross_validation_sets(P, T, 2 , binary_column = 1)

    #Network part

    p = len(P[0]) #number of input covariates

    net = build_feedforward(p, netsize, 1, output_function = 'linear')
    #net = build_feedforward_multilayered(p, [7, 10], 1, output_function = 'linear')

    try:
        epochs = input("Number of generations (200): ")
    except SyntaxError as e:
        epochs = 200

    for times, ((tP, tT), (vP, vT)) in zip(xrange(2), TandV):
        #train
        net = test(net, tP, tT, vP, vT, filename, epochs, population_size = pop_size, mutation_rate = mutation_rate)

        raw_input("Press enter to show plots...")
        glogger.show()
Beispiel #8
0
def train_single():
    try:
        netsize = input('Number of hidden nodes? [1]: ')
    except SyntaxError as e:
        netsize = 1

    try:
        pop_size = input('Population size? [100]: ')
    except SyntaxError as e:
        pop_size = 100

    try:
        mutation_rate = input('Please input a mutation rate (0.05): ')
    except SyntaxError as e:
        mutation_rate = 0.05

    SB22 = "/home/gibson/jonask/Dropbox/Ann-Survival-Phd/Two_thirds_of_SA_1889_dataset_SB22.txt"
    Benmargskohorten = "/home/gibson/jonask/Dropbox/Ann-Survival-Phd/Two_thirds_of_SA_1889_dataset_Benmargskohorten.txt"
    SB91b = "/home/gibson/jonask/Dropbox/Ann-Survival-Phd/Two_thirds_of_SA_1889_dataset_SB91b.txt"
    all_studies = "/home/gibson/jonask/Dropbox/Ann-Survival-Phd/Two_thirds_of_SA_1889_dataset.txt"

    #Real data
    print("Studies to choose from:")
    print("1: SB22")
    print("2: Benmargskohorten")
    print("3: SB91b")
    print("0: All combined (default)")

    try:
        study = input("Which study to train on? [0]: ")
    except SyntaxError as e:
        study = 0

    if study == 1:
        filename = SB22
    elif study == 2:
        filename = Benmargskohorten
    elif study == 3:
        filename = SB91b
    else:
        filename = all_studies

    try:
        columns = input("Which columns to include? (Do NOT forget trailing comma if only one column is used, e.g. '3,'\nAvailable columns are: 2, -4, -3, -2, -1. Just press ENTER for all columns.\n")
    except SyntaxError:
        columns = (2, -4, -3, -2, -1)
    #P, T = parse_file(filename, targetcols = [4, 5], inputcols = [2, -4, -3, -2, -1], ignorerows = [0], normalize = True)
    P, T = parse_file(filename, targetcols = [4, 5], inputcols = columns, ignorerows = [0], normalize = True)

    #Used for output comparison
    studies = {}
    studies[SB22] = parse_file(SB22, targetcols = [4, 5], inputcols = columns, ignorerows = [0], normalize = True)
    studies[Benmargskohorten] = parse_file(Benmargskohorten, targetcols = [4, 5], inputcols = columns, ignorerows = [0], normalize = True)
    studies[SB91b] = parse_file(SB91b, targetcols = [4, 5], inputcols = columns, ignorerows = [0], normalize = True)
    studies[all_studies] = parse_file(all_studies, targetcols = [4, 5], inputcols = columns, ignorerows = [0], normalize = True)

    #remove tail censored
    try:
        cutoff = input('Cutoff for censored data? [9999 years]: ')
    except SyntaxError as e:
        cutoff = 9999
    P, T = copy_without_censored(P, T, cutoff)

    #Divide into validation sets
    try:
        pieces = input('Size of validation set? Input denominator (1 for no validation set). Default is 1/[1] parts: ')
    except:
        pieces = 1
    TandV = get_cross_validation_sets(P, T, pieces , binary_column = 1)

    #Network part

    p = len(P[0]) #number of input covariates

    net = build_feedforward(p, netsize, 1, output_function = 'linear')
    #net = build_feedforward_multilayered(p, [7, 10], 1, output_function = 'linear')

    #Initial state
    #outputs = net.sim(tP)
    #orderscatter(outputs, tT, filename, 's')

    try:
        epochs = input("Number of generations (1): ")
    except SyntaxError as e:
        epochs = 1

    for ((tP, tT), (vP, vT)) in TandV:
        #train
        net = test(net, tP, tT, vP, vT, filename, epochs, population_size = pop_size, mutation_rate = mutation_rate)

        if plt:
            outputs = net.sim(tP)
            threshold = kaplanmeier(time_array = tT[:, 0], event_array = tT[:, 1], output_array = outputs[:, 0])
            if len(vP) > 0:
                outputs = net.sim(vP)
                kaplanmeier(time_array = vT[:, 0], event_array = vT[:, 1], output_array = outputs[:, 0], threshold = threshold)
            print("\nThreshold dividing the training set in two equal pieces: " + str(threshold))

            raw_input("\nPress enter to show plots...")
            plt.show()
        try:
            answer = input("Do you wish to print network output? Enter filename, or 'no' / 'n'. ['n']: ")
        except (SyntaxError, NameError):
            answer = 'n'
        if os.path.exists(answer):
            print("File exists. Will add random number to front")
            answer = str(random.randint(0, 123456)) + answer
        if answer != 'n' and answer != 'no':
            print_output(answer, net, filename, targetcols = [4, 5], inputcols = columns, ignorerows = [0], normalize = True)