### Install Skorch from Source using Pip Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/installation.rst Installs Skorch from its source repository using pip. This method involves cloning the repository and then installing Skorch. It provides options for basic installation or editable installation with development/testing dependencies. ```bash git clone https://github.com/skorch-dev/skorch.git cd skorch # create and activate a virtual environment # install pytorch version for your system (see below) python -m pip install . ``` ```bash git clone https://github.com/skorch-dev/skorch.git cd skorch # create and activate a virtual environment # install pytorch version for your system (see below) python -m pip install -e '.[test,docs,dev,extended]' py.test # unit tests pylint skorch # static code checks ``` -------------------------------- ### Install Dependencies Source: https://github.com/skorch-dev/skorch/blob/master/examples/optuna/README.md Installs all required Python packages for the example using pip. ```bash pip install -r requirements.txt ``` -------------------------------- ### Install PyTorch Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/installation.rst Installs PyTorch, a core dependency for Skorch. The specific PyTorch version depends on your OS and device. This command provides a general installation command. ```bash python -m pip install torch ``` -------------------------------- ### Install Skorch using Pip Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/installation.rst Installs the latest stable version of Skorch using pip. It is recommended to use a virtual environment for this installation. ```bash python -m pip install -U skorch ``` -------------------------------- ### Install Dependencies Source: https://github.com/skorch-dev/skorch/blob/master/examples/nuclei_image_segmentation/README.md Installs project dependencies using pip from a requirements file. This is a standard Python setup step. ```Python python -m pip install -r requirements.txt ``` -------------------------------- ### Install Skorch from Source using Conda Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/installation.rst Installs Skorch from its source repository using Conda. This method involves cloning the repository, creating a Conda environment, and then installing Skorch. It offers options for basic installation or installation with development/testing dependencies. ```bash git clone https://github.com/skorch-dev/skorch.git cd skorch conda create -n skorch-env python=3.10 conda activate skorch-env # install pytorch version for your system (see below) python -m pip install . ``` ```bash git clone https://github.com/skorch-dev/skorch.git cd skorch conda create -n skorch-env python==3.10 conda activate skorch-env # install pytorch version for your system (see below) python -m pip install '.[test,docs,dev,extended]' py.test # unit tests pylint skorch # static code checks ``` -------------------------------- ### Show Help for Skorch Example Source: https://github.com/skorch-dev/skorch/blob/master/examples/optuna/README.md Displays all available command-line arguments and options for the Skorch example script. ```bash python skorch_example.py --help ``` -------------------------------- ### Run Skorch Example Source: https://github.com/skorch-dev/skorch/blob/master/examples/optuna/README.md Executes the Skorch example script. The `--pruning` argument can be used to enable pruning during optimization. ```bash python skorch_example.py [--pruning] ``` -------------------------------- ### Install skorch from Source using Pip Source: https://github.com/skorch-dev/skorch/blob/master/README.rst Installs skorch from its source repository using pip. This involves cloning the repository, setting up a virtual environment, installing PyTorch, and then installing skorch. ```bash git clone https://github.com/skorch-dev/skorch.git cd skorch # create and activate a virtual environment # install pytorch version for your system (see below) python -m pip install . ``` -------------------------------- ### Install Fire and Numpydoc Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/helper.rst Installs the necessary libraries, Fire and Numpydoc, which are required for using Skorch's command-line argument parsing functionality. ```bash python -m pip install fire numpydoc ``` -------------------------------- ### Install Hugging Face Accelerate Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/huggingface.rst Provides the command to install the Hugging Face Accelerate library, which is required for using the `AccelerateMixin` in skorch. ```bash python -m pip install accelerate ``` -------------------------------- ### Install Skorch and Transformers Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/Hugging_Face_Finetuning.ipynb Installs the necessary skorch and Hugging Face transformers libraries. This command is typically run in a terminal or within a notebook environment. ```bash python -m pip install skorch transformers ``` -------------------------------- ### Install Accelerate Library Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/Hugging_Face_Finetuning.ipynb Installs or upgrades the 'accelerate' library, which is required for enabling automatic mixed precision (AMP) training. This command uses subprocess to run the pip installation. ```python import subprocess subprocess.run(['python', '-m', 'pip', 'install', 'accelerate>=0.11']) ``` -------------------------------- ### Get Skorch CLI Help Source: https://github.com/skorch-dev/skorch/blob/master/examples/image-classifier-finetuning/README.md Demonstrates how to access the command-line interface help for the training script. It shows how to get general help for the script and specific help for the 'net' component (e.g., NeuralNetClassifier parameters). ```bash # general help python train.py net -- --help # model specific help python train.py net --help ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/skorch-dev/skorch/blob/master/examples/image-classifier-finetuning/README.md Installs required Python packages including numpydoc for documentation parsing, Google Fire for CLI argument handling, and Hugging Face datasets for data loading. ```bash python -m pip install fire numpydoc datasets ``` -------------------------------- ### Install PyTorch Source: https://github.com/skorch-dev/skorch/blob/master/README.rst A general command to install PyTorch. The specific version needed depends on the user's OS and device, and should be verified on the official PyTorch website. ```bash python -m pip install torch ``` -------------------------------- ### Colab Installation Script Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/Hugging_Face_Finetuning.ipynb A Python script to install skorch and transformers specifically within a Google Colab environment. It checks for the presence of Colab and executes the installation if detected. ```python import subprocess # Installation on Google Colab try: import google.colab subprocess.run(['python', '-m', 'pip', 'install', 'skorch', 'transformers']) except ImportError: pass ``` -------------------------------- ### Run Jupyter Notebook Source: https://github.com/skorch-dev/skorch/blob/master/examples/nuclei_image_segmentation/README.md Starts a Jupyter Notebook server, allowing interactive exploration of the tutorial's code and data. ```Shell jupyter notebook ``` -------------------------------- ### Install skorch from Source using Conda Source: https://github.com/skorch-dev/skorch/blob/master/README.rst Installs skorch from its source repository using conda. This process involves cloning the repository, creating and activating a conda environment, installing PyTorch, and then installing skorch. ```bash git clone https://github.com/skorch-dev/skorch.git cd skorch conda create -n skorch-env python=3.12 conda activate skorch-env python -m pip install torch python -m pip install . ``` -------------------------------- ### Install Dependencies and Download Data (Colab) Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/Transfer_Learning.ipynb Installs skorch and torchvision, creates a datasets directory, downloads a zip file, and extracts it. This snippet is intended for Google Colab environments. ```python import subprocess # Installation on Google Colab try: import os import google.colab subprocess.run(['python', '-m', 'pip', 'install', 'skorch', 'torchvision']) subprocess.run(['mkdir', '-p', 'datasets']) subprocess.run(['wget', '-nc', '--no-check-certificate', 'https://download.pytorch.org/tutorial/hymenoptera_data.zip', '-P', 'datasets']) subprocess.run(['unzip', '-u', 'datasets/hymenoptera_data.zip', '-d' 'datasets']) except ImportError: pass ``` -------------------------------- ### Install skorch and torch_geometric Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/CORA-geometric.ipynb Installs necessary libraries (skorch, torch_geometric, torch-sparse, torch-scatter) for the example, particularly in Google Colab environments. It checks for Colab and installs specific versions compatible with the detected PyTorch version. ```python import subprocess # Installation on Google Colab try: import google.colab import torch subprocess.run(['python', '-m', 'pip', 'install', 'skorch' , 'torch_geometric']) subprocess.run(['python', '-m', 'pip', 'install', 'torch-sparse' , '-f', f'https://data.pyg.org/whl/torch-{torch.__version__}.html']) subprocess.run(['python', '-m', 'pip', 'install', 'torch-scatter' , '-f', f'https://data.pyg.org/whl/torch-{torch.__version__}.html']) except ImportError: pass ``` -------------------------------- ### Install GPyTorch Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/probabilistic.rst Instructions for installing the GPyTorch library, which is necessary for using skorch with Gaussian Processes. Supports both pip and conda package managers. ```bash # using pip python -m pip install -U gpytorch # using conda conda install gpytorch -c gpytorch ``` -------------------------------- ### Get Help for skorch CLI Source: https://github.com/skorch-dev/skorch/blob/master/examples/cli/README.md Illustrates how to retrieve help messages for a skorch-based CLI script. It shows commands to get general help and model-specific parameter help, distinguishing between net-only and pipeline configurations. ```bash # General help for net python train.py net -- --help # General help for pipeline python train.py pipeline -- --help # Model-specific help for net python train.py net --help # Model-specific help for pipeline python train.py pipeline --help ``` -------------------------------- ### Install skorch using Pip Source: https://github.com/skorch-dev/skorch/blob/master/README.rst Installs or upgrades the skorch library using pip. It is recommended to use a virtual environment for this installation. ```bash python -m pip install -U skorch ``` -------------------------------- ### Install Skorch and Hugging Face Hub on Colab Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/Hugging_Face_Model_Checkpoint.ipynb Installs necessary libraries (skorch, huggingface_hub, transformers) for Hugging Face Hub integration within a Google Colab environment. It checks for the Colab environment before attempting installation. ```python import subprocess # Installation on Google Colab try: import google.colab subprocess.run(['python', '-m', 'pip', 'install', 'skorch' , 'huggingface_hub', 'transformers']) except ImportError: pass ``` -------------------------------- ### Install skorch from Source with Development Dependencies using Pip Source: https://github.com/skorch-dev/skorch/blob/master/README.rst Installs skorch from source with additional development dependencies (test, docs, dev, extended) using pip. Includes commands for running unit tests and static code checks. ```bash git clone https://github.com/skorch-dev/skorch.git cd skorch # create and activate a virtual environment # install pytorch version for your system (see below) python -m pip install -e '.[test,docs,dev,extended]' py.test # unit tests pylint skorch # static code checks ``` -------------------------------- ### Start New Experiment with Checkpoint and LoadInitState Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/save_load.rst Shows how to start a new experiment with a different directory and potentially modified model parameters. A new `Checkpoint` is created, and `LoadInitState` is initialized with it. The `NeuralNetClassifier` is configured with these callbacks and updated module parameters. ```python cp = Checkpoint(dirname='exp2') load_state = LoadInitState(cp) net = NeuralNetClassifier( MyModule, lr=0.5, callbacks=[cp, load_state], module__num_units=20, ) _ = net.fit(X, y) ``` -------------------------------- ### Install Safetensors Library Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/save_load.rst Command to install the safetensors library, which is recommended for secure and efficient tensor serialization with skorch. ```bash python -m pip install safetensors ``` -------------------------------- ### Install skorch CLI Dependencies Source: https://github.com/skorch-dev/skorch/blob/master/examples/cli/README.md Installs the necessary libraries, fire and numpydoc, required for creating command-line interfaces with skorch. These are external dependencies not included with skorch itself. ```bash python -m pip install fire numpydoc ``` -------------------------------- ### Install Hugging Face Transformers Library Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/LLM.rst Command to install the Hugging Face transformers library, which is a prerequisite for using skorch's LLM integrations like ZeroShotClassifier. ```bash python -m pip install transformers ``` -------------------------------- ### FewShotClassifier Example Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/LLM.rst Illustrates the usage of Skorch's FewShotClassifier for improved LLM performance by providing a few examples. It shows how to initialize the classifier with a model name and max_samples, and then fit and predict. ```python from skorch.llm import FewShotClassifier from sklearn.metrics import accuracy_score X_train, y_train, X_test, y_test = ... # your data clf = FewShotClassifier('bigscience/bloomz-1b1', max_samples=5) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) accuracy_score(y_test, y_pred) ``` -------------------------------- ### skorch: Example of Custom Module Initialization Source: https://github.com/skorch-dev/skorch/blob/master/CHANGES.md Shows how to add a custom PyTorch module to a skorch `NeuralNet` and have it managed automatically. ```Python import torch import torch.nn as nn from skorch import NeuralNetClassifier class CustomLayer(nn.Module): def __init__(self, in_features, out_features): super().__init__() self.linear = nn.Linear(in_features, out_features) def forward(self, x): return self.linear(x) # Initialize NeuralNet with a custom module custom_module = CustomLayer(10, 5) net = NeuralNetClassifier( module=custom_module, # Custom module passed directly criterion=nn.CrossEntropyLoss, optimizer=torch.optim.Adam, max_epochs=10, lr=0.01, device='cuda' if torch.cuda.is_available() else 'cpu' ) # Skorch will automatically set mode, move to device, and update parameters # net.fit(X_train, y_train) ``` -------------------------------- ### skorch: Example Usage of train_step_single Source: https://github.com/skorch-dev/skorch/blob/master/CHANGES.md Illustrates the usage of `train_step_single` with the new signature accepting the entire batch. ```Python import torch import torch.nn as nn from skorch import NeuralNetClassifier class MyNet(NeuralNetClassifier): def train_step_single(self, batch): # batch is now a tuple or list, e.g., (X, y) X, y = batch self.optimizer_.zero_grad() y_hat = self.forward(X) loss = self.criterion_(y_hat, y) loss.backward() self.optimizer_.step() return loss # Example usage with a DataLoader that yields (X, y) tuples # dataloader = DataLoader(dataset, batch_size=32) # net = MyNet(module=MyModel(), ...) # net.fit(dataloader, ...) ``` -------------------------------- ### Import Sklearn Pipeline Components Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/Basic_Usage.ipynb Imports necessary classes from scikit-learn for building pipelines: `Pipeline` to chain estimators and `StandardScaler` for data preprocessing. ```python from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler ``` -------------------------------- ### Install skorch from Source with Development Dependencies using Conda Source: https://github.com/skorch-dev/skorch/blob/master/README.rst Installs skorch from source with additional development dependencies (test, docs, dev, extended) using conda. Includes commands for running unit tests and static code checks. ```bash git clone https://github.com/skorch-dev/skorch.git cd skorch conda create -n skorch-env python=3.12 conda activate skorch-env python -m pip install torch python -m pip install '.[test,docs,dev,extended]' py.test # unit tests pylint skorch # static code checks ``` -------------------------------- ### Access skorch Prefixes Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/Basic_Usage.ipynb Prints the list of special parameter prefixes recognized by skorch, such as 'module__' and 'optimizer__'. These prefixes allow access to nested parameters for configuration and tuning. ```python print(', '.join(net.prefixes_)) ``` -------------------------------- ### Build Skorch Documentation Locally Source: https://github.com/skorch-dev/skorch/blob/master/docs/README.md These commands demonstrate the process of building the Skorch project's documentation locally using the make utility. It includes steps to generate HTML output and then open the main index file in the Firefox browser. ```bash make html ``` ```bash firefox _build/html/index.html ``` -------------------------------- ### Install Skorch and PyTorch on Colab Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/Basic_Usage.ipynb Installs the skorch and torch libraries using pip within a Google Colab environment. This snippet checks if the code is running in Colab before attempting the installation. ```python import subprocess # Installation on Google Colab try: import google.colab subprocess.run(['python', '-m', 'pip', 'install', 'skorch' , 'torch']) except ImportError: pass ``` -------------------------------- ### Skorch Integration Example Source: https://github.com/skorch-dev/skorch/blob/master/examples/word_language_model/data/penn/test.txt This snippet demonstrates a basic integration of Skorch with PyTorch, showing how to initialize a PyTorch module and wrap it with a Skorch NeuralNet classifier. It highlights the core usage pattern for training and prediction within the Skorch framework. ```python import torch import torch.nn as nn from skorch import NeuralNetClassifier # Define a simple PyTorch module class MyNet(nn.Module): def __init__(self, num_hidden=10): super().__init__() self.fc1 = nn.Linear(10, num_hidden) self.relu = nn.ReLU() self.fc2 = nn.Linear(num_hidden, 2) def forward(self, x): x = self.fc1(x) x = self.relu(x) x = self.fc2(x) return x # Initialize the Skorch classifier with the PyTorch module net = NeuralNetClassifier( MyNet, max_epochs=10, lr=0.01, # Other Skorch parameters can be set here ) # Example usage (assuming X_train and y_train are defined) # net.fit(X_train, y_train) # y_pred = net.predict(X_test) ``` -------------------------------- ### Install Dependencies Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/LLM_Classifier.ipynb Installs necessary libraries like skorch, transformers, and datasets, specifically for Google Colab environments. This ensures the required tools are available for running the LLM classification examples. ```python import subprocess # Installation on Google Colab try: import google.colab subprocess.run(['python', '-m', 'pip', 'install', 'skorch', 'transformers', 'datasets']) except ImportError: pass ``` -------------------------------- ### Install skorch and PyTorch Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/Advanced_Usage.ipynb Installs the skorch and torch libraries using pip. This snippet is designed to run specifically in a Google Colab environment, checking for its presence before attempting installation. ```python import subprocess # Installation on Google Colab try: import google.colab subprocess.run(['python', '-m', 'pip', 'install', 'skorch' , 'torch']) except ImportError: pass ``` -------------------------------- ### Run Skorch Model Training Source: https://github.com/skorch-dev/skorch/blob/master/examples/image-classifier-finetuning/README.md Provides examples for running the training script. The first command trains with default settings. The second command shows how to override default parameters like epochs, batch size, device, verbosity, and output file using the '--net__' prefix for skorch parameters. ```bash # train default model python train.py net # train with some non-defaults python train.py net --net__max_epochs=10 --net__batch_size=32 --device=cpu --net__verbose=False --output_file=mymodel.pkl ``` -------------------------------- ### Install Dask and Distributed Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/parallelism.rst Installs the necessary Dask libraries for distributed computing. This is a prerequisite for using Dask with Skorch. ```shell python -m pip install dask distributed ``` -------------------------------- ### Train skorch Model via CLI Source: https://github.com/skorch-dev/skorch/blob/master/examples/cli/README.md Provides examples of running a skorch CLI script to train a model. It covers running with default parameters, and then demonstrates how to override parameters like learning rate, epochs, device, and module-specific settings via the command line. ```bash # Run with defaults python train.py net python train.py pipeline # Example with non-defaults for net python train.py net --n_samples 1000 --output_file 'model.pkl' --lr 0.1 --max_epochs 5 --device 'cuda' --module__hidden_units 50 --module__nonlin 'torch.nn.RReLU(0.1, upper=0.4)' --callbacks__valid_acc__on_train --callbacks__valid_acc__name train_acc # Example with non-defaults for pipeline python train.py pipeline --n_samples 1000 --net__lr 0.1 --net__module__nonlin 'torch.nn.LeakyReLU()' --scale__minmax__feature_range '(-2, 2)' --scale__normalize__norm l1 ``` -------------------------------- ### Basic Skorch CLI Usage with Fire Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/helper.rst Demonstrates the fundamental structure for a Skorch script using Fire. It shows how to import necessary modules, define a main function that parses arguments, wraps the model, and fits it to data. ```python import fire from skorch.helper import parse_args # your model definition and data fetching code below ... def main(**kwargs): X, y = get_data() my_model = get_model() # important: wrap the model with the parsed arguments parsed = parse_args(kwargs) my_model = parsed(my_model) my_model.fit(X, y) if __name__ == '__main__': fire.Fire(main) ``` -------------------------------- ### Basic skorch CLI Structure with Fire Source: https://github.com/skorch-dev/skorch/blob/master/examples/cli/README.md Demonstrates the fundamental structure of a Python script using skorch and fire to create a CLI. It shows how to import necessary modules, define a main function that parses arguments using skorch's `parse_args`, and integrates with fire for command-line execution. ```python import fire from skorch.helper import parse_args # your model definition and data fetching code below ... def main(**kwargs): X, y = get_data() my_model = get_model() # important: wrap the model with the parsed arguments parsed = parse_args(kwargs) my_model = parsed(my_model) my_model.fit(X, y) if __name__ == '__main__': fire.Fire(main) ``` -------------------------------- ### Install skorch and PyTorch on Colab Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/MNIST.ipynb Installs necessary libraries (skorch, torch) for running the notebook in a Google Colab environment using subprocess. ```python import subprocess # Installation on Google Colab try: import google.colab subprocess.run(['python', '-m', 'pip', 'install', 'skorch' , 'torch']) except ImportError: pass ``` -------------------------------- ### skorch FAQ entry Source: https://github.com/skorch-dev/skorch/blob/master/CHANGES.md Mentions the addition of an FAQ entry addressing the initialization behavior of `NeuralNet` when instantiated models are passed as arguments. ```APIDOC Documentation: FAQ: - Added an entry explaining the initialization behavior of `NeuralNet` when passed instantiated models. ``` -------------------------------- ### Skorch Model Initialization and Optimizer Setup Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/customization.rst Illustrates the internal logic for initializing custom criteria and optimizers within a Skorch model. It shows how to retrieve parameters for specific components and assign them to PyTorch optimizers. ```python def initialize_criterion(self): params = self.get_params_for('other_criterion') self.other_criterion_ = nn.BCELoss(**params) return self def initialize_optimizer(self): # first initialize the normal optimizer named_params = self.module_.named_parameters() args, kwargs = self.get_params_for_optimizer('optimizer', named_params) self.optimizer_ = self.optimizer(*args, **kwargs) # next add an another optimizer called 'optimizer2_' that is # only responsible for training 'module2_' named_params = self.module2_.named_parameters() args, kwargs = self.get_params_for_optimizer('optimizer2', named_params) self.optimizer2_ = torch.optim.SGD(*args, **kwargs) return self ``` -------------------------------- ### Hyperparameter Tuning with skorch and GridSearchCV Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/quickstart.rst Illustrates performing hyperparameter optimization for a skorch model using sklearn.model_selection.GridSearchCV. It details setting parameters like learning rate and module units, and running the search. ```python from sklearn.model_selection import GridSearchCV # Assuming 'net' is a NeuralNetClassifier instance # X, y are also defined # deactivate skorch-internal train-valid split and verbose logging net.set_params(train_split=False, verbose=0) params = { 'lr': [0.01, 0.02], 'max_epochs': [10, 20], 'module__num_units': [10, 20], } gs = GridSearchCV(net, params, refit=False, cv=3, scoring='accuracy') gs.fit(X, y) print(gs.best_score_, gs.best_params_) ``` -------------------------------- ### Deploy to Staging and Live PyPI Source: https://github.com/skorch-dev/skorch/wiki/Release Executes deployment scripts to upload the package to PyPI's testing environment (stage) and then to the live PyPI. Requires a specific PyTorch version to be set. ```shell PYTORCH_VERSION=1.8.1 ./scripts/deploy.sh stage PYTORCH_VERSION=1.8.1 ./scripts/deploy.sh live ``` -------------------------------- ### NeuralNet Initialization with Module Arguments Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/neuralnet.rst Shows how to pass arguments to a PyTorch module via the NeuralNet constructor using the 'module__' prefix for configuration. ```python class MyModule(torch.nn.Module): def __init__(self, num_units, dropout): ... net = NeuralNet( module=MyModule, module__num_units=100, module__dropout=0.5, criterion=torch.nn.NLLLoss, ) ``` -------------------------------- ### Palladium Web Service Response Example Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/REST.rst Example JSON response from a Palladium prediction service. It includes metadata and the prediction results, typically probabilities for classification tasks. ```json { "metadata": {"error_code": 0, "status": "OK"}, "result": [0.326442807912827, 0.673557221889496] } ``` -------------------------------- ### TensorBoard Logging Setup Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/MNIST-torchvision.ipynb Configures TensorBoard logging for skorch callbacks. This involves importing necessary classes from PyTorch and skorch, creating a SummaryWriter instance, and adding the TensorBoard callback to the list of callbacks used during model initialization. It also provides instructions on how to run TensorBoard from the command line. ```python # To enable TensorBoard logging, ensure 'tensorboard', 'future', 'pillow' are installed. # Run '$ tensorboard --logdir runs' in your project directory to view logs. # Assuming USE_TENSORBOARD is a boolean flag callbacks = [] if USE_TENSORBOARD: from torch.utils.tensorboard import SummaryWriter from skorch.callbacks import TensorBoard # The SummaryWriter will log to a 'runs' directory by default writer = SummaryWriter() callbacks.append(TensorBoard(writer)) # These callbacks are then passed to NeuralNetClassifier: callbacks=callbacks ``` -------------------------------- ### Initialize Accelerator for AMP Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/Hugging_Face_Finetuning.ipynb Creates an instance of the 'Accelerator' class, configuring it for mixed precision training using 'fp16' (16-bit floating-point). This object will manage the AMP process during training. ```python accelerator = Accelerator(mixed_precision='fp16') ``` -------------------------------- ### Install skorch, torch, and torchvision on Colab Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/MNIST-torchvision.ipynb Installs necessary libraries for running the notebook in a Google Colab environment using pip. This ensures all required packages are available before proceeding with the training process. ```python import subprocess # Installation on Google Colab try: import google.colab subprocess.run(['python', '-m', 'pip', 'install', 'skorch' , 'torch', 'torchvision']) except ImportError: pass ``` -------------------------------- ### Install skorch using Conda Source: https://github.com/skorch-dev/skorch/blob/master/README.rst Installs the skorch library using the conda-forge channel. It is recommended to use a conda virtual environment for managing dependencies. ```bash conda install -c conda-forge skorch ``` -------------------------------- ### Initialize DistributedHistory with TCPStore Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/history.rst Provides a Python code example for initializing the `DistributedHistory` class, which is necessary for distributed training to synchronize history data across processes. It shows the import and initialization with `TCPStore`. ```python from torch.distributed import TCPStore # Example initialization for DistributedHistory # store = TCPStore(store_name, rank, world_size) # history = DistributedHistory(store, rank, world_size) ``` -------------------------------- ### Skorch CLI Configuration Options Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/helper.rst Details command-line interface options for configuring skorch components, automatically generated from docstrings. This facilitates reproducible experiments by exposing skorch and PyTorch module parameters directly via the CLI. ```APIDOC Skorch CLI Configuration: This section outlines the command-line arguments available for configuring skorch models and their underlying PyTorch components. These options are typically accessed via a `--help` flag on a skorch-based script. options: --select__score_func : callable Function taking two arrays X and y, and returning a pair of arrays (scores, pvalues) or a single array with scores. Default is f_classif. The default function only works with classification tasks. --select__k : int or "all", optional, default=10 Number of top features to select. The "all" option bypasses selection, for use in a parameter search. options: --net__module : torch module (class or instance) A PyTorch \class:`~torch.nn.Module`. In general, the uninstantiated class should be passed, although instantiated modules will also work. --net__criterion : torch criterion (class, default=torch.nn.NLLLoss) Negative log likelihood loss. Note that the module should return probabilities, the log is applied during `get_loss`. --net__optimizer : torch optim (class, default=torch.optim.SGD) The uninitialized optimizer (update rule) used to optimize the module. --net__lr : float (default=0.01) Learning rate passed to the optimizer. You may use `lr` instead of using `optimizer__lr`, which would result in the same outcome. --net__max_epochs : int (default=10) The number of epochs to train for each `fit` call. Note that you may keyboard-interrupt training at any time. --net__batch_size : int (default=128) The batch size for training. --net__verbose : int (default=1) Control the verbosity level. --net__device : str, torch.device (default='cpu') The compute device to be used. If set to 'cuda', data in torch tensors will be pushed to cuda tensors before being sent to the module. options: --net__module__hidden_units : int (default=10) Number of units in hidden layers. --net__module__num_hidden : int (default=1) Number of hidden layers. --net__module__nonlin : torch.nn.Module instance (default=torch.nn.ReLU()) Non-linearity to apply after hidden layers. --net__module__dropout : float (default=0) Dropout rate. Dropout is applied between layers. ``` -------------------------------- ### Create Hugging Face Hub Repository Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/Hugging_Face_Model_Checkpoint.ipynb Creates a new repository on the Hugging Face Hub using the `create_repo` function from the `huggingface_hub` library. It allows specifying the repository name, privacy, and token, with an option to exist_ok. ```python skorch_repo = create_repo( REPO_NAME, private=True, # set to False if it should be public token=TOKEN, exist_ok=True, ) skorch_repo ``` -------------------------------- ### Create Skorch Pipeline with AcceleratedNet Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/Hugging_Face_Finetuning.ipynb Sets up an scikit-learn Pipeline using the custom 'AcceleratedNet' class for AMP compatibility. The 'accelerator' object is passed to the 'net' component, and the 'device' parameter is commented out as 'accelerate' handles device placement. ```python pipeline2 = Pipeline([ ('tokenizer', HuggingfacePretrainedTokenizer(TOKENIZER)), ('net', AcceleratedNet( BertModule, accelerator=accelerator, module__name=PRETRAINED_MODEL, module__num_labels=len(set(y_train)), optimizer=OPTMIZER, lr=LR, max_epochs=MAX_EPOCHS, criterion=CRITERION, batch_size=BATCH_SIZE, iterator_train__shuffle=True, # device=DEVICE, # <= changed callbacks=[ LRScheduler(LambdaLR, lr_lambda=lr_schedule, step_every='batch'), ProgressBar(), ], )), ]) ``` -------------------------------- ### Get Train Data Shapes Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/MNIST.ipynb Displays the shapes of the training features and labels. ```python X_train.shape, y_train.shape ``` -------------------------------- ### Get MNIST Data Shape Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/MNIST.ipynb Displays the shape of the loaded MNIST dataset features. ```python mnist.data.shape ``` -------------------------------- ### Display Sample Training Data Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/Hugging_Face_Finetuning.ipynb Shows the first two samples of the training data (X_train) to provide a glimpse of the text content being used for model training. ```python print(X_train[:2]) ``` -------------------------------- ### Download and Prepare Data Source: https://github.com/skorch-dev/skorch/blob/master/examples/nuclei_image_segmentation/README.md Executes a shell script to download, extract, and prepare the necessary data for the image segmentation task. This script is crucial for having the dataset ready. ```Shell ./dl_extract_prepare.sh ``` -------------------------------- ### Distributed Training with TCPStore Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/history.rst This snippet demonstrates setting up distributed training in Skorch using TCPStore for communication and DistributedHistory for tracking training progress across processes. It shows the initialization of a store, history object, and a Skorch classifier for fitting. ```python from torch.nn.parallel import DistributedDataParallel from skorch.net import NeuralNetClassifier from skorch.history import DistributedHistory from torch.distributed import TCPStore def train(rank, world_size, is_master): store = TCPStore( "127.0.0.1", port=1234, world_size=world_size) dist_history = DistributedHistory( store=store, rank=rank, world_size=world_size) net = NeuralNetClassifier(..., history=dist_history) net.fit(X, y) ``` -------------------------------- ### skorch: Example Usage of validation_step Source: https://github.com/skorch-dev/skorch/blob/master/CHANGES.md Demonstrates how to use the updated `validation_step` signature which receives the entire batch. ```Python import torch import torch.nn as nn from skorch import NeuralNetClassifier class MyNet(NeuralNetClassifier): def validation_step(self, batch, batch_idx): # batch is now a tuple or list, e.g., (X, y) X, y = batch y_hat = self.forward(X) loss = self.criterion_(y_hat, y) return loss # Example usage with a DataLoader that yields (X, y) tuples # dataloader = DataLoader(dataset, batch_size=32) # net = MyNet(module=MyModel(), ...) # net.fit(dataloader, ...) ``` -------------------------------- ### skorch MANIFEST.in inclusion Source: https://github.com/skorch-dev/skorch/blob/master/CHANGES.md Ensures that requirements are correctly included in the `MANIFEST.in` file, which is crucial for package distribution and installation. ```APIDOC Packaging: MANIFEST.in: - Included requirements in `MANIFEST.in` for proper package distribution. ``` -------------------------------- ### Train PyTorch Model with skorch NeuralNetClassifier Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/quickstart.rst Demonstrates training a custom PyTorch nn.Module using skorch.NeuralNetClassifier. It covers data preparation, model definition, and fitting the classifier with specified parameters like learning rate and criterion. ```python import numpy as np from sklearn.datasets import make_classification from torch import nn from skorch import NeuralNetClassifier X, y = make_classification(1000, 20, n_informative=10, random_state=0) X = X.astype(np.float32) y = y.astype(np.int64) class MyModule(nn.Module): def __init__(self, num_units=10, nonlin=nn.ReLU()): super().__init__() self.dense0 = nn.Linear(20, num_units) self.nonlin = nonlin self.dropout = nn.Dropout(0.5) self.dense1 = nn.Linear(num_units, num_units) self.output = nn.Linear(num_units, 2) def forward(self, X, **kwargs): X = self.nonlin(self.dense0(X)) X = self.dropout(X) X = self.nonlin(self.dense1(X)) X = self.output(X) return X net = NeuralNetClassifier( MyModule, max_epochs=10, criterion=nn.CrossEntropyLoss(), lr=0.1, # Shuffle training data on each epoch iterator_train__shuffle=True, ) net.fit(X, y) y_proba = net.predict_proba(X) ``` -------------------------------- ### Skorch: Resume Training with LoadInitState Callback Source: https://github.com/skorch-dev/skorch/blob/master/docs/user/save_load.rst Shows how to resume training from a previously saved checkpoint using the `LoadInitState` callback. This callback initializes model, history, and optimizer parameters from a specified checkpoint directory at the beginning of training, allowing continuation from a saved epoch. ```python from skorch.callbacks import LoadInitState cp = Checkpoint(dirname='exp1') load_state = LoadInitState(cp) net = NeuralNetClassifier( MyModule, lr=0.1, callbacks=[cp, load_state] ) _ = net.fit(X, y) ``` -------------------------------- ### skorch: Using loss_scoring for Custom Metrics Source: https://github.com/skorch-dev/skorch/blob/master/CHANGES.md Example of using `scoring.loss_scoring` to calculate the network's loss as a metric. ```Python import torch import torch.nn as nn from skorch import NeuralNetClassifier from skorch import scoring class MyModel(nn.Module): def __init__(self): super().__init__() self.fc = nn.Linear(10, 2) def forward(self, x): return self.fc(x) net = NeuralNetClassifier( module=MyModel(), criterion=nn.CrossEntropyLoss, optimizer=torch.optim.Adam, max_epochs=5, lr=0.01, # Use loss_scoring as a custom scoring function scoring=scoring.loss_scoring ) # When net.score(X_test, y_test) is called, it will compute the loss # net.fit(X_train, y_train) # score = net.score(X_test, y_test) # print(f'Test loss: {score}') ``` -------------------------------- ### Plot Example Training Images Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/MNIST.ipynb Uses the `plot_example` function to display the first 5 training images and their labels. ```python plot_example(X_train, y_train) ``` -------------------------------- ### Skorch CLI and NeuralNetClassifier Parameters Source: https://github.com/skorch-dev/skorch/blob/master/examples/image-classifier-finetuning/README.md Documents the command-line interface of the `train.py` script, which integrates Hugging Face datasets and models with skorch. It highlights how skorch's CLI helper automatically exposes `NeuralNetClassifier` parameters via a `net__` prefix, allowing direct configuration from the command line. This includes parameters like `max_epochs`, `batch_size`, `verbose`, and `device`. ```APIDOC Skorch CLI Integration: The `train.py` script utilizes skorch's CLI helper to automatically expose model parameters. Usage Examples: - General Help: `python train.py net -- --help` - Model Specific Help: `python train.py net --help` Parameter Configuration: Parameters for `NeuralNetClassifier` (or other skorch wrappers) are accessed using the `net__` prefix. - `net__max_epochs=`: Sets the maximum number of training epochs. - `net__batch_size=`: Sets the batch size for training. - `device=`: Specifies the computation device (e.g., 'cpu', 'cuda'). - `net__verbose=`: Controls the verbosity of the training output (e.g., `False` to disable). - `output_file=`: Specifies the path to save the trained model artifact. ``` -------------------------------- ### Import Sklearn Datasets Source: https://github.com/skorch-dev/skorch/blob/master/notebooks/Basic_Usage.ipynb Imports the make_classification function from scikit-learn to generate synthetic datasets for classification tasks. ```python import numpy as np from sklearn.datasets import make_classification ```