Beispiel #1
0
def test_solve_ffn_numberadder():
    """test FFN class number adder can be solved by gradient decent
    """

    import numpy as np
    from crpm.ffn import FFN
    from crpm.dataset import load_dataset
    from crpm.gradientdecent import gradientdecent

    #init numpy seed
    np.random.seed(40017)

    #create number adder from file
    model = FFN("crpm/data/numberadder_bodyplan.csv")

    #train numberadder model  with mean squared error
    _, data = load_dataset("crpm/data/numberadder.csv")
    _, _, _ = gradientdecent(model,
                             data[0:5, ],
                             data[-1, ],
                             "mse",
                             healforces=False,
                             finetune=7)

    print(model.body[1]["weight"])

    assert np.allclose(model.body[1]["weight"], 1.0, rtol=.005)
Beispiel #2
0
def test_ffn_pre():
    """test static preprocessing block
    number adder should have negative weights with inverting pre-processor
    """

    import numpy as np
    from crpm.ffn import FFN
    from crpm.dataset import load_dataset
    from crpm.gradientdecent import gradientdecent

    #init numpy seed
    np.random.seed(40017)

    #create an inverter for number adder model input
    inverter = FFN("crpm/data/numberadder_pre_bodyplan.csv")

    #assert inverter is 5 by 5 square
    assert inverter.body[1]["weight"].shape == (5, 5)

    #manually set weight to define the negative of the identity matrix
    inverter.body[1]["weight"] = -1 * np.identity(5)

    #create number adder with inverter as pre-processor
    model = FFN("crpm/data/numberadder_bodyplan.csv", pre=inverter.body)

    #train numberadder model  with mean squared error
    _, data = load_dataset("crpm/data/numberadder.csv")
    _, _, _ = gradientdecent(model, data[0:5, ], data[-1, ], "mse", finetune=7)

    print(model.body[1]["weight"])

    assert np.allclose(model.body[1]["weight"], -1.0, rtol=.005)
Beispiel #3
0
def test_ffn_post():
    """test static post processing block
    number adder should have negative weights with inverting post-processor
    """

    import numpy as np
    from crpm.ffn import FFN
    from crpm.dataset import load_dataset
    from crpm.gradientdecent import gradientdecent

    #init numpy seed
    np.random.seed(40017)

    #create an inverter for number adder model output
    inverter = FFN("crpm/data/numberadder_post_bodyplan.csv")

    #manually set weights to define the inverter
    inverter.body[1]["weight"] = np.array([[-1 / 2], [-1 / 2]])
    inverter.body[2]["weight"] = np.array([[1, 1]])

    #create number adder with inverter as pre-processor
    model = FFN("crpm/data/numberadder_bodyplan.csv", post=inverter.body)

    #train numberadder model  with mean squared error
    _, data = load_dataset("crpm/data/numberadder.csv")
    _, _, _ = gradientdecent(model, data[0:5, ], data[-1, ], "mse", finetune=7)

    print(model.body[1]["weight"])

    assert np.allclose(model.body[1]["weight"], -1.0, rtol=.005)
def test_solve_numberadder():
    """test number adder can be solved begining with weights = 1.1
    """
    import numpy as np
    from crpm.ffn_bodyplan import read_bodyplan
    from crpm.ffn_bodyplan import init_ffn
    from crpm.dataset import load_dataset
    from crpm.gradientdecent import gradientdecent

    #create shallow bodyplan with 5 inputs and 1 output for numebr adder data
    bodyplan = read_bodyplan("crpm/data/numberadder_bodyplan.csv")

    #create numberadder model
    model = init_ffn(bodyplan)

    #manually set layer weights to 1.1 and biases to 0
    model[1]["weight"] = 1.1 * np.ones(model[1]["weight"].shape)

    #train numberadder model  with mean squared error
    _, data = load_dataset("crpm/data/numberadder.csv")
    _, _, _ = gradientdecent(model, data[0:5, ], data[-1, ], "mse")

    print(model[1]["weight"])

    assert np.allclose(model[1]["weight"], 1.0, rtol=.005)
Beispiel #5
0
def test_fwdprop_numberadder():
    """test that unit weights will make a number adder.
    """
    import numpy as np
    from crpm.ffn_bodyplan import read_bodyplan
    from crpm.ffn_bodyplan import init_ffn
    from crpm.dataset import load_dataset
    from crpm.fwdprop import fwdprop

    #create shallow bodyplan with 5 inputs and 1 output for number adder data
    bodyplan = read_bodyplan("crpm/data/numberadder_bodyplan.csv")

    #create model
    model = init_ffn(bodyplan)

    #manually set layer 1 weights to 1 and biases to 0
    model[1]["weight"] = np.ones(model[1]["weight"].shape)

    #run forward propagation with example data in numberadder.csv
    __, data = load_dataset("crpm/data/numberadder.csv")
    indepvars = data[0:5, ]
    depvars = data[-1, ]
    prediction, __ = fwdprop(indepvars, model)

    assert np.allclose(depvars, prediction, rtol=1E-7)
Beispiel #6
0
def test_numadd_forcedir():
    """test that number adder with initial wieghts >1 will have negative forces.
    """
    import numpy as np
    from crpm.ffn_bodyplan import read_bodyplan
    from crpm.ffn_bodyplan import init_ffn
    from crpm.dataset import load_dataset
    from crpm.fwdprop import fwdprop
    from crpm.lossfunctions import loss
    from crpm.backprop import backprop

    #create shallow bodyplan for numberadder.csv data
    bodyplan = read_bodyplan("crpm/data/numberadder_bodyplan.csv")

    #create numberadder model
    addermodel = init_ffn(bodyplan)

    #manually set layer 1 weights to 1.1 and biases to 0
    addermodel[1]["weight"] = 1.1 * np.ones(addermodel[1]["weight"].shape)

    #compute forces using numberadder.csv data with mean squared error
    __, data = load_dataset("crpm/data/numberadder.csv")
    pred, state = fwdprop(data[0:5,], addermodel)
    __, dloss = loss("mse", pred, data[-1,])
    forces, _ = backprop(addermodel, state, dloss)

    assert np.all(forces[-1]["fweight"] < 0)
Beispiel #7
0
def test_backprop_numberadder():
    """test that solved number adder will have zero forces with proper shape.
    """
    import numpy as np
    from crpm.ffn_bodyplan import read_bodyplan
    from crpm.ffn_bodyplan import init_ffn
    from crpm.dataset import load_dataset
    from crpm.fwdprop import fwdprop
    from crpm.lossfunctions import loss
    from crpm.backprop import backprop

    #create shallow bodyplan for numberadder.csv data
    bodyplan = read_bodyplan("crpm/data/numberadder_bodyplan.csv")

    #create numberadder model
    addermodel = init_ffn(bodyplan)

    #manually set layer 1 weights to 1 and biases to 0
    addermodel[1]["weight"] = np.ones(addermodel[1]["weight"].shape)

    #compute forces using numberadder.csv data with mean squared error
    __, data = load_dataset("crpm/data/numberadder.csv")
    pred, state = fwdprop(data[0:5,], addermodel)
    __, dloss = loss("mse", pred, data[-1,])
    forces, _ = backprop(addermodel, state, dloss)

    assert forces[-1]["fweight"].shape == (1, 5)
    assert np.allclose(1+forces[-1]["fweight"], 1, rtol=1E-7)
    assert forces[-1]["fbias"].shape == (1, 1)
    assert np.allclose(1+forces[-1]["fbias"], 1, rtol=1E-7)
Beispiel #8
0
def setup_overfitting_shallow():
    """ will return shallow model and downloaded data."""

    from crpm.ffn_bodyplan import read_bodyplan
    from crpm.ffn_bodyplan import init_ffn
    from crpm.dataset import load_dataset

    #create model from  bodyplan file
    bodyplan = read_bodyplan("crpm/data/overfitting_shallow_bodyplan.csv")

    #create model
    model = init_ffn(bodyplan)

    #download data
    __, traindata = load_dataset("crpm/data/overfitting_training.csv")
    keys, validdata = load_dataset("crpm/data/overfitting_validation.csv")

    return model, keys[1:], traindata[1:, :], validdata[1:, :]
def test_solve_numberadder():
    """test number adder can be solved begining with init weights set
    """
    import numpy as np
    from crpm.ffn_bodyplan import read_bodyplan
    from crpm.dataset import load_dataset
    from crpm.ffn_bodyplan import init_ffn
    from crpm.fwdprop import fwdprop
    from crpm.lossfunctions import loss
    from crpm.langevindynamics import langevindynamics


    #load data
    __, data = load_dataset("crpm/data/numberadder.csv")
    __, testdata = load_dataset("crpm/data/numberadder_test.csv")

    #create shallow bodyplan with 5 inputs and 1 output for numebr adder data
    bodyplan = read_bodyplan("crpm/data/numberadder_bodyplan.csv")

    #create numberadder model
    model = init_ffn(bodyplan)

    #manually set layer weights to 1.5 and biases to 0
    model[1]["weight"] = 1.5*np.ones(model[1]["weight"].shape)

    #calculate initial mean squared error
    pred, __ = fwdprop(data[0:5,], model)
    icost, __ = loss("mse", pred, data[-1,])
    print("icost = "+str(icost))
    print(model[1]["weight"])

    #train numberadder model  with mean squared error
    __, cost = langevindynamics(model, data[0:5,], data[-1,],
                                "mse", testdata[0:5,], testdata[-1,],
                                maxepoch=int(3E5), maxbuffer=int(1E3))
    print("cost ="+str(cost))
    print(model[1]["weight"])

    assert icost > cost
    assert np.allclose(model[1]["weight"], 1.0, rtol=.005)
Beispiel #10
0
def setup_multicorrel_deep_c():
    """ will return deep model and downloaded data."""

    from crpm.ffn_bodyplan import read_bodyplan
    from crpm.ffn_bodyplan import init_ffn
    from crpm.dataset import load_dataset

    #create model from  bodyplan file
    bodyplan = read_bodyplan("crpm/data/multicorrel_deep_bodyplan.csv")

    #create model
    model = init_ffn(bodyplan)

    #download nestedCs data
    __, data = load_dataset("crpm/data/multicorrel_C.csv")

    return model, data
Beispiel #11
0
def setup_periodiccases():
    """ will return model and downloaded data."""

    from crpm.ffn_bodyplan import read_bodyplan
    from crpm.ffn_bodyplan import init_ffn
    from crpm.dataset import load_dataset

    #create model from bodyplan file
    bodyplan = read_bodyplan("crpm/data/periodiccases_bodyplan.csv")

    #create model
    model = init_ffn(bodyplan)

    #download data
    __, data = load_dataset("crpm/data/periodiccases.csv")

    return model, data
Beispiel #12
0
def setup_toruscases_deep():
    """ will return model and downloaded data."""

    from crpm.ffn_bodyplan import read_bodyplan
    from crpm.ffn_bodyplan import init_ffn
    from crpm.dataset import load_dataset

    #create model from deep bodyplan file
    bodyplan = read_bodyplan("crpm/data/intorus_deep_bodyplan.csv")

    #create model
    model = init_ffn(bodyplan)

    #download data
    __, data = load_dataset("crpm/data/intorus.csv")

    return model, data
Beispiel #13
0
def setup_numberadder():
    """ will return numberadder model and downloaded data."""

    from crpm.ffn_bodyplan import read_bodyplan
    from crpm.ffn_bodyplan import init_ffn
    from crpm.dataset import load_dataset

    #create model from  bodyplan file
    bodyplan = read_bodyplan("crpm/data/numberadder_bodyplan.csv")

    #create model
    model = init_ffn(bodyplan)

    #download data
    keys, data = load_dataset("crpm/data/numberadder.csv")

    return model, keys, data