Skip to main content

TrainConfig

Struct TrainConfig 

Source
pub struct TrainConfig {
    pub hidden: Vec<usize>,
    pub epochs: usize,
    pub per_epoch: usize,
    pub batch: usize,
    pub lr: f64,
    pub apm: bool,
    pub seed: u64,
}
Expand description

Trainer settings.

Fields§

§hidden: Vec<usize>

Hidden layer widths.

§epochs: usize

Passes.

§per_epoch: usize

Samples per pass (a seeded random subset when fewer than available).

§batch: usize

Minibatch size.

§lr: f64

Adam learning rate (cosine-decayed to 10%).

§apm: bool

Use the adaptive probability map at coding time.

§seed: u64

Seed.

Implementations§

Source§

impl TrainConfig

Source

pub fn overfit() -> Self

Small network for per-file overfitting (its weights are shipped).

Source

pub fn pretrain() -> Self

Larger network for offline pretraining.

Examples found in repository?
examples/pretrain.rs (line 21)
16fn main() {
17    let a: Vec<String> = std::env::args().skip(1).collect();
18    let out = &a[0];
19    let mut dims = vec![64, 128, 128];
20    let mut psnrs = vec![40.0, 60.0, 80.0, 100.0];
21    let mut cfg = TrainConfig::pretrain();
22    let mut per = 150_000usize;
23    let mut files = Vec::new();
24    let mut i = 1;
25    while i < a.len() {
26        match a[i].as_str() {
27            "--dims" => dims = a[i + 1].split('x').map(|s| s.parse().unwrap()).collect(),
28            "--psnr" => psnrs = a[i + 1].split(',').map(|s| s.parse().unwrap()).collect(),
29            "--hidden" => cfg.hidden = a[i + 1].split(',').map(|s| s.parse().unwrap()).collect(),
30            "--per" => per = a[i + 1].parse().unwrap(),
31            "--epochs" => cfg.epochs = a[i + 1].parse().unwrap(),
32            "--per-epoch" => cfg.per_epoch = a[i + 1].parse().unwrap(),
33            f => {
34                files.push(f.to_string());
35                i += 1;
36                continue;
37            }
38        }
39        i += 2;
40    }
41    let mut samples: Vec<Sample> = Vec::new();
42    let mut seed = 12345u64;
43    for f in &files {
44        let b = std::fs::read(f).unwrap();
45        let x: Vec<f32> = b
46            .as_chunks::<4>()
47            .0
48            .iter()
49            .map(|&c| f32::from_le_bytes(c))
50            .collect();
51        let xd: Vec<f64> = x.iter().map(|&v| v as f64).collect();
52        let co = Coeffs::new(&xd, &dims);
53        for &p in &psnrs {
54            let wc = WavConfig::new(ErrorBound::Psnr(p));
55            let (_, st) = encode_f32(&x, &dims, &wc);
56            let (q, _) = quantize(&co, st.delta, wc.rho);
57            let preds = fit_predictors(&co.p, &q);
58            let mut s = collect(&co.p, &q, &preds);
59            // seeded partial Fisher–Yates subsample
60            let k = per.min(s.len());
61            for j in 0..k {
62                seed = seed
63                    .wrapping_mul(6364136223846793005)
64                    .wrapping_add(1442695040888963407);
65                let r = j + (seed >> 11) as usize % (s.len() - j);
66                s.swap(j, r);
67            }
68            s.truncate(k);
69            eprintln!(
70                "{f} psnr {p}: delta {:.4e}, hand {:.4} bpv, {} samples",
71                st.delta,
72                st.bits_per_value,
73                s.len()
74            );
75            samples.extend(s);
76        }
77    }
78    let t = std::time::Instant::now();
79    let net = train(&samples, &cfg);
80    eprintln!(
81        "trained {} params on {} samples in {:.1}s: {:.4} bits/sample (float view)",
82        net.num_params(),
83        samples.len(),
84        t.elapsed().as_secs_f64(),
85        mean_bits_float(&net, &samples)
86    );
87    std::fs::write(out, Arc::new(net).to_bytes()).unwrap();
88    let _ = Net::from_bytes(&std::fs::read(out).unwrap()).unwrap();
89}

Trait Implementations§

Source§

impl Clone for TrainConfig

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TrainConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.