def train_nn(pred_fun, loss_fun, num_weights, train_smiles, train_raw_targets, train_params, seed=0,
             validation_smiles=None, validation_raw_targets=None):
    """loss_fun has inputs (weights, smiles, targets)"""
    print "Total number of weights in the network:", num_weights
    init_weights = npr.RandomState(seed).randn(num_weights) * train_params['init_scale']

    train_targets, undo_norm = normalize_array(train_raw_targets)
    training_curve = []
    def callback(weights, iter):
        if iter % 10 == 0:
            print "max of weights", np.max(np.abs(weights))
            train_preds = undo_norm(pred_fun(weights, train_smiles))
            cur_loss = loss_fun(weights, train_smiles, train_targets)
            training_curve.append(cur_loss)
            print "Iteration", iter, "loss", cur_loss, "train RMSE", rmse(train_preds, train_raw_targets),
            if validation_smiles is not None:
                validation_preds = undo_norm(pred_fun(weights, validation_smiles))
                print "Validation RMSE", iter, ":", rmse(validation_preds, validation_raw_targets),

    # Build gradient using autograd.
    grad_fun = grad(loss_fun)
    grad_fun_with_data = build_batched_grad(grad_fun, train_params['batch_size'],
                                            train_smiles, train_targets)

    # Optimize weights.
    trained_weights = adam(grad_fun_with_data, init_weights, callback=callback,
                           num_iters=train_params['num_iters'], step_size=train_params['step_size'],
                           b1=train_params['b1'], b2=train_params['b2'])

    def predict_func(new_smiles):
        """Returns to the original units that the raw targets were in."""
        return undo_norm(pred_fun(trained_weights, new_smiles))
    return predict_func, trained_weights, training_curve
def train_nn(pred_fun, loss_fun, num_weights, train_smiles, train_raw_targets, train_params,
             validation_smiles=None, validation_raw_targets=None):
    """loss_fun has inputs (weights, smiles, targets)"""
    print "Total number of weights in the network:", num_weights
    npr.seed(0)
    init_weights = npr.randn(num_weights) * train_params['param_scale']

    train_targets, undo_norm = normalize_array(train_raw_targets)
    training_curve = []
    def callback(weights, iter):
        if iter % 10 == 0:
            print "max of weights", np.max(np.abs(weights))
            train_preds = undo_norm(pred_fun(weights, train_smiles))
            cur_loss = loss_fun(weights, train_smiles, train_targets)
            training_curve.append(cur_loss)
            print "Iteration", iter, "loss", cur_loss, "train RMSE", \
                np.sqrt(np.mean((train_preds - train_raw_targets)**2)),
            if validation_smiles is not None:
                validation_preds = undo_norm(pred_fun(weights, validation_smiles))
                print "Validation RMSE", iter, ":", \
                    np.sqrt(np.mean((validation_preds - validation_raw_targets) ** 2)),

    grad_fun = grad(loss_fun)
    grad_fun_with_data = build_batched_grad(grad_fun, train_params['batch_size'],
                                            train_smiles, train_targets)

    num_iters = train_params['num_epochs'] * len(train_smiles) / train_params['batch_size']
    trained_weights = adam(grad_fun_with_data, init_weights, callback=callback,
                           num_iters=num_iters, step_size=train_params['learn_rate'],
                           b1=train_params['b1'], b2=train_params['b2'])

    def predict_func(new_smiles):
        """Returns to the original units that the raw targets were in."""
        return undo_norm(pred_fun(trained_weights, new_smiles))
    return predict_func, trained_weights, training_curve
