Skip to content

Commit be386f2

Browse files
committed
formatted and fixed clippy remark
1 parent fc58b36 commit be386f2

16 files changed

Lines changed: 145 additions & 78 deletions

File tree

examples/mnist-mlp-cpu/mnist.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
use meuron::{NeuralNetwork, Layers, NetworkType};
21
use meuron::activation::{ReLU, Softmax};
32
use meuron::cost::CrossEntropy;
4-
use meuron::optimizer::SGD;
5-
use meuron::layer::{DenseLayer};
3+
use meuron::layer::DenseLayer;
64
use meuron::metric::classification::accuracy;
5+
use meuron::optimizer::SGD;
6+
use meuron::{Layers, NetworkType, NeuralNetwork};
77
use ndarray::Array2;
88
use std::fs::File;
99
use std::io::{self, Read};
1010
use std::path::PathBuf;
1111

12-
type MnistNetwork = NeuralNetwork<
13-
NetworkType![DenseLayer<ReLU>, DenseLayer<Softmax>],
14-
CrossEntropy,
15-
>;
16-
12+
type MnistNetwork =
13+
NeuralNetwork<NetworkType![DenseLayer<ReLU>, DenseLayer<Softmax>], CrossEntropy>;
1714

1815
fn read_u32_from_file(file: &mut File) -> Result<u32, io::Error> {
1916
let mut buf = [0u8; 4];
@@ -82,9 +79,12 @@ fn main() {
8279
let (images, labels) = match load_mnist_data(
8380
PathBuf::from("./examples/mnist-mlp-cpu/train-images.idx3-ubyte"),
8481
PathBuf::from("./examples/mnist-mlp-cpu/train-labels.idx1-ubyte"),
85-
) {
82+
) {
8683
Ok(data) => data,
87-
Err(e) => { eprintln!("Error loading MNIST training data: {}", e); return; }
84+
Err(e) => {
85+
eprintln!("Error loading MNIST training data: {}", e);
86+
return;
87+
}
8888
};
8989

9090
println!("Loaded {} training images", images.shape()[0]);
@@ -100,7 +100,10 @@ fn main() {
100100
PathBuf::from("./t10k-labels.idx1-ubyte"),
101101
) {
102102
Ok(data) => data,
103-
Err(e) => { eprintln!("Error loading MNIST test data: {}", e); return; }
103+
Err(e) => {
104+
eprintln!("Error loading MNIST test data: {}", e);
105+
return;
106+
}
104107
};
105108

106109
let acc = accuracy(&mut nn, &test_images, &test_labels);

src/activation/sigmoid.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use ndarray::Dimension;
2-
use serde::{Deserialize, Serialize};
31
use crate::activation::Activation;
42
use crate::backend::Backend;
3+
use ndarray::Dimension;
4+
use serde::{Deserialize, Serialize};
55

66
#[derive(Clone, Copy, Serialize, Deserialize)]
77
pub struct Sigmoid;
@@ -16,4 +16,3 @@ impl<B: Backend> Activation<B> for Sigmoid {
1616
B::mul(&s, &B::scalar_sub(1.0, &s))
1717
}
1818
}
19-

src/activation/softmax.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use ndarray::{Dimension};
2-
use serde::{Deserialize, Serialize};
31
use crate::activation::Activation;
42
use crate::backend::Backend;
3+
use ndarray::Dimension;
4+
use serde::{Deserialize, Serialize};
55

66
#[derive(Clone, Copy, Serialize, Deserialize)]
77
pub struct Softmax;
@@ -17,4 +17,4 @@ impl<B: Backend> Activation<B> for Softmax {
1717
fn vjp<D: Dimension>(&self, z: &B::Tensor<D>, grad: &B::Tensor<D>) -> B::Tensor<D> {
1818
B::softmax_vjp(z, grad)
1919
}
20-
}
20+
}

src/activation/tanh.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use ndarray::{Dimension};
2-
use serde::{Deserialize, Serialize};
31
use crate::activation::Activation;
42
use crate::backend::Backend;
3+
use ndarray::Dimension;
4+
use serde::{Deserialize, Serialize};
55

66
#[derive(Clone, Copy, Serialize, Deserialize)]
77
pub struct Tanh;
@@ -11,7 +11,9 @@ impl<B: Backend> Activation<B> for Tanh {
1111
B::mapv(x, |v| v.tanh())
1212
}
1313
fn derivative<D: Dimension>(&self, x: &B::Tensor<D>) -> B::Tensor<D> {
14-
B::mapv(x, |v| { let t = v.tanh(); 1.0 - t * t })
14+
B::mapv(x, |v| {
15+
let t = v.tanh();
16+
1.0 - t * t
17+
})
1518
}
1619
}
17-

src/backend/cpu.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use crate::backend::Backend;
12
use ndarray::{Array, ArrayBase, Axis, Dimension, Ix2, OwnedRepr, RemoveAxis};
23
use ndarray_rand::RandomExt;
34
use ndarray_rand::rand_distr::Uniform;
4-
use crate::backend::Backend;
55

66
#[derive(Clone)]
77
pub struct CPUBackend;
@@ -29,10 +29,18 @@ impl Backend for CPUBackend {
2929
tensor.mapv(f)
3030
}
3131

32-
fn add<D: Dimension>(a: &Self::Tensor<D>, b: &Self::Tensor<D>) -> Self::Tensor<D> { a + b }
33-
fn sub<D: Dimension>(a: &Self::Tensor<D>, b: &Self::Tensor<D>) -> Self::Tensor<D> { a - b }
34-
fn mul<D: Dimension>(a: &Self::Tensor<D>, b: &Self::Tensor<D>) -> Self::Tensor<D> { a * b }
35-
fn div<D: Dimension>(a: &Self::Tensor<D>, b: &Self::Tensor<D>) -> Self::Tensor<D> { a / b }
32+
fn add<D: Dimension>(a: &Self::Tensor<D>, b: &Self::Tensor<D>) -> Self::Tensor<D> {
33+
a + b
34+
}
35+
fn sub<D: Dimension>(a: &Self::Tensor<D>, b: &Self::Tensor<D>) -> Self::Tensor<D> {
36+
a - b
37+
}
38+
fn mul<D: Dimension>(a: &Self::Tensor<D>, b: &Self::Tensor<D>) -> Self::Tensor<D> {
39+
a * b
40+
}
41+
fn div<D: Dimension>(a: &Self::Tensor<D>, b: &Self::Tensor<D>) -> Self::Tensor<D> {
42+
a / b
43+
}
3644

3745
fn scale<D: Dimension>(tensor: &Self::Tensor<D>, scalar: f32) -> Self::Tensor<D> {
3846
tensor * scalar
@@ -71,8 +79,10 @@ impl Backend for CPUBackend {
7179
let b2 = b_dyn.view().into_dimensionality::<Ix2>().unwrap();
7280
let mut result = Array::zeros((batch, m, n)).into_dyn();
7381
for i in 0..batch {
74-
let ai = a_dyn.index_axis(Axis(0), i)
75-
.into_dimensionality::<Ix2>().unwrap();
82+
let ai = a_dyn
83+
.index_axis(Axis(0), i)
84+
.into_dimensionality::<Ix2>()
85+
.unwrap();
7686
result.index_axis_mut(Axis(0), i).assign(&ai.dot(&b2));
7787
}
7888
result
@@ -82,15 +92,23 @@ impl Backend for CPUBackend {
8292
let n = b_dyn.shape()[2];
8393
let mut result = Array::zeros((batch, m, n)).into_dyn();
8494
for i in 0..batch {
85-
let ai = a_dyn.index_axis(Axis(0), i)
86-
.into_dimensionality::<Ix2>().unwrap();
87-
let bi = b_dyn.index_axis(Axis(0), i)
88-
.into_dimensionality::<Ix2>().unwrap();
95+
let ai = a_dyn
96+
.index_axis(Axis(0), i)
97+
.into_dimensionality::<Ix2>()
98+
.unwrap();
99+
let bi = b_dyn
100+
.index_axis(Axis(0), i)
101+
.into_dimensionality::<Ix2>()
102+
.unwrap();
89103
result.index_axis_mut(Axis(0), i).assign(&ai.dot(&bi));
90104
}
91105
result
92106
}
93-
_ => panic!("matmul: unsupported shapes {:?} × {:?}", a_dyn.shape(), b_dyn.shape()),
107+
_ => panic!(
108+
"matmul: unsupported shapes {:?} × {:?}",
109+
a_dyn.shape(),
110+
b_dyn.shape()
111+
),
94112
};
95113
out.into_dimensionality::<D1>()
96114
.expect("matmul output rank must match left operand")
@@ -121,7 +139,6 @@ impl Backend for CPUBackend {
121139
.expect("broadcast_add output rank must match left operand")
122140
}
123141

124-
125142
fn softmax<D: Dimension>(tensor: &Self::Tensor<D>) -> Self::Tensor<D> {
126143
let shape = tensor.shape().to_vec();
127144
let last_dim = shape[shape.len() - 1];

src/backend/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ compile_error!(
1616
);
1717

1818
#[cfg(all(feature = "cpu", feature = "gpu"))]
19-
compile_error!(r#"
19+
compile_error!(
20+
r#"
2021
Only one backend feature can be active at a time. Choose either "cpu" or "gpu":
2122
2223
meuron = { version = "^0.2.0", features = ["cpu"] }
2324
meuron = { version = "^0.2.0", features = ["gpu"] }
24-
"#);
25-
25+
"#
26+
);
2627

2728
#[cfg(feature = "cpu")]
2829
pub type DefaultBackend = CPUBackend;

src/cost/binary_cross_entropy.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use ndarray::Dimension;
2-
use serde::{Deserialize, Serialize};
31
use crate::backend::Backend;
42
use crate::cost::Cost;
3+
use ndarray::Dimension;
4+
use serde::{Deserialize, Serialize};
55

66
#[derive(Clone, Copy, Serialize, Deserialize)]
77
pub struct BinaryCrossEntropy;
@@ -12,7 +12,10 @@ impl<B: Backend> Cost<B> for BinaryCrossEntropy {
1212
let c = B::mapv(predicted, |v| v.clamp(eps, 1.0 - eps));
1313
let loss = B::add(
1414
&B::mul(target, &B::mapv(&c, |v| v.ln())),
15-
&B::mul(&B::scalar_sub(1.0, target), &B::mapv(&c, |v| (1.0 - v).ln())),
15+
&B::mul(
16+
&B::scalar_sub(1.0, target),
17+
&B::mapv(&c, |v| (1.0 - v).ln()),
18+
),
1619
);
1720
-B::mean(&loss).unwrap_or(0.0)
1821
}

src/cost/cross_entropy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use ndarray::Dimension;
2-
use serde::{Deserialize, Serialize};
31
use crate::backend::Backend;
42
use crate::cost::Cost;
3+
use ndarray::Dimension;
4+
use serde::{Deserialize, Serialize};
55

66
#[derive(Clone, Copy, Serialize, Deserialize)]
77
pub struct CrossEntropy;

src/cost/mod.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
pub mod mse; pub mod cross_entropy; pub mod binary_cross_entropy;
2-
pub use mse::MSE; pub use cross_entropy::CrossEntropy;
3-
pub use binary_cross_entropy::BinaryCrossEntropy;
1+
pub mod binary_cross_entropy;
2+
pub mod cross_entropy;
3+
pub mod mse;
44
use crate::backend::Backend;
5+
pub use binary_cross_entropy::BinaryCrossEntropy;
6+
pub use cross_entropy::CrossEntropy;
7+
pub use mse::MSE;
58
use ndarray::Dimension;
69

710
pub trait Cost<B: Backend> {
811
fn loss<D: Dimension>(&self, predicted: &B::Tensor<D>, target: &B::Tensor<D>) -> f32;
9-
fn gradient<D: Dimension>(&self, predicted: &B::Tensor<D>, target: &B::Tensor<D>) -> B::Tensor<D>;
12+
fn gradient<D: Dimension>(
13+
&self,
14+
predicted: &B::Tensor<D>,
15+
target: &B::Tensor<D>,
16+
) -> B::Tensor<D>;
1017
}

src/cost/mse.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use ndarray::Dimension;
2-
use serde::{Deserialize, Serialize};
31
use crate::backend::Backend;
42
use crate::cost::Cost;
3+
use ndarray::Dimension;
4+
use serde::{Deserialize, Serialize};
55

66
#[derive(Clone, Copy, Serialize, Deserialize)]
77
pub struct MSE;
@@ -11,7 +11,11 @@ impl<B: Backend> Cost<B> for MSE {
1111
let diff = B::sub(predicted, target);
1212
B::mean(&B::mul(&diff, &diff)).unwrap_or(0.0)
1313
}
14-
fn gradient<D: Dimension>(&self, predicted: &B::Tensor<D>, target: &B::Tensor<D>) -> B::Tensor<D> {
14+
fn gradient<D: Dimension>(
15+
&self,
16+
predicted: &B::Tensor<D>,
17+
target: &B::Tensor<D>,
18+
) -> B::Tensor<D> {
1519
B::sub(predicted, target)
1620
}
17-
}
21+
}

0 commit comments

Comments
 (0)