Skip to Main Content
Rust Programming Cookbook
book

Rust Programming Cookbook

by Claus Matzinger
October 2019
Intermediate to advanced content levelIntermediate to advanced
444 pages
10h 37m
English
Packt Publishing
Content preview from Rust Programming Cookbook

How to do it...

Only a few steps are needed to train and use a neural network in Rust:

  1. Open Cargo.toml to add the dependency for tch-rs:
[dependencies]tch = "0.1"failure ="0.1"
  1. Let's add some code for the imports to src/main.rs before diving deep:
use std::io::{Error, ErrorKind};use std::path::Path;use std::time::Instant;use tch::{nn, nn::ModuleT, nn::OptimizerConfig, Device, Tensor};
  1. PyTorch (and thereby, tch-rs) architectures typically store their layers individually, so we can store them in individual properties in struct:
#[derive(Debug)]struct ConvNet {    conv1: nn::Conv2D,    conv2: nn::Conv2D,    fc1: nn::Linear,    fc2: nn::Linear,}impl ConvNet {    fn new(vs: &nn::Path, labels: i64) -> ConvNet {        ConvNet { conv1: nn::conv2d(vs, 1, 32, 5, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Rust Web Programming

Rust Web Programming

Maxwell Flitton
The Complete Rust Programming Reference Guide

The Complete Rust Programming Reference Guide

Rahul Sharma, Vesa Kaihlavirta, Claus Matzinger

Publisher Resources

ISBN: 9781789530667Supplemental Content