def __init__(self, discount=0.95, exploration_rate=1.0, exploration_rate_decay=.99, target_every=2): """ define deep network hyperparameters""" self.discount = discount # how much future rewards are valued w.r.t. current self.exploration_rate = exploration_rate # initial exploration rate self.exploration_rate_decay = exploration_rate_decay # transition from exploration to expliotation self.target_every = target_every #how many iterations to skip before we swap prediciton network with target network #retrieve the body plan #input has 6 neurons, one for each metabolite conc. and one for time horizon #output has 1 neuron, representing the only action and its value function approximation #~~output has 3 neurons, representing the Q values for each of the 3 actions #~~ action 0 is no treatment, action 1 is drug1 Tx, and and action 2 is for drug2 Tx self.bodyplan = read_bodyplan("crpm/data/abbc_bodyplan.csv") #define prediction network self.prednet = init_ffn(self.bodyplan) self.loss = None #current prediction error #init the target network(s) self.targetnet1 = init_ffn(self.bodyplan) self.targetnet2 = init_ffn(self.bodyplan) self.targetnet3 = init_ffn(self.bodyplan) self.targetnet4 = init_ffn(self.bodyplan) #with the prediciton network #self.targetnet = copy_ffn(self.prednet) #init counter used to determine when to update target network with prediction network self.iteration = 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)
def test_example_bodyplan(): """Check example_ffn_bodyplan.csv is read as we expect """ from crpm.ffn_bodyplan import read_bodyplan bodyplan = read_bodyplan("crpm/data/example_ffn_bodyplan.csv") #confirm 4 layers deep assert len(bodyplan) == 5 #confirm parameters for each layer assert bodyplan[0]["layer"] == 0 assert bodyplan[0]["n"] == 2 assert bodyplan[0]["activation"] == "linear" assert bodyplan[1]["layer"] == 1 assert bodyplan[1]["n"] == 3 assert bodyplan[1]["activation"] == "relu" assert bodyplan[2]["layer"] == 2 assert bodyplan[2]["n"] == 5 assert bodyplan[2]["activation"] == "relu" assert bodyplan[3]["layer"] == 3 assert bodyplan[3]["n"] == 7 assert bodyplan[3]["activation"] == "relu" assert bodyplan[4]["layer"] == 4 assert bodyplan[4]["n"] == 1 assert bodyplan[4]["activation"] == "logistic"
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)
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)
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)
def __init__(self, desc, std=None, pre=None, post=None): """define model from description with options for pre and post procs and inital weight distribution. """ from crpm.ffn_bodyplan import read_bodyplan from crpm.ffn_bodyplan import init_ffn #save weight variance parameter self.weightstd = std #get bodyplan from a file description if isinstance(desc, str): self.bodyplan = read_bodyplan(desc) #get bodyplan from a list description if isinstance(desc, list): self.bodyplan = desc #define model from bodyplan self.body = init_ffn(self.bodyplan, weightstd=self.weightstd) #link static pre-processing body self.pre = pre #append indicator in description if applicable if self.pre is not None: for layer in self.pre: layer["desc"] = layer["desc"] + str(' static pre-processor') #link static post-processing body self.post = post #append indicator in description if applicable if self.post is not None: for layer in self.post: layer["desc"] = layer["desc"] + str(' static post-processor')
def setup_afmodel(): """ will return model prototype and downloaded data.""" import numpy as np 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/afmodel_bodyplan.csv") #create model prototype = init_ffn(bodyplan) #download data data = np.load("crpm/data/afmodel.npz") #get list of keys in data (represents individual arrays) keylist = [] for key in data.keys(): keylist.append(key) #return encoder protype, cohort1 data, cohort1 labels, cohort2 data, cohort2 labels return prototype, data[keylist[0]], data[keylist[1]], data[ keylist[2]], data[keylist[3]]
def test_init_ffn_from_bodyplan(): """Test ffn is created properly from example_bodyplan """ from crpm.ffn import FFN from crpm.ffn_bodyplan import read_bodyplan bodyplan = read_bodyplan("crpm/data/example_ffn_bodyplan.csv") #create FFN from bodyplan model = FFN(bodyplan) example_bodyplan_assertions(model)
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
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
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
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
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_init_ffn_types(): """check if elements in layer dictionaries are of the correct type weights and biases should be ndarrays """ import numpy as np from crpm.ffn_bodyplan import read_bodyplan from crpm.ffn_bodyplan import init_ffn bodyplan = read_bodyplan("crpm/data/example_ffn_bodyplan.csv") model = init_ffn(bodyplan) for layer in model: assert isinstance(layer["layer"], int) assert isinstance(layer["n"], int) assert isinstance(layer["activation"], str) if layer["layer"] > 0: assert isinstance(layer["regval"], float) assert isinstance(layer["weight"], np.ndarray) assert isinstance(layer["bias"], np.ndarray)
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)
def setup_spectra2(): """ will return model and downloaded data.""" import numpy as np 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/spectra2_bodyplan.csv") #create model model = init_ffn(bodyplan) #download data data = np.load("crpm/data/spectra2.npz") #get list of keys in data (represents individual arrays) keylist = [] for key in data.keys(): keylist.append(key) return model, data[keylist[0]]
def test_init_ffn(): """Test ffn is created properly from example_bodyplan.csv """ from crpm.ffn_bodyplan import read_bodyplan from crpm.ffn_bodyplan import init_ffn bodyplan = read_bodyplan("crpm/data/example_ffn_bodyplan.csv") model = init_ffn(bodyplan) assert model[0]["layer"] == 0 assert model[1]["layer"] == 1 assert model[2]["layer"] == 2 assert model[3]["layer"] == 3 assert model[4]["layer"] == 4 assert model[0]["n"] == 2 assert model[1]["n"] == 3 assert model[2]["n"] == 5 assert model[3]["n"] == 7 assert model[4]["n"] == 1 assert model[1]["weight"].shape == (3, 2) assert model[2]["weight"].shape == (5, 3) assert model[3]["weight"].shape == (7, 5) assert model[4]["weight"].shape == (1, 7) assert model[1]["bias"].shape == (3, 1) assert model[2]["bias"].shape == (5, 1) assert model[3]["bias"].shape == (7, 1) assert model[4]["bias"].shape == (1, 1) assert model[0]["activation"] == 'linear' assert model[1]["activation"] == 'relu' assert model[2]["activation"] == 'relu' assert model[3]["activation"] == 'relu' assert model[4]["activation"] == 'logistic'