Beispiel #3
0
def train_nn(pred_fun, loss_fun, num_weights, train_smiles, train_raw_targets, train_params, seed=0,
             validation_smiles=None, validation_raw_targets=None):
    """loss_fun has inputs (weights, smiles, targets)"""
    print "Total number of weights in the network:", num_weights
    init_weights = npr.RandomState(seed).randn(num_weights) * train_params['init_scale']

    num_print_examples = 100
    train_targets, undo_norm = normalize_array(train_raw_targets)
    training_curve = []
    def callback(weights, iter):
        if iter % 10 == 0:
            print "max of weights", np.max(np.abs(weights))
            train_preds = undo_norm(pred_fun(weights, train_smiles[:num_print_examples]))
            cur_loss = loss_fun(weights, train_smiles[:num_print_examples], train_targets[:num_print_examples])
            training_curve.append(cur_loss)
            print "Iteration", iter, "loss", cur_loss,\
                  "train RMSE", rmse(train_preds, train_raw_targets[:num_print_examples]),
            if validation_smiles is not None:
                validation_preds = undo_norm(pred_fun(weights, validation_smiles))
                print "Validation RMSE", iter, ":", rmse(validation_preds, validation_raw_targets),

    # Build gradient using autograd.

    grad_fun = grad(loss_fun)
    grad_fun_with_data = build_batched_grad(grad_fun, train_params['batch_size'],
                                            train_smiles, train_targets)
    # Optimize weights.
    trained_weights = adam(grad_fun_with_data, init_weights, callback=callback,
                           num_iters=train_params['num_iters'], step_size=train_params['step_size'])

    def predict_func(new_smiles):
        """Returns to the original units that the raw targets were in."""
        return undo_norm(pred_fun(trained_weights, new_smiles))
    return predict_func, trained_weights, training_curve
    def train_nn(self,
                 pred_fun,
                 loss_fun,
                 num_weights,
                 train_smiles,
                 train_raw_targets,
                 train_params,
                 validation_smiles=None,
                 validation_raw_targets=None):
        #def train_nn(self, pred_fun, loss_fun, num_weights, train_fps, train_raw_targets, train_params,
        #             validation_smiles=None, validation_raw_targets=None):
        """loss_fun has inputs (weights, smiles, targets)"""
        print "Total number of weights in the network:", num_weights
        npr.seed(0)
        init_weights = npr.randn(num_weights) * train_params['param_scale']
        init_weights[-1] = self.other_param_dict['init_bias']

        train_targets, undo_norm = normalize_array(train_raw_targets)
        training_curve = []

        def callback(weights, iter):
            if iter % 20 == 0:
                print "max of weights", np.max(np.abs(weights))
                train_preds = undo_norm(pred_fun(weights, train_smiles))
                cur_loss = loss_fun(weights, train_smiles, train_raw_targets)
                training_curve.append(cur_loss)
                print "Iteration", iter, "loss", cur_loss,

        grad_fun = grad(loss_fun)
        grad_fun_with_data = build_batched_grad(grad_fun,
                                                train_params['batch_size'],
                                                train_smiles,
                                                train_raw_targets)

        #num_iters = train_params['num_epochs'] * np.shape(train_fps)[0] / train_params['batch_size']
        num_iters = train_params['num_epochs'] * np.shape(
            train_smiles)[0] / train_params['batch_size']
        trained_weights = adam(grad_fun_with_data,
                               init_weights,
                               callback=callback,
                               num_iters=num_iters,
                               step_size=train_params['learn_rate'])

        #b1=train_params['b1'], b2=train_params['b2'])

        def predict_func(new_smiles):
            """Returns to the original units that the raw targets were in."""
            return pred_fun(trained_weights, new_smiles)

        def predict_func_fps(new_fps):
            """ return function for fps """
            return pred_fun(trained_weights, new_fps)

        return predict_func, trained_weights, training_curve
Beispiel #5
0
    def fit(self, smiles_list, logS_list, seed=0):
        train_smiles = [smiles for smiles in smiles_list]
        train_logS = [logS for logS in logS_list]

        conv_layer_sizes = [self.model_params['conv_width']
                            ] * self.model_params['fp_depth']
        conv_arch_params = {
            'num_hidden_features': conv_layer_sizes,
            'fp_length': self.model_params['fp_length'],
            'normalize': 1
        }

        # Neural net architecture
        net_arch_params = dict(layer_sizes=[
            self.model_params['fp_length'], self.model_params['h1_size']
        ],
                               normalize=True,
                               L2_reg=self.model_params['L2_reg'],
                               nll_func=rmse)

        loss_fun, pred_fun, conv_parser = build_conv_deep_net(
            conv_arch_params, net_arch_params, self.model_params['L2_reg'])

        num_weights = len(conv_parser)
        init_weights = npr.RandomState(seed).randn(
            num_weights) * self.train_params['init_scale']

        train_logS_norm, undo_norm = normalize_array(train_logS)

        # Build gradient using autograd.
        grad_fun = grad(loss_fun)
        grad_fun_with_data = build_batched_grad(
            grad_fun, self.train_params['batch_size'], train_smiles,
            train_logS_norm)

        # Optimize weights.
        trained_weights = adam(grad_fun_with_data,
                               init_weights,
                               num_iters=self.train_params['num_iters'],
                               step_size=self.train_params['step_size'])

        self.model = (undo_norm, trained_weights, pred_fun)
def train_nn(net_objects, smiles, raw_targets, callback, normalize_outputs,
             seed, init_scale, batch_size, num_iters, **opt_params):
    loss_fun, pred_fun, net_parser = net_objects
    init_weights = init_scale * npr.RandomState(seed).randn(len(net_parser))
    if normalize_outputs:
        targets, undo_norm = normalize_array(raw_targets)
    else:
        targets, undo_norm = raw_targets, lambda x: x

    def make_predict_func(new_weights):
        return lambda new_smiles: undo_norm(pred_fun(new_weights, new_smiles))

    def opt_callback(weights, i):
        callback(make_predict_func(weights), i)

    grad_fun = build_batched_grad(grad(loss_fun), batch_size, smiles, targets)
    trained_weights = adam(grad_fun,
                           init_weights,
                           callback=opt_callback,
                           num_iters=num_iters,
                           **opt_params)
    return trained_weights
def train_nn(pred_fun, loss_fun, num_weights, train_smiles, train_raw_targets, train_params, seed=0,
             validation_smiles=None, validation_raw_targets=None):
    """loss_fun has inputs (weights, smiles, targets)"""
    print "Total number of weights in the network:", num_weights
    init_weights = npr.RandomState(seed).randn(num_weights) * train_params['init_scale']

    num_print_examples = 100
    train_targets, undo_norm = normalize_array(train_raw_targets)
    training_curve = []
    def callback(weights, iter):
        if iter % 10 == 0:
            print "max of weights", np.max(np.abs(weights))
            # import pdb; pdb.set_trace()
            train_preds = undo_norm(pred_fun(weights, train_smiles[:num_print_examples]))
            cur_loss = loss_fun(weights, train_smiles[:num_print_examples], train_targets[:num_print_examples]) # V: refers to line number #78 i.e.
                                                                                                                # def loss_fun(weights, smiles, targets) of build_vanilla_net.py
            training_curve.append(cur_loss)
            print "Iteration", iter, "loss", cur_loss,\
                  "train RMSE", rmse(train_preds, train_raw_targets[:num_print_examples]),
            if validation_smiles is not None:
                validation_preds = undo_norm(pred_fun(weights, validation_smiles))
                print "Validation RMSE", iter, ":", rmse(validation_preds, validation_raw_targets),

    # Build gradient using autograd.
    grad_fun = grad(loss_fun)                       # V: grad_fun acts as a gradient function which accept same arguments as that of loss_fun(Weights,Input,target) and,
                                                    # returns the gradient of weights for that sample (input, target) 
    # import pdb; pdb.set_trace()
    grad_fun_with_data = build_batched_grad(grad_fun, train_params['batch_size'],
                                            train_smiles, train_targets)

    # Optimize weights.
    trained_weights = adam(grad_fun_with_data, init_weights, callback=callback,
                           num_iters=train_params['num_iters'], step_size=train_params['step_size'])

    def predict_func(new_smiles):
        """Returns to the original units that the raw targets were in."""
        return undo_norm(pred_fun(trained_weights, new_smiles))
    return predict_func, trained_weights, training_curve
def train_nn(pred_fun,
             loss_fun,
             nll_func_name,
             nll_func,
             num_weights,
             train_smiles,
             train_raw_targets,
             train_params,
             seed=0,
             validation_smiles=None,
             validation_raw_targets=None):
    """loss_fun has inputs (weights, smiles, targets)"""
    print("Total number of weights in the network: {}".format(num_weights))

    init_weights = npr.RandomState(seed).randn(
        num_weights) * train_params['init_scale']

    train_targets, undo_norm = normalize_array(train_raw_targets)

    training_curve = []

    def callback(weights, iter):
        if iter % 10 == 0:
            print("Iteration {}".format(iter))
            print("\tmax of weights: {}".format(np.max(np.abs(weights))))

            cur_loss = loss_fun(weights, train_smiles, train_targets)
            training_curve.append(cur_loss)
            print("\tloss {}".format(cur_loss))

            train_preds = undo_norm(pred_fun(weights, train_smiles))
            print("\ttrain {}: {}".format(
                nll_func_name, nll_func(train_preds, train_raw_targets)))

            if validation_smiles is not None:
                validation_preds = undo_norm(
                    pred_fun(weights, validation_smiles))
                print("\tvalidation {}: {}".format(
                    nll_func_name,
                    nll_func(validation_preds, validation_raw_targets)))

    # Build gradient using autograd.
    grad_fun = grad(loss_fun)
    grad_fun_with_data = build_batched_grad(grad_fun,
                                            train_params['batch_size'],
                                            train_smiles, train_targets)

    # Optimize weights.
    trained_weights = adam(grad_fun_with_data,
                           init_weights,
                           callback=callback,
                           num_iters=train_params['num_iters'],
                           step_size=train_params['step_size'],
                           b1=train_params['b1'],
                           b2=train_params['b2'])

    def predict_func(new_smiles):
        """Returns to the original units that the raw targets were in."""
        return undo_norm(pred_fun(trained_weights, new_smiles))

    return predict_func, trained_weights, training_curve