Esempio n. 1
0
  def compute_nll(self, x, t, e):
    r"""This function computes the negative log likelihood of the given data.
    In case of competing risks, the negative log likelihoods are summed over
    the different events' type.

    Parameters
    ----------
    x: np.ndarray
        A numpy array of the input features, \( x \).
    t: np.ndarray
        A numpy array of the event/censoring times, \( t \).
    e: np.ndarray
        A numpy array of the event/censoring indicators, \( \delta \).
        \( \delta = r \) means the event r took place.

    Returns:
      float: Negative log likelihood.
    """
    if not self.fitted:
      raise Exception("The model has not been fitted yet. Please fit the " +
                      "model using the `fit` method on some training data " +
                      "before calling `_eval_nll`.")
    processed_data = self._prepocess_training_data(x, t, e, 0, None, 0)
    _, _, _, x_val, t_val, e_val = processed_data
    x_val, t_val, e_val = x_val,\
        _reshape_tensor_with_nans(t_val),\
        _reshape_tensor_with_nans(e_val)
    loss = 0
    x_val, t_val, e_val = x_val.cuda(), t_val.cuda(), e_val.cuda()
    for r in range(self.torch_model.risks):
      loss += float(losses.conditional_loss(self.torch_model,
                    x_val, t_val, e_val, elbo=False,
                    risk=str(r+1)).detach().cpu().numpy())
    return loss
Esempio n. 2
0
def train_dsm(model,
              x_train,
              t_train,
              e_train,
              x_valid,
              t_valid,
              e_valid,
              n_iter=10000,
              lr=1e-3,
              elbo=True,
              bs=100):
    """Function to train the torch instance of the model."""

    logging.info('Pretraining the Underlying Distributions...')
    # For padded variable length sequences we first unroll the input and
    # mask out the padded nans.
    t_train_ = _reshape_tensor_with_nans(t_train)
    e_train_ = _reshape_tensor_with_nans(e_train)
    t_valid_ = _reshape_tensor_with_nans(t_valid)
    e_valid_ = _reshape_tensor_with_nans(e_valid)

    premodel = pretrain_dsm(model,
                            t_train_,
                            e_train_,
                            t_valid_,
                            e_valid_,
                            n_iter=10000,
                            lr=1e-2,
                            thres=1e-4)

    for r in range(model.risks):
        model.shape[str(r + 1)].data.fill_(float(premodel.shape[str(r + 1)]))
        model.scale[str(r + 1)].data.fill_(float(premodel.scale[str(r + 1)]))

    model.double()
    optimizer = get_optimizer(model, lr)

    patience = 0
    oldcost = float('inf')

    nbatches = int(x_train.shape[0] / bs) + 1

    dics = []
    costs = []
    i = 0
    for i in tqdm(range(n_iter)):
        for j in range(nbatches):

            xb = x_train[j * bs:(j + 1) * bs]
            tb = t_train[j * bs:(j + 1) * bs]
            eb = e_train[j * bs:(j + 1) * bs]

            if xb.shape[0] == 0:
                continue

            optimizer.zero_grad()
            loss = 0
            for r in range(model.risks):
                loss += conditional_loss(model,
                                         xb,
                                         _reshape_tensor_with_nans(tb),
                                         _reshape_tensor_with_nans(eb),
                                         elbo=elbo,
                                         risk=str(r + 1))
            #print ("Train Loss:", float(loss))
            loss.backward()
            optimizer.step()

        valid_loss = 0
        for r in range(model.risks):
            valid_loss += conditional_loss(model,
                                           x_valid,
                                           t_valid_,
                                           e_valid_,
                                           elbo=False,
                                           risk=str(r + 1))

        valid_loss = valid_loss.detach().cpu().numpy()
        costs.append(float(valid_loss))
        dics.append(deepcopy(model.state_dict()))

        if costs[-1] >= oldcost:
            if patience == 2:
                minm = np.argmin(costs)
                model.load_state_dict(dics[minm])

                del dics
                gc.collect()

                return model, i
            else:
                patience += 1
        else:
            patience = 0

        oldcost = costs[-1]

    minm = np.argmin(costs)
    model.load_state_dict(dics[minm])

    del dics
    gc.collect()

    return model, i
Esempio n. 3
0
def train_dsm(model,
              x_train, t_train, e_train,
              x_valid, t_valid, e_valid,
              n_iter=10000, lr=1e-3, elbo=True,
              bs=100):

  print('Pretraining the Underlying Distributions...')

  print(t_train.shape, e_train.shape)

  t_train_ = _reshape_tensor_with_nans(t_train)
  e_train_ = _reshape_tensor_with_nans(e_train)
  t_valid_ = _reshape_tensor_with_nans(t_valid)
  e_valid_ = _reshape_tensor_with_nans(e_valid)

  print(t_train_.shape, e_train_.shape)

  premodel = pretrain_dsm(model,
                          t_train_,
                          e_train_,
                          t_valid_,
                          e_valid_,
                          n_iter=10000,
                          lr=1e-2,
                          thres=1e-4)
  model.shape.data.fill_(float(premodel.shape))
  model.scale.data.fill_(float(premodel.scale))

  print(float(premodel.shape), float(premodel.scale))

  model.double()
  optimizer = torch.optim.Adam(model.parameters(), lr=lr)

  patience = 0
  oldcost = float('inf')

  nbatches = int(x_train.shape[0]/bs)+1

  dics = []
  costs = []
  i = 0
  for i in tqdm(range(n_iter)):
    for j in range(nbatches):

      xb = x_train[j*bs:(j+1)*bs]
      tb = t_train[j*bs:(j+1)*bs]
      eb = e_train[j*bs:(j+1)*bs]

      optimizer.zero_grad()
      loss = conditional_loss(model,
                              xb,
                              _reshape_tensor_with_nans(tb),
                              _reshape_tensor_with_nans(eb),
                              elbo=elbo)
      #print ("Train Loss:", float(loss))
      loss.backward()
      optimizer.step()

    valid_loss = conditional_loss(model,
                                  x_valid,
                                  t_valid_,
                                  e_valid_,
                                  elbo=False)

    valid_loss = valid_loss.detach().cpu().numpy()
    costs.append(float(valid_loss))
    dics.append(deepcopy(model.state_dict()))

    if (costs[-1] >= oldcost) is True:
      if patience == 2:
        maxm = np.argmax(costs)
        model.load_state_dict(dics[maxm])

        del dics
        gc.collect()
        return model, i
      else:
        patience += 1
    else:
      patience = 0

    oldcost = costs[-1]

  return model, i