Commit ede8600f authored by nd-02110114's avatar nd-02110114
Browse files

♻️ refactor wandb import

parent e9226da3
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
"""
Callback functions that can be invoked while fitting a KerasModel.
"""

import tensorflow as tf
import sys

from deepchem.models.keras_model import is_wandb_available
if is_wandb_available():
  import wandb


class ValidationCallback(object):
  """Performs validation while training a KerasModel.
@@ -86,6 +80,7 @@ class ValidationCallback(object):
      for key in scores:
        model._log_value_to_tensorboard(tag=key, simple_value=scores[key])
    if model.wandb:
      import wandb
      wandb.log(scores, step=step)
    if self.save_dir is not None:
      score = scores[self.metrics[self.save_metric].name]
+9 −13
Original line number Diff line number Diff line
@@ -8,8 +8,6 @@ try:
except:
  from collections import Sequence as SequenceCollection

logger = logging.getLogger(__name__)

from deepchem.data import Dataset, NumpyDataset
from deepchem.metrics import Metric
from deepchem.models.losses import Loss
@@ -34,9 +32,7 @@ try:
except (ImportError, AttributeError):
  _has_wandb = False


def is_wandb_available():
  return _has_wandb
logger = logging.getLogger(__name__)


class KerasModel(Model):
@@ -188,12 +184,12 @@ class KerasModel(Model):
    self.tensorboard = tensorboard

    # W&B logging
    if wandb and not is_wandb_available():
    if wandb and not _has_wandb:
      logger.warning(
          "You set wandb to True but W&B is not installed. To use wandb logging, "
          "run `pip install wandb; wandb login` see https://docs.wandb.com/huggingface."
      )
    self.wandb = wandb and is_wandb_available()
    self.wandb = wandb and _has_wandb

    # Backwards compatibility
    if "tensorboard_log_frequency" in kwargs:
@@ -381,7 +377,6 @@ class KerasModel(Model):
    avg_loss = 0.0
    last_avg_loss = 0.0
    averaged_batches = 0
    train_op = None
    if loss is None:
      loss = self._loss_fn
    var_key = None
@@ -574,12 +569,12 @@ class KerasModel(Model):
    variances: Optional[List[np.ndarray]] = None
    if (outputs is not None) and (other_output_types is not None):
      raise ValueError(
          'This model cannot compute outputs and other output_types simultaneously. Please invoke one at a time.'
      )
          'This model cannot compute outputs and other output_types simultaneously.'
          'Please invoke one at a time.')
    if uncertainty and (other_output_types is not None):
      raise ValueError(
          'This model cannot compute uncertainties and other output types simultaneously. Please invoke one at a time.'
      )
          'This model cannot compute uncertainties and other output types simultaneously.'
          'Please invoke one at a time.')
    if uncertainty:
      assert outputs is None
      if self._variance_outputs is None or len(self._variance_outputs) == 0:
@@ -596,7 +591,8 @@ class KerasModel(Model):
    if (outputs is not None and self.model.inputs is not None and
        len(self.model.inputs) == 0):
      raise ValueError(
          "Cannot use 'outputs' argument with a model that does not specify its inputs. Note models defined in imperative subclassing style cannot specify outputs"
          "Cannot use 'outputs' argument with a model that does not specify its inputs."
          "Note models defined in imperative subclassing style cannot specify outputs"
      )
    if isinstance(outputs, tf.Tensor):
      outputs = [outputs]
+3 −8
Original line number Diff line number Diff line
import numpy as np
import torch
import torch.utils.tensorboard
import time
import logging
import os
@@ -9,8 +8,6 @@ try:
except:
  from collections import Sequence as SequenceCollection

logger = logging.getLogger(__name__)

from deepchem.data import Dataset, NumpyDataset
from deepchem.metrics import Metric
from deepchem.models.losses import Loss
@@ -35,9 +32,7 @@ try:
except (ImportError, AttributeError):
  _has_wandb = False


def is_wandb_available():
  return _has_wandb
logger = logging.getLogger(__name__)


class TorchModel(Model):
@@ -186,12 +181,12 @@ class TorchModel(Model):
    self.model.to(device)

    # W&B logging
    if wandb and not is_wandb_available():
    if wandb and not _has_wandb:
      logger.warning(
          "You set wandb to True but W&B is not installed. To use wandb logging, "
          "run `pip install wandb; wandb login` see https://docs.wandb.com/huggingface."
      )
    self.wandb = wandb and is_wandb_available()
    self.wandb = wandb and _has_wandb

    self.log_frequency = log_frequency
    if self.tensorboard: