-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "bias_test" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[features] | ||
metal = ["dep:luminal_metal"] | ||
cuda = ["dep:luminal_cuda"] | ||
|
||
[dependencies] | ||
luminal = { path = "../.." } | ||
luminal_training = { path = "../../crates/luminal_training" } | ||
luminal_nn = { path = "../../crates/luminal_nn" } | ||
rand = "0.8.5" | ||
luminal_metal = { path = "../../crates/luminal_metal", optional = true } | ||
luminal_cuda = { path = "../../crates/luminal_cuda", optional = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use luminal::prelude::*; | ||
use luminal_nn::Linear; | ||
use luminal_training::Autograd; | ||
|
||
pub struct LinearBiased { | ||
pub linear: Linear<1, 2>, | ||
pub bias: GraphTensor<R1<2>>, | ||
} | ||
|
||
impl SerializeModule for LinearBiased { | ||
fn serialize(&self, s: &mut luminal::module::Serializer) { | ||
s.module("linear", &self.linear); | ||
s.tensor("bias", self.bias); | ||
} | ||
} | ||
|
||
impl InitModule for LinearBiased { | ||
fn initialize(cx: &mut Graph) -> Self { | ||
use rand::Rng; | ||
let mut rng = rand::thread_rng(); | ||
Self { | ||
linear: Linear::initialize(cx), | ||
bias: cx.named_tensor("Bias").set( | ||
(0..2) | ||
.map(|_| rng.gen_range(-1_f32..1_f32)) | ||
.collect::<Vec<_>>(), | ||
), | ||
} | ||
} | ||
} | ||
|
||
impl Module<GraphTensor<R1<1>>> for LinearBiased { | ||
type Output = GraphTensor<R1<2>>; | ||
fn forward(&self, x: GraphTensor<R1<1>>) -> Self::Output { | ||
let x: GraphTensor<R1<2>> = self.linear.forward(x); | ||
let bias: GraphTensor<R1<2>> = self.bias.expand::<R1<2>, Axis<0>>(); | ||
// let bias: GraphTensor<R1<2>> = self.bias.clone(); | ||
x + bias | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut cx = Graph::new(); | ||
let model = LinearBiased::initialize(&mut cx); | ||
let input = cx.tensor::<R1<1>>(); | ||
let output = model.forward(input).retrieve(); | ||
let loss = output.sum_reduce().retrieve(); | ||
|
||
let weights = params(&model); | ||
let _grads = cx.compile(Autograd::new(&weights, loss), ()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters