Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:LaurentMazare Tch rs VarStore New

From Leeroopedia


Knowledge Sources
Domains Deep_Learning, Parameter_Management
Last Updated 2026-02-08 14:00 GMT

Overview

Concrete tool for creating a device-bound parameter store provided by the tch nn module.

Description

nn::VarStore::new creates a new variable store on the specified device. The VarStore holds an Arc<Mutex<Variables>> containing an empty named_variables HashMap and an empty trainable_variables Vec. The default kind is Float (f32). All subsequent layer constructors that receive a path from this VarStore will register their parameters into it.

Usage

Call this as the first step when building any neural network model. Pass Device::Cpu for CPU training or Device::cuda_if_available() for GPU training. The VarStore must be created before any layers.

Code Reference

Source Location

  • Repository: tch-rs
  • File: src/nn/var_store.rs
  • Lines: 58-62

Signature

pub fn new(device: Device) -> VarStore

Import

use tch::nn;
use tch::Device;

I/O Contract

Inputs

Name Type Required Description
device Device Yes Target compute device (Device::Cpu, Device::Cuda(0), or Device::cuda_if_available())

Outputs

Name Type Description
VarStore nn::VarStore Empty variable store with no parameters, default Kind::Float

Usage Examples

CPU VarStore

use tch::{nn, Device};

let vs = nn::VarStore::new(Device::Cpu);
let root = vs.root();  // Get root path for layer construction

CUDA VarStore

use tch::{nn, Device};

// Automatically use CUDA if available, otherwise CPU
let vs = nn::VarStore::new(Device::cuda_if_available());
println!("Training on: {:?}", vs.device());

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment