-
Notifications
You must be signed in to change notification settings - Fork 0
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
344 additions
and
117 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 |
---|---|---|
|
@@ -2,3 +2,6 @@ | |
name = "minigrad" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
candle-core = "0.4.1" |
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 |
---|---|---|
@@ -1,16 +1,17 @@ | ||
use crate::variable::Variable; | ||
use candle_core::Tensor; | ||
|
||
#[derive(Debug)] | ||
pub struct Gradient { | ||
derivatives: Vec<f64>, | ||
pub(crate) derivatives: Vec<Tensor>, | ||
} | ||
|
||
impl Gradient { | ||
pub fn from(derivatives: Vec<f64>) -> Self { | ||
pub fn from(derivatives: Vec<Tensor>) -> Self { | ||
Self { derivatives } | ||
} | ||
|
||
pub fn wrt(&self, var: &Variable) -> f64 { | ||
self.derivatives[var.index] | ||
pub fn wrt(&self, var: &Variable) -> &Tensor { | ||
&self.derivatives[var.index] | ||
} | ||
} |
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 |
---|---|---|
@@ -1,58 +1,75 @@ | ||
use std::ops::{Add, Mul, Sub}; | ||
use candle_core::{DType, Device, Shape, Tensor}; | ||
|
||
use crate::tape::convert_to_tensor; | ||
use crate::variable::Variable; | ||
use std::ops::{Add, Mul, Sub}; | ||
|
||
impl<'a> Add for Variable<'a> { | ||
type Output = Variable<'a>; | ||
fn add(self, rhs: Self) -> Self::Output { | ||
let position = self | ||
.tape | ||
.unwrap() | ||
.push_binary(1.0, self.index, 1.0, rhs.index); | ||
let new_value = self.value + rhs.value; | ||
Variable::new(self.tape.unwrap(), position, new_value) | ||
let new_value = (&self.value + &rhs.value).unwrap(); | ||
let n = rhs.value.shape().dims()[1]; | ||
println!("THe second dimension sizie is {}", n); | ||
let position = self.tape.unwrap().push_binary( | ||
Tensor::from_slice(&[1.0], (1, 1), &candle_core::Device::Cpu).unwrap(), | ||
self.index, | ||
Tensor::eye(n, DType::F64, &Device::Cpu).unwrap(), | ||
rhs.index, | ||
new_value.shape().clone(), | ||
); | ||
Variable::new_tensor(self.tape.unwrap(), position, new_value) | ||
} | ||
} | ||
|
||
// this is for self - rhs | ||
impl<'a> Sub for Variable<'a> { | ||
type Output = Variable<'a>; | ||
fn sub(self, rhs: Self) -> Self::Output { | ||
let position = self | ||
.tape | ||
.unwrap() | ||
.push_binary(1.0, self.index, -1.0, rhs.index); | ||
let new_value = self.value - rhs.value; | ||
Variable::new(self.tape.unwrap(), position, new_value) | ||
let new_value = (&self.value - &rhs.value).unwrap(); | ||
let position = self.tape.unwrap().push_binary( | ||
rhs.value.t().unwrap().ones_like().unwrap(), | ||
self.index, | ||
(-1.0 * self.value.t().unwrap().ones_like().unwrap()).unwrap(), | ||
rhs.index, | ||
new_value.shape().clone(), | ||
); | ||
Variable::new_tensor(self.tape.unwrap(), position, new_value) | ||
} | ||
} | ||
|
||
impl<'a> Mul for Variable<'a> { | ||
type Output = Variable<'a>; | ||
fn mul(self, rhs: Self) -> Self::Output { | ||
let position = self | ||
.tape | ||
.unwrap() | ||
.push_binary(rhs.value, self.index, self.value, rhs.index); | ||
let new_value = self.value * rhs.value; | ||
Variable::new(self.tape.unwrap(), position, new_value) | ||
let new_value = self.value.matmul(&rhs.value).unwrap(); | ||
let position = self.tape.unwrap().push_binary( | ||
rhs.value.t().unwrap().clone(), | ||
self.index, | ||
self.value.t().unwrap().clone(), | ||
rhs.index, | ||
new_value.shape().clone(), | ||
); | ||
Variable::new_tensor(self.tape.unwrap(), position, new_value) | ||
} | ||
} | ||
|
||
impl<'a> Mul<Variable<'a>> for f64 { | ||
type Output = Variable<'a>; | ||
fn mul(self, rhs: Variable<'a>) -> Self::Output { | ||
let position = rhs.tape.unwrap().push_unary(self, rhs.index); | ||
let new_value = self * rhs.value; | ||
Variable::new(rhs.tape.unwrap(), position, new_value) | ||
let new_value = (self * rhs.value).unwrap(); | ||
let position = rhs.tape.unwrap().push_unary( | ||
convert_to_tensor(self), | ||
rhs.index, | ||
new_value.shape().clone(), | ||
); | ||
Variable::new_tensor(rhs.tape.unwrap(), position, new_value) | ||
} | ||
} | ||
|
||
impl<'a> Mul<Variable<'a>> for i32 { | ||
type Output = Variable<'a>; | ||
fn mul(self, rhs: Variable<'a>) -> Self::Output { | ||
let position = rhs.tape.unwrap().push_unary(self as f64, rhs.index); | ||
let new_value = self as f64 * rhs.value; | ||
Variable::new(rhs.tape.unwrap(), position, new_value) | ||
} | ||
} | ||
// impl<'a> Mul<Variable<'a>> for i32 { | ||
// type Output = Variable<'a>; | ||
// fn mul(self, rhs: Variable<'a>) -> Self::Output { | ||
// let position = rhs.tape.unwrap().push_unary(self as f64, rhs.index); | ||
// let new_value = self as f64 * rhs.value; | ||
// Variable::new(rhs.tape.unwrap(), position, new_value) | ||
// } | ||
// } |
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
Oops, something went wrong.