### Run minGPT-rs Example Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/min-gpt/README.md Use this command to compile and run the minGPT-rs character-level language model example. Ensure your training text file is located at `data/input.txt`. ```bash cargo run --example min-gpt ``` -------------------------------- ### Run JIT Example with Cargo Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit/README.md Executes the compiled Rust example with a model file and image file as command-line arguments. ```bash cargo run --example jit model.pt image.jpg ``` -------------------------------- ### Run transfer learning example with dataset and weights Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/transfer-learning/README.md Execute the transfer learning example, providing the path to the pretrained ResNet-18 weights and the extracted dataset directory as arguments. ```bash cargo run --example transfer-learning resnet18.ot hymenoptera_data ``` -------------------------------- ### Run Policy Gradient Example with Cargo Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/reinforcement-learning/README.md Execute the policy gradient reinforcement learning example on CartPole-v0 environment. Requires the python feature flag and OpenAI Gym Python package installed. ```bash cargo run --example reinforcement-learning --features=python pg ``` -------------------------------- ### Install tch-rs and run tests Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/transfer-learning/README.md Use these commands to clone the tch-rs repository, navigate into it, and run the tests. This process also installs the CPU version of libtorch if required. ```bash git clone https://github.com/LaurentMazare/tch-rs.git cd tch-rs cargo test ``` -------------------------------- ### Run Character-level RNN Example Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/char-rnn/README.md Use this command to compile and execute the character-level RNN example. Ensure your training data is located at "data/input.txt". ```bash cargo run --example char-rnn ``` -------------------------------- ### Run Neural Style Transfer Example Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/neural-style-transfer/README.md Execute the neural style transfer example from the command line. This command processes a style image and a content image using a pre-trained VGG-16 model. ```bash cargo run --example neural-style-transfer style.jpg content.jpg vgg16.ot ``` -------------------------------- ### Run A2C Example with Cargo Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/reinforcement-learning/README.md Execute the A2C reinforcement learning example on any OpenAI Gym Atari game. Generates weight files in the current directory upon completion. ```bash cargo run --example reinforcement-learning --features=python a2c ``` -------------------------------- ### Running Pre-trained Model Example via Cargo in Bash Source: https://github.com/laurentmazare/tch-rs/blob/main/README.md This command shows how to execute the `pretrained-models` example from the `tch-rs` library, specifying a pre-trained ResNet-18 model and an input image file. ```bash cargo run --example pretrained-models -- resnet18.ot tiger.jpg ``` -------------------------------- ### Install safetensors via pip Source: https://github.com/laurentmazare/tch-rs/blob/main/README.md Install the safetensors package using Python's pip package manager. ```bash pip install safetensors ``` -------------------------------- ### Initialize Input Image Variable for Optimization in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/neural-style-transfer/README.md Create a new variable store for the image being optimized. The initial value of this variable is a copy of the content image, serving as the starting point for gradient descent. ```rust let vs = nn::VarStore::new(device); let input_var = vs.root().var_copy("img", &content_img); ``` -------------------------------- ### Defining and Training a Simple Neural Network in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/README.md This example illustrates building a basic neural network with one hidden layer using `tch-rs`'s `nn` API. It then trains this network on the MNIST dataset using the Adam optimizer, showing the training loop and accuracy calculation. ```rust use anyhow::Result; use tch::{nn, nn::Module, nn::OptimizerConfig, Device}; const IMAGE_DIM: i64 = 784; const HIDDEN_NODES: i64 = 128; const LABELS: i64 = 10; fn net(vs: &nn::Path) -> impl Module { nn::seq() .add(nn::linear( vs / "layer1", IMAGE_DIM, HIDDEN_NODES, Default::default(), )) .add_fn(|xs| xs.relu()) .add(nn::linear(vs, HIDDEN_NODES, LABELS, Default::default())) } pub fn run() -> Result<()> { let m = tch::vision::mnist::load_dir("data")?; let vs = nn::VarStore::new(Device::Cpu); let net = net(&vs.root()); let mut opt = nn::Adam::default().build(&vs, 1e-3)?; for epoch in 1..200 { let loss = net .forward(&m.train_images) .cross_entropy_for_logits(&m.train_labels); opt.backward_step(&loss); let test_accuracy = net .forward(&m.test_images) .accuracy_for_logits(&m.test_labels); println!( "epoch: {:4} train loss: {:8.5} test acc: {:5.2}%", epoch, f64::from(&loss), 100. * f64::from(&test_accuracy), ); } Ok(()) } ``` -------------------------------- ### Load and run model in Python Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit-trace/README.md Execute this script to load the saved model.pt and run inference on MNIST dataset. Requires torch and torchvision packages installed. ```bash python ./examples/jit-trace/test.py ``` -------------------------------- ### Build and test Python extension with tch-rs Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/python-extension/README.md Compile the tch-ext crate and run the test suite. Requires a Python environment with torch installed. The LIBTORCH_USE_PYTORCH flag ensures the C++ library matches the Python runtime version to prevent segfaults. ```bash LIBTORCH_USE_PYTORCH=1 cargo build -p tch-ext && cp -f target/debug/libtch_ext.so tch_ext.so PYTHONPATH=. python examples/python-extension/test.py ``` -------------------------------- ### CMake Project Configuration for tch Library Source: https://github.com/laurentmazare/tch-rs/blob/main/torch-sys/libtch/CMakeLists.txt Complete CMakeLists.txt configuration that sets up the tch static library build, links PyTorch dependencies, enforces C++11 standard, and defines installation targets. Required for building the torch-sys Rust bindings. ```cmake cmake_minimum_required(VERSION 3.5 FATAL_ERROR) project(tch) find_package(Torch REQUIRED) add_library(tch STATIC torch_api.cpp torch_api_generated.cpp) target_link_libraries(tch "${TORCH_LIBRARIES}") set_property(TARGET tch PROPERTY CXX_STANDARD 11) install(TARGETS tch DESTINATION .) ``` -------------------------------- ### Set LIBTORCH Environment Variable (Linux/macOS) Source: https://github.com/laurentmazare/tch-rs/blob/main/README.md Sets the `LIBTORCH` environment variable to the path of the manually installed libtorch directory for Linux and macOS users. ```bash export LIBTORCH=/path/to/libtorch ``` -------------------------------- ### Create variable store, model, and Adam optimizer Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/mnist/README.md Initialize a VarStore on GPU (if available), build a neural network model using the variable store root, and create an Adam optimizer with learning rate 1e-4 to manage all stored parameters. ```rust let vs = nn::VarStore::new(Device::cuda_if_available()); let net = Net::new(&vs.root()); let opt = nn::Optimizer::adam(&vs, 1e-4, Default::default()); ``` -------------------------------- ### Loading and Inferring with a Pre-trained ResNet Model in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/README.md This Rust snippet demonstrates the core logic for using a pre-trained computer vision model. It covers loading an image, initializing a `VarStore`, building and loading weights into a ResNet model, performing a forward pass, and printing top classification results. ```rust // First the image is loaded and resized to 224x224. let image = imagenet::load_image_and_resize(image_file)?; // A variable store is created to hold the model parameters. let vs = tch::nn::VarStore::new(tch::Device::Cpu); // Then the model is built on this variable store, and the weights are loaded. let resnet18 = tch::vision::resnet::resnet18(vs.root(), imagenet::CLASS_COUNT); vs.load(weight_file)?; // Apply the forward pass of the model to get the logits and convert them // to probabilities via a softmax. let output = resnet18 .forward_t(&image.unsqueeze(0), /*train=*/ false) .softmax(-1); // Finally print the top 5 categories and their associated probabilities. for (probability, class) in imagenet::top(&output, 5).iter() { println!("{:50} {:5.2}%", class, 100.0 * probability) } ``` -------------------------------- ### Initialize Device for Computation in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/neural-style-transfer/README.md Create a computation device, prioritizing CUDA (GPU) if available, otherwise falling back to CPU. This determines where tensors and models will be processed. ```rust let device = Device::cuda_if_available(); ``` -------------------------------- ### Perform Basic Tensor Operations (Rust) Source: https://github.com/laurentmazare/tch-rs/blob/main/README.md Demonstrates how to create a tensor from a slice, perform a multiplication operation, and print the resulting tensor using the `tch` crate. ```rust use tch::Tensor; fn main() { let t = Tensor::from_slice(&[3, 1, 4, 1, 5]); let t = t * 2; t.print(); } ``` -------------------------------- ### Mini-batch training loop with GPU support Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/mnist/README.md Train for 100 epochs using shuffled mini-batches of size 256 moved to the model's device. Compute cross-entropy loss and perform optimizer backward step for each batch, then evaluate test accuracy on full test set. ```rust for epoch in 1..100 { for (bimages, blabels) in m.train_iter(256).shuffle().to_device(vs.device()) { let loss = net .forward_t(&bimages, true) .cross_entropy_for_logits(&blabels); opt.backward_step(&loss); } let test_accuracy = net.batch_accuracy_for_logits(&m.test_images, &m.test_labels, vs.device(), 1024); println!("epoch: {:4} test acc: {:5.2}%", epoch, 100. * test_accuracy,); } ``` -------------------------------- ### Build Libtorch for Static Linking Source: https://github.com/laurentmazare/tch-rs/blob/main/README.md Clones the PyTorch repository and builds it with static linking enabled, which is required when `LIBTORCH_STATIC=1` is set. ```bash git clone -b v2.11.0 --recurse-submodule https://github.com/pytorch/pytorch.git pytorch-static --depth 1 cd pytorch-static USE_CUDA=OFF BUILD_SHARED_LIBS=OFF python setup.py build # export LIBTORCH to point at the build directory in pytorch-static. ``` -------------------------------- ### Training a Model via Gradient Descent in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/README.md This snippet demonstrates how to perform gradient descent using `tch-rs`. It defines a simple module, initializes a `VarStore` and `Sgd` optimizer, and then iteratively computes loss and performs optimization steps on dummy data. ```rust use tch::nn::{Module, OptimizerConfig}; use tch::{kind, nn, Device, Tensor}; fn my_module(p: nn::Path, dim: i64) -> impl nn::Module { let x1 = p.zeros("x1", &[dim]); let x2 = p.zeros("x2", &[dim]); nn::func(move |xs| xs * &x1 + xs.exp() * &x2) } fn gradient_descent() { let vs = nn::VarStore::new(Device::Cpu); let my_module = my_module(vs.root(), 7); let mut opt = nn::Sgd::default().build(&vs, 1e-2).unwrap(); for _idx in 1..50 { // Dummy mini-batches made of zeros. let xs = Tensor::zeros(&[7], kind::FLOAT_CPU); let ys = Tensor::zeros(&[7], kind::FLOAT_CPU); let loss = (my_module.forward(&xs) - ys).pow_tensor_scalar(2).sum(kind::Kind::Float); opt.backward_step(&loss); } } ``` -------------------------------- ### Set LIBTORCH Include and Lib Paths Separately (Linux/macOS) Source: https://github.com/laurentmazare/tch-rs/blob/main/README.md Specifies the include and lib directories for libtorch separately using `LIBTORCH_INCLUDE` and `LIBTORCH_LIB` environment variables. ```bash export LIBTORCH_INCLUDE=/path/to/libtorch/ export LIBTORCH_LIB=/path/to/libtorch/ ``` -------------------------------- ### Initialize Adam Optimizer in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/neural-style-transfer/README.md Creates an Adam optimizer bound to a variable store for gradient-based optimization. The optimizer is configured with a specified learning rate. ```rust let opt = nn::Adam::default().build(&vs, LEARNING_RATE)?; ``` -------------------------------- ### Set Quantization Engine in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit-quantized/README.md Before loading a quantized model, specify the quantization engine (backend) for inference. Choose FBGEMM for x86 or QNNPACK for ARM architectures. ```rust tch::QEngine::FBGEMM.set()? ``` ```rust tch::QEngine::QNNPACK.set()? ``` -------------------------------- ### Manual gradient descent training loop Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/mnist/README.md Perform 200 epochs of gradient descent by computing cross-entropy loss, zeroing gradients, backpropagating, and manually updating weights and biases. Use no_grad() to prevent gradient tracking during parameter updates. ```rust for epoch in 1..200 { let logits = m.train_images.mm(&ws) + &bs; let loss = logits.log_softmax(-1).nll_loss(&m.train_labels); ws.zero_grad(); bs.zero_grad(); loss.backward(); no_grad(|| { ws += ws.grad() * (-1); bs += bs.grad() * (-1); }); ``` -------------------------------- ### Load and Prepare Images for Model Input in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/neural-style-transfer/README.md Load style and content images, then move them to the specified device. The `unsqueeze` method adds a batch dimension, making them compatible with the model's input requirements. ```rust let style_img = imagenet::load_image(style_img)? .unsqueeze(0) .to_device(device); let content_img = imagenet::load_image(content_img)? .unsqueeze(0) .to_device(device); ``` -------------------------------- ### Load Pre-trained VGG-16 Model Weights in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/neural-style-transfer/README.md Initialize a variable store, load pre-trained VGG-16 weights from a file, and freeze the variable store to prevent model weight modification during optimization. ```rust let mut net_vs = tch::nn::VarStore::new(device); let net = vgg::vgg16(&net_vs.root(), imagenet::CLASS_COUNT); net_vs.load(weights_file)?; net_vs.freeze(); ``` -------------------------------- ### Train and serialize model in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit-trace/README.md Run this command to train a model on MNIST and save it as model.pt in TorchScript format. Requires MNIST data files unzipped in data/ directory. ```bash cargo run --example jit-trace ``` -------------------------------- ### Load Image and Resize for Model Input Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit/README.md Loads and preprocesses an image file to 224x224 dimensions with ImageNet normalization, returning a tensor ready for model inference. ```rust let image = imagenet::load_image_and_resize(image_file)?; ``` -------------------------------- ### Initialize weight and bias tensors with gradient tracking Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/mnist/README.md Create zero-initialized tensors for linear classifier weights and biases on CPU with gradient computation enabled via set_requires_grad(true). ```rust let mut ws = Tensor::zeros(&[IMAGE_DIM, LABELS], kind::FLOAT_CPU).set_requires_grad(true); let mut bs = Tensor::zeros(&[LABELS], kind::FLOAT_CPU).set_requires_grad(true); ``` -------------------------------- ### Load image dataset from directory (Rust) Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/transfer-learning/README.md Load the image dataset from the specified directory using the `imagenet::load_from_dir` helper function. The `println!` macro displays the dimensions of the loaded tensors. ```rust let dataset = imagenet::load_from_dir(dataset_dir)?; println!("{:?}", dataset); ``` -------------------------------- ### Quantize and Save a PyTorch ResNet18 Model in Python Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit-quantized/README.md This snippet demonstrates how to quantize a pre-trained ResNet18 model using `torchvision.models.quantization`, trace it with `torch.jit.trace`, and save it as a TorchScript file for later use in Rust. ```python import torch import torchvision model = torchvision.models.quantization.resnet18( pretrained=True, quantize=True ) model.eval() example = torch.rand(1, 3, 224, 224) traced_script_module = torch.jit.trace(model, example) traced_script_module.save("resnet18_int8.pt") ``` -------------------------------- ### Load and Run Torch Script Model from Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit/README.md Loads a serialized Torch Script model and runs inference on an image. Extracts model and image filenames from command-line arguments, preprocesses the image, and outputs top-5 predictions with probabilities. ```rust pub fn main() -> anyhow::Result<()> { let args: Vec<_> = std::env::args().collect(); let (model_file, image_file) = match args.as_slice() { [_, m, i] => (m.to_owned(), i.to_owned()), _ => bail!("usage: main model.pt image.jpg"), }; let image = imagenet::load_image_and_resize(image_file)?; let model = tch::CModule::load(model_file)?; let output = model.forward_ts(&[image.unsqueeze(0)])?.softmax(-1); for (probability, class) in imagenet::top(&output, 5).iter() { println!("{:50} {:5.2}%", class, 100.0 * probability) } Ok(()) } ``` -------------------------------- ### Load MNIST data with vision module Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/mnist/README.md Load MNIST dataset from the data directory using the vision helper module. Returns train/test images and labels as tensors. ```rust let m = vision::mnist::load_dir("data").unwrap(); ``` -------------------------------- ### Initialize ResNet-18 and extract features (Rust) Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/transfer-learning/README.md This snippet initializes a ResNet-18 model, loads pretrained weights, and then uses it to extract features from training and testing image datasets. The `no_grad` closure prevents gradient computation during feature extraction. ```rust let mut vs = tch::nn::VarStore::new(tch::Device::Cpu); let net = resnet::resnet18_no_final_layer(&vs.root()); vs.load(weights)?; let train_images = tch::no_grad(|| dataset.train_images.apply_t(&net, false)); let test_images = tch::no_grad(|| dataset.test_images.apply_t(&net, false)); ``` -------------------------------- ### Convert PyTorch Model to Torch Script with Tracing Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit/README.md Uses torch.jit.trace to serialize a pre-trained ResNet-18 model to a .pt file. Call model.eval() first to ensure batch-norm layers operate in testing mode rather than training mode. ```python import torch import torchvision model = torchvision.models.resnet18(pretrained=True) model.eval() example = torch.rand(1, 3, 224, 224) traced_script_module = torch.jit.trace(model, example) traced_script_module.save("model.pt") ``` -------------------------------- ### Gradient Descent Training Loop in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/neural-style-transfer/README.md Main optimization loop that computes combined style and content losses, performs backward pass and optimizer step, and periodically logs loss and saves intermediate results. Requires style_loss and content_loss to be computed before each iteration. ```rust for step_idx in 1..(1 + TOTAL_STEPS) { // ... compute style_loss and content_loss ... let loss = style_loss * STYLE_WEIGHT + content_loss; opt.backward_step(&loss); if step_idx % 1000 == 0 { println!("{} {}", step_idx, f64::from(loss)); imagenet::save_image(&input_var, &format!("out{}.jpg", step_idx))?; } } ``` -------------------------------- ### Define and Serialize PyTorch Model to Torch Script in Python Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit-train/README.md This script defines a simple convolutional neural network (DemoModule) and serializes it into a Torch Script file (model.pt) using torch.jit.script for later use in Rust. ```python import torch from torch.nn import Module class DemoModule(Module): def __init__(self): super().__init__() self.batch_norm = torch.nn.BatchNorm2d(1) self.conv1 = torch.nn.Conv2d(1, 8, kernel_size=(5, 5), padding=(2, 2)) self.conv2 = torch.nn.Conv2d(8, 16, kernel_size=(5, 5), padding=(2, 2)) self.flatten = torch.nn.Flatten() self.dropout = torch.nn.Dropout() self.linear1 = torch.nn.Linear(16 * 28 * 28, 100) self.linear2 = torch.nn.Linear(100, 10) def forward(self, x): x = self.batch_norm(x) x = self.conv1(x) x = self.conv2(x) x = self.flatten(x) x = self.dropout(x) x = self.linear1(x) return self.linear2(x) traced_script_module = torch.jit.script(DemoModule()) traced_script_module.save("model.pt") ``` -------------------------------- ### Print Top-5 Predictions with Probabilities Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit/README.md Iterates over the top 5 predicted classes and their probabilities, formatting output with class name and percentage. ```rust for (probability, class) in imagenet::top(&output, 5).iter() { println!("{:50} {:5.2}%", class, 100.0 * probability) } ``` -------------------------------- ### Train Linear Classifier with SGD in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/transfer-learning/README.md This snippet demonstrates the training loop using stochastic gradient descent, calculating cross-entropy loss and updating model weights, then evaluating accuracy after each epoch. ```rust let sgd = nn::Sgd::default().build(&vs, 1e-3)?; for epoch_idx in 1..1001 { let predicted = train_images.apply(&linear); let loss = predicted.cross_entropy_for_logits(&dataset.train_labels); sgd.backward_step(&loss); let test_accuracy = test_images .apply(&linear) .accuracy_for_logits(&dataset.test_labels); println!("{} {:.2}%", epoch_idx, 100. * f64::from(test_accuracy)); } ``` -------------------------------- ### Load Torch Script Model File Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit/README.md Loads a serialized Torch Script model from a .pt file using tch::CModule. ```rust let model = tch::CModule::load(model_file)?; ``` -------------------------------- ### Set LIBTORCH Environment Variables (Windows PowerShell) Source: https://github.com/laurentmazare/tch-rs/blob/main/README.md Temporarily sets the `LIBTORCH` environment variable and appends the libtorch `lib` directory to the `Path` variable in PowerShell for Windows users. ```powershell $Env:LIBTORCH = "X:\path\to\libtorch" $Env:Path += ";X:\path\to\libtorch\lib" ``` -------------------------------- ### Run Model Inference and Apply Softmax Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit/README.md Executes the model on an image tensor using forward_ts, then applies softmax to convert logits to probabilities across the 1000 ImageNet classes. ```rust let output = model.forward_ts(&[image.unsqueeze(0)])?.softmax(-1); ``` -------------------------------- ### Define Linear Layer with VarStore in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/transfer-learning/README.md This snippet initializes a variable store and defines a linear layer for a binary classifier, using precomputed features as input. ```rust let vs = tch::nn::VarStore::new(tch::Device::Cpu); let linear = nn::linear(vs.root(), 512, dataset.labels, Default::default()); ``` -------------------------------- ### Perform Forward and Backward Pass in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/transfer-learning/README.md This snippet shows the forward pass to compute predictions, calculates the cross-entropy loss, and performs the backward pass to compute gradients for optimization. ```rust let predicted = train_images.apply(&linear); let loss = predicted.cross_entropy_for_logits(&dataset.train_labels); sgd.backward_step(&loss); ``` -------------------------------- ### Export PyTorch ResNet18 weights to safetensors Source: https://github.com/laurentmazare/tch-rs/blob/main/README.md Save a pre-trained ResNet18 model's state dictionary to a safetensors file. The filename must use the `.safetensors` suffix for proper decoding by tch. ```python import torchvision from safetensors import torch as stt model = torchvision.models.resnet18(pretrained=True) stt.save_file(model.state_dict(), 'resnet18.safetensors') ``` -------------------------------- ### Alternative Model Inference Using apply Method Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit/README.md Runs inference on a single-input model by treating tch::CModule as a standard module via the apply method, equivalent to forward_ts but with different syntax. ```rust let output = image.unsqueeze(0).apply(&model).softmax(-1); ``` -------------------------------- ### Compute Style and Content Losses in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/neural-style-transfer/README.md Extracts intermediate layer activations from the network, then aggregates style loss from designated style layers and content loss from designated content layers using mean squared error. Requires STYLE_INDEXES and CONTENT_INDEXES to be predefined. ```rust let input_layers = net.forward_all_t(&input_var, false, Some(max_layer)); let style_loss: Tensor = STYLE_INDEXES .iter() .map(|&i| style_loss(&input_layers[i], &style_layers[i])) .sum(); let content_loss: Tensor = CONTENT_INDEXES .iter() .map(|&i| input_layers[i].mse_loss(&content_layers[i], 1)) .sum(); ``` -------------------------------- ### Extract Features from Style and Content Images in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/neural-style-transfer/README.md Run the neural network on the style and content images to extract features from various layers. These features are used to compute style and content losses. ```rust let style_layers = net.forward_all_t(&style_img, false, Some(max_layer)); let content_layers = net.forward_all_t(&content_img, false, Some(max_layer)); ``` -------------------------------- ### Load safetensors weights and run inference in tch-rs Source: https://github.com/laurentmazare/tch-rs/blob/main/README.md Load pre-trained ResNet18 weights from a safetensors file in Rust, process an image, and output top-5 ImageNet predictions. Requires tch-rs with vision module support. ```rust use anyhow::Result; use tch::{ Device, Kind, nn::VarStore, vision::{ imagenet, resnet::resnet18, } }; fn main() -> Result<()> { // Create the model and load the pre-trained weights let mut vs = VarStore::new(Device::cuda_if_available()); let model = resnet18(&vs.root(), 1000); vs.load("resnet18.safetensors")?; // Load the image file and resize it to the usual imagenet dimension of 224x224. let image = imagenet::load_image_and_resize224("dog.jpg")? .to_device(vs.device()); // Apply the forward pass of the model to get the logits let output = image .unsqueeze(0) .apply_t(&model, false) .softmax(-1, Kind::Float); // Print the top 5 categories for this image. for (probability, class) in imagenet::top(&output, 5).iter() { println!("{:50} {:5.2}%", class, 100.0 * probability) } Ok(()) } ``` -------------------------------- ### Load Trained Torch Script Model and Evaluate Accuracy in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit-train/README.md This Rust function loads a previously trained Torch Script model (trained_model.pt), sets it to evaluation mode, and calculates its accuracy on the test dataset. ```rust fn load_trained_and_test_acc(dataset: &Dataset, device: Device) -> Result<()> { let mut module = CModule::load_on_device("trained_model.pt", device)?; module.set_eval(); let accuracy = module.batch_accuracy_for_logits(&dataset.test_images, &dataset.test_labels, device, 1024); println!("Updated accuracy: {:5.2}%", 100. * accuracy); Ok(()) } ``` -------------------------------- ### Set libtorch Library Path on Linux and macOS Source: https://github.com/laurentmazare/tch-rs/blob/main/README.md These environment variables ensure that the tch-rs library can locate the libtorch shared libraries at runtime on Linux and macOS systems. ```bash # For Linux export LD_LIBRARY_PATH=/path/to/libtorch/lib:$LD_LIBRARY_PATH # For macOS export DYLD_LIBRARY_PATH=/path/to/libtorch/lib:$DYLD_LIBRARY_PATH ``` -------------------------------- ### Linear classifier forward pass with softmax Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/mnist/README.md Compute logits by matrix multiplication of input images with weights and adding bias. Output is transformed into probability distribution via softmax. ```rust let logits = m.train_images.mm(&ws) + &bs; ``` -------------------------------- ### Load, Train, and Save Torch Script Model in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/jit-train/README.md This Rust function loads a Torch Script model (model.pt), trains it on the MNIST dataset using tch-rs with an Adam optimizer, and saves the updated model as trained_model.pt. ```rust fn train_and_save_model(dataset: &Dataset, device: Device) -> Result<()> { let vs = VarStore::new(device); let mut trainable = TrainableCModule::load("model.pt", vs.root())?; trainable.set_train(); let initial_acc = trainable.batch_accuracy_for_logits( &dataset.test_images, &dataset.test_labels, vs.device(), 1024, ); println!("Initial accuracy: {:5.2}%", 100. * initial_acc); let mut opt = Adam::default().build(&vs, 1e-4)?; for epoch in 1..20 { for (images, labels) in dataset .train_iter(128) .shuffle() .to_device(vs.device()) .take(50) { let loss = trainable .forward_t(&images, true) .cross_entropy_for_logits(&labels); opt.backward_step(&loss); } let test_accuracy = trainable.batch_accuracy_for_logits( &dataset.test_images, &dataset.test_labels, vs.device(), 1024, ); println!("epoch: {:4} test acc: {:5.2}%", epoch, 100. * test_accuracy,); } trainable.save("trained_model.pt")?; Ok(()) } ``` -------------------------------- ### Calculate Gram Matrix and Style Loss in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/neural-style-transfer/README.md Define functions to compute the Gram matrix for a tensor and the style loss between two tensors. The Gram matrix is used to capture style information independently of spatial location. ```rust fn gram_matrix(m: &Tensor) -> Tensor { let (a, b, c, d) = m.size4().unwrap(); let m = m.view(&[a * b, c * d]); let g = m.matmul(&m.tr()); g / (a * b * c * d) } fn style_loss(m1: &Tensor, m2: &Tensor) -> Tensor { gram_matrix(m1).mse_loss(&gram_matrix(m2), 1) } ``` -------------------------------- ### Evaluate Test Accuracy in Rust Source: https://github.com/laurentmazare/tch-rs/blob/main/examples/transfer-learning/README.md This snippet calculates the accuracy of the linear model on the testing dataset after an epoch and prints the result. ```rust let test_accuracy = test_images .apply(&linear) .accuracy_for_logits(&dataset.test_labels); println!("{} {:.2}%", epoch_idx, 100. * f64::from(test_accuracy)); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.