code: def test_uncertainty():
      """Test estimating uncertainty a TorchModel."""
      n_samples = 30
      n_features = 1
      noise = 0.1
      X = np.random.rand(n_samples, n_features)
      y = 10 * X + np.random.normal(
          scale=noise, size=(n_samples, n_features)
      )
      dataset = dc.data.NumpyDataset(X, y)
  
      # Build a model that predicts uncertainty.
  
      class PyTorchUncertainty(torch.nn.Module):
          def __init__(self):
              super(PyTorchUncertainty, self).__init__()
              self.hidden = torch.nn.Linear(n_features, 200)
              self.output = torch.nn.Linear(200, n_features)
              self.log_var = torch.nn.Linear(200, n_features)
  
          def forward(self, inputs):
              x, use_dropout = inputs
              x = self.hidden(x)
              if use_dropout:
                  x = F.dropout(x, 0.1)
              output = self.output(x)
              log_var = self.log_var(x)
              var = torch.exp(log_var)
              return (output, var, output, log_var)
  
      def loss(outputs, labels, weights):
          diff = labels[0] - outputs[0]
          log_var = outputs[1]
          var = torch.exp(log_var)
          return torch.mean(diff * diff / var + log_var)
  
      class UncertaintyModel(dc.models.TorchModel):
          def default_generator(
              self,
              dataset,
              epochs=1,
              mode="fit",
              deterministic=True,
              pad_batches=True,
          ):
              for epoch in range(epochs):
                  for X_b, y_b, w_b, ids_b in dataset.iterbatches(
                      batch_size=self.batch_size,
                      deterministic=deterministic,
                      pad_batches=pad_batches,
                  ):
                      if mode == "predict":
                          dropout = np.array(False)
                      else:
                          dropout = np.array(True)
                      yield ([X_b, dropout], [y_b], [w_b])
  
      pytorch_model = PyTorchUncertainty()
      model = UncertaintyModel(
          pytorch_model,
          loss,
          output_types=["prediction", "variance", "loss", "loss"],
          learning_rate=0.003,
      )
  
      # Fit the model and see if its predictions are correct.
  
      model.fit(dataset, nb_epoch=2500)
      pred, std = model.predict_uncertainty(dataset)
      assert np.mean(np.abs(y - pred)) < 1.0
      assert noise < np.mean(std) < 1.0
  
file_path: deepchem/deepchem/models/tests/test_torch_model.py
imports: import os
  import pytest
  import deepchem
  import numpy
  import math
  import unittest
label_desc: Use image node_id 7 for calling a global function with example usage: test_uncertainty() and returns: torch, output,  var,  output,  log_var