def build_model(model, model_in, model_out, network_shape, activations, weight_decays, learning_rate): if (len(activations) == 1): activations = activations * (len(network_shape)) if (len(weight_decays) - len(network_shape) < 1): k = deepcopy(network_shape) k[:len(weight_decays) - 1] = weight_decays[:-1] k[len(weight_decays) - 1:-1] = len(k[len(weight_decays) - 1:-1]) * [weight_decays[-2]] k[-1] = weight_decays[-1] weight_decays = k activation = activations for idx, extras in enumerate(zip(network_shape, weight_decays[:-1])): hidden_units, weight_decay = extras if (idx == 0): model.add( FC(hidden_units, input_dim=model_in, activation=activation, weight_decay=weight_decay)) else: model.add( FC(hidden_units, activation=activation, weight_decay=weight_decay)) model.add(FC(model_out, weight_decay=weight_decays[-1])) # print("\n\n\n\n this is the type of the learning rate = {} \n\n\n\n".format(type(learning_rate))) # model.finalize(tf.train.AdamOptimizer, {"learning_rate": learning_rate}) return model
def value_nn_constructor(self, name, model_init_cfg_val): model = get_required_argument(model_init_cfg_val, "model_class", "Must provide model class")(DotMap( name=name, num_networks=get_required_argument(model_init_cfg_val, "num_nets", "Must provide ensemble size"), sess=self.SESS, load_model=model_init_cfg_val.get("load_model", False), model_dir=model_init_cfg_val.get("model_dir", None) )) if not model_init_cfg_val.get("load_model", False): model.add(FC(500, input_dim=self.VALUE_IN, activation='swish', weight_decay=0.0001)) model.add(FC(500, activation='swish', weight_decay=0.00025)) model.add(FC(500, activation='swish', weight_decay=0.00025)) model.add(FC(self.VALUE_OUT, weight_decay=0.0005)) model.finalize(tf.train.AdamOptimizer, {"learning_rate": 0.001}, suffix = "val") return model
def nn_constructor(self, model_init_cfg): model = get_required_argument(model_init_cfg, "model_class", "Must provide model class")(DotMap( name="model", num_networks=get_required_argument(model_init_cfg, "num_nets", "Must provide ensemble size"), sess=self.SESS, load_model=model_init_cfg.get("load_model", False), model_dir=model_init_cfg.get("model_dir", None) )) if not model_init_cfg.get("load_model", False): model.add(FC(200, input_dim=self.MODEL_IN, activation="swish", weight_decay=0.00025)) model.add(FC(200, activation="swish", weight_decay=0.0005)) model.add(FC(200, activation="swish", weight_decay=0.0005)) model.add(FC(200, activation="swish", weight_decay=0.0005)) model.add(FC(self.MODEL_OUT, weight_decay=0.00075)) model.finalize(tf.train.AdamOptimizer, {"learning_rate": 0.00075}) return model
def bnn_constructor(model_init_cfg): """ Constructs the Bayesian Neural Network model. Moodel_init_cfg is a dotmap object containing: - model_in (int): Number of inputs to the model. - model_out (int): Number of outputs to the model. - n_layers (int): Number of hidden layers. - n_neurons (int): Number of neurons per hidden layer. - learning_rate (float): Learning rate. - wd_in (float): Weight decay for the input layer neurons. - wd_hid (float): Weight decay for the hidden layer neurons. - wd_out (float): Weight decay for the output layer neurons. Returns: BNN class object """ cfg = tf.ConfigProto() cfg.gpu_options.allow_growth = True SESS = tf.Session(config=cfg) # Tensorflow session model = BNN( DotMap(name=get_required_argument(model_init_cfg, "model_name", "Must provide model name size"), num_networks=get_required_argument( model_init_cfg, "num_nets", "Must provide ensemble size"), sess=SESS, load_model=model_init_cfg.get("load_model", False), model_dir=model_init_cfg.get("model_dir", None))) if not model_init_cfg.get("load_model", False): model.add( FC(model_init_cfg.n_neurons, input_dim=model_init_cfg.model_in, activation="swish", weight_decay=model_init_cfg.wd_in)) for i in range(model_init_cfg.n_layers): model.add( FC(model_init_cfg.n_neurons, activation="swish", weight_decay=model_init_cfg.wd_hid)) model.add( FC(model_init_cfg.model_out, weight_decay=model_init_cfg.wd_out)) model.finalize(tf.train.AdamOptimizer, {"learning_rate": model_init_cfg.learning_rate}) return model
def create_bnn(X, y): model = BNN(DotMap(name="test")) model.add(FC(OUT_DIM, input_dim=IN_DIM, weight_decay=0.0005)) # linear model for simplicity model.finalize(tf.train.AdamOptimizer, {"learning_rate": 0.03}) model.train(X, y, epochs=1000, batch_size=64) model.calibrate(X, y) # model.plot_calibration(X, y) return model
def _load_structure(self): """Uses the saved structure in self.model_dir with the name of this network to initialize the structure of this network. """ structure = [] with open(os.path.join(self.model_dir, "%s.nns" % self.name), "r") as f: for line in f: kwargs = { key: val for (key, val) in [argval.split("=") for argval in line[3:-2].split(", ")] } kwargs["input_dim"] = int(kwargs["input_dim"]) kwargs["output_dim"] = int(kwargs["output_dim"]) kwargs["weight_decay"] = None if kwargs["weight_decay"] == "None" else float(kwargs["weight_decay"]) kwargs["activation"] = None if kwargs["activation"] == "None" else kwargs["activation"][1:-1] kwargs["ensemble_size"] = int(kwargs["ensemble_size"]) structure.append(FC(**kwargs)) self.layers = structure