code: def sklearn_train_linear_model(
      model: LinearModel,
      dataloader: DataLoader,
      construct_kwargs: Dict[str, Any],
      sklearn_trainer: str = "Lasso",
      norm_input: bool = False,
      **fit_kwargs,
  ):
      r"""
      Alternative method to train with sklearn. This does introduce some slight
      overhead as we convert the tensors to numpy and then convert the resulting
      trained model to a `LinearModel` object. However, this conversion
      should be negligible.
  
      Please note that this assumes:
  
      0. You have sklearn and numpy installed
      1. The dataset can fit into memory
  
      Args
          model
              The model to train.
          dataloader
              The data to use. This will be exhausted and converted to numpy
              arrays. Therefore please do not feed an infinite dataloader.
          norm_input
              Whether or not to normalize the input
          sklearn_trainer
              The sklearn model to use to train the model. Please refer to
              sklearn.linear_model for a list of modules to use.
          construct_kwargs
              Additional arguments provided to the `sklearn_trainer` constructor
          fit_kwargs
              Other arguments to send to `sklearn_trainer`'s `.fit` method
      """
      from functools import reduce
  
      try:
          import numpy as np
      except ImportError:
          raise ValueError(
              "numpy is not available. Please install numpy."
          )
  
      try:
          import sklearn
          import sklearn.linear_model
          import sklearn.svm
      except ImportError:
          raise ValueError(
              "sklearn is not available. Please install sklearn >= 0.23"
          )
  
      if not sklearn.__version__ >= "0.23.0":
          warnings.warn(
              "Must have sklearn version 0.23.0 or higher to use "
              "sample_weight in Lasso regression."
          )
  
      num_batches = 0
      xs, ys, ws = [], [], []
      for data in dataloader:
          if len(data) == 3:
              x, y, w = data
          else:
              assert len(data) == 2
              x, y = data
              w = None
  
          xs.append(x.cpu().numpy())
          ys.append(y.cpu().numpy())
          if w is not None:
              ws.append(w.cpu().numpy())
          num_batches += 1
  
      x = np.concatenate(xs, axis=0)
      y = np.concatenate(ys, axis=0)
      if len(ws) > 0:
          w = np.concatenate(ws, axis=0)
      else:
          w = None
  
      if norm_input:
          mean, std = x.mean(0), x.std(0)
          x -= mean
          x /= std
  
      t1 = time.time()
      sklearn_model = reduce(
          lambda val, el: getattr(val, el),
          [sklearn] + sklearn_trainer.split("."),
      )(**construct_kwargs)
      try:
          sklearn_model.fit(x, y, sample_weight=w, **fit_kwargs)
      except TypeError:
          sklearn_model.fit(x, y, **fit_kwargs)
          warnings.warn(
              "Sample weight is not supported for the provided linear model!"
              " Trained model without weighting inputs. For Lasso, please"
              " upgrade sklearn to a version >= 0.23.0."
          )
  
      t2 = time.time()
  
      # Convert weights to pytorch
      classes = (
          torch.IntTensor(sklearn_model.classes_)
          if hasattr(sklearn_model, "classes_")
          else None
      )
  
      # extract model device
      device = getattr(model, "device", "cpu")
  
      num_outputs = (
          sklearn_model.coef_.shape[0]
          if sklearn_model.coef_.ndim > 1
          else 1
      )
      weight_values = torch.FloatTensor(sklearn_model.coef_).to(device)  # type: ignore
      bias_values = torch.FloatTensor([sklearn_model.intercept_]).to(  # type: ignore
          device  # type: ignore
      )  # type: ignore
      model._construct_model_params(
          norm_type=None,
          weight_values=weight_values.view(num_outputs, -1),
          bias_value=bias_values.squeeze().unsqueeze(0),
          classes=classes,
      )
  
      if norm_input:
          model.norm = NormLayer(mean, std)
  
      return {"train_time": t2 - t1}
  
file_path: captum/captum/_utils/models/linear_model/train.py
imports: import time
  import warnings
  from typing import Any, Callable, Dict, List, Optional
  import torch
  import torch.nn
  from captum._utils.models.linear_model.model import LinearModel
  from torch.utils.data import DataLoader
label_desc: Use image node_id 3 for calling a global function with example usage: sklearn_train_linear_model(model, dataloader, construct_kwargs, sklearn_trainer, norm_input) and returns: dict