forked from tracel-ai/burn
-
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
1 parent
34ad49d
commit bffc543
Showing
24 changed files
with
234 additions
and
25 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
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,9 @@ | ||
use crate::tensor::backend::backend::Backend; | ||
use crate::tensor::{backend::autodiff::ADTensor, ops::*}; | ||
|
||
impl<B: Backend, P, const D: usize> TensorOpsDetach<P, D> for ADTensor<D, B> { | ||
fn detach(self) -> Self { | ||
let tensor = self.tensor(); | ||
Self::from_tensor(tensor.detach()) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use crate::tensor::backend::backend::Backend; | ||
use crate::ElementConversion; | ||
use crate::{ | ||
execute_ops, | ||
graph::ops::{UnaryOps, UnaryOpsNodeState}, | ||
register_ops, | ||
tensor::{backend::autodiff::ADTensor, ops::*}, | ||
}; | ||
|
||
register_ops!( | ||
ops UnaryOps, | ||
name ADTensorPowOps state f32, | ||
partial | | ||
value: &f32, | ||
state: &UnaryOpsNodeState<B::TensorPrimitive<D>, B::TensorPrimitive<D>> | ||
| { | ||
let value = state.input | ||
.value() | ||
.powf(value - 1.0) | ||
.mul_scalar(&value.clone().to_elem()); | ||
state.output.grad().mul(&value) | ||
}, | ||
); | ||
|
||
impl<B: Backend, const D: usize> TensorOpsPow<B::Elem, D> for ADTensor<D, B> { | ||
fn powf(&self, value: f32) -> Self { | ||
execute_ops!( | ||
input self.node.clone(), | ||
out TensorOpsPow::powf(&self.tensor(), value), | ||
ops ADTensorPowOps::<B, D>::new(value.clone()), | ||
) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::tensor::{backend::autodiff::helper::TestADTensor, Data}; | ||
|
||
#[test] | ||
fn should_diff_powf() { | ||
let data_1 = Data::<f64, 2>::from([[0.0, 1.0], [3.0, 4.0]]); | ||
let data_2 = Data::<f64, 2>::from([[6.0, 7.0], [9.0, 10.0]]); | ||
|
||
let tensor_1 = TestADTensor::from_data(data_1.clone()); | ||
let tensor_2 = TestADTensor::from_data(data_2.clone()); | ||
|
||
let tensor_3 = tensor_1.matmul(&tensor_2.powf(0.4)); | ||
let tensor_4 = tensor_3.matmul(&tensor_2); | ||
let grads = tensor_4.backward(); | ||
|
||
let grad_1 = tensor_1.grad(&grads).unwrap(); | ||
let grad_2 = tensor_2.grad(&grads).unwrap(); | ||
|
||
grad_1 | ||
.to_data() | ||
.assert_approx_eq(&Data::from([[68.0, 79.0328], [68.0, 79.0328]]), 3); | ||
grad_2 | ||
.to_data() | ||
.assert_approx_eq(&Data::from([[23.5081, 25.2779], [26.0502, 28.6383]]), 3); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use crate::{ | ||
tensor::{backend::ndarray::NdArrayTensor, ops::*}, | ||
NdArrayElement, | ||
}; | ||
|
||
impl<E, const D: usize> TensorOpsDetach<E, D> for NdArrayTensor<E, D> | ||
where | ||
E: NdArrayElement, | ||
{ | ||
fn detach(self) -> Self { | ||
self | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use crate::{ | ||
tensor::{backend::ndarray::NdArrayTensor, ops::*}, | ||
NdArrayElement, | ||
}; | ||
|
||
impl<E, const D: usize> TensorOpsPow<E, D> for NdArrayTensor<E, D> | ||
where | ||
E: NdArrayElement, | ||
{ | ||
fn powf(&self, value: f32) -> Self { | ||
let array = self.array.mapv(|a| a.pow_elem(value)).into_shared(); | ||
let shape = self.shape.clone(); | ||
|
||
Self { array, shape } | ||
} | ||
} |
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,13 @@ | ||
use crate::{ | ||
tensor::{backend::tch::TchTensor, ops::*}, | ||
TchElement, | ||
}; | ||
|
||
impl<E, const D: usize> TensorOpsDetach<E, D> for TchTensor<E, D> | ||
where | ||
E: TchElement, | ||
{ | ||
fn detach(self) -> Self { | ||
self | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use crate::{ | ||
tensor::{backend::tch::TchTensor, ops::*}, | ||
TchElement, | ||
}; | ||
|
||
impl<E, const D: usize> TensorOpsPow<E, D> for TchTensor<E, D> | ||
where | ||
E: TchElement, | ||
{ | ||
fn powf(&self, value: f32) -> Self { | ||
let tensor = self.tensor.pow_tensor_scalar(value as f64); | ||
let kind = self.kind.clone(); | ||
let shape = self.shape.clone(); | ||
|
||
Self { | ||
tensor, | ||
shape, | ||
kind, | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub(crate) mod ops; | ||
pub(crate) mod stats; | ||
|
||
mod bool_tensor; | ||
mod data; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use crate::{backend::Backend, ElementConversion, Tensor}; | ||
|
||
pub fn var<B: Backend, const D: usize>(tensor: &Tensor<B, D>, dim: usize) -> Tensor<B, D> { | ||
let mean = tensor.mean_dim(dim); | ||
var_with_mean(tensor, &mean, dim) | ||
} | ||
|
||
pub fn var_with_mean<B: Backend, const D: usize>( | ||
tensor: &Tensor<B, D>, | ||
mean: &Tensor<B, D>, | ||
dim: usize, | ||
) -> Tensor<B, D> { | ||
let n = tensor.shape().dims[dim] - 1; | ||
let n = (n as f32).to_elem(); | ||
|
||
tensor.sub(&mean).powf(2.0).sum_dim(dim).div_scalar(&n) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ pub type TestADBackend = burn_tensor::backend::TchADBackend<f32>; | |
mod activation; | ||
mod grad; | ||
mod ops; | ||
mod stats; |
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 |
---|---|---|
|
@@ -9,5 +9,6 @@ mod mask; | |
mod matmul; | ||
mod mul; | ||
mod neg; | ||
mod powf; | ||
mod reshape; | ||
mod sub; |
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,13 @@ | ||
use super::super::TestBackend; | ||
use burn_tensor::{Data, Tensor}; | ||
|
||
#[test] | ||
fn should_support_powf_ops() { | ||
let data = Data::from([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]]); | ||
let tensor = Tensor::<TestBackend, 2>::from_data(data); | ||
|
||
let data_actual = tensor.powf(0.71).into_data(); | ||
|
||
let data_expected = Data::from([[0.0, 1.0, 1.6358], [2.182, 2.6759, 3.1352]]); | ||
data_expected.assert_approx_eq(&data_actual, 3); | ||
} |
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,13 @@ | ||
use super::super::TestBackend; | ||
use burn_tensor::{Data, Tensor}; | ||
|
||
#[test] | ||
fn test_var() { | ||
let data = Data::from([[0.5, 1.8, 0.2, -2.0], [3.0, -4.0, 5.0, 0.0]]); | ||
let tensor = Tensor::<TestBackend, 2>::from_data(data); | ||
|
||
let data_actual = tensor.var(1).into_data(); | ||
|
||
let data_expected = Data::from([[2.4892], [15.3333]]); | ||
data_expected.assert_approx_eq(&data_actual, 3); | ||
} |
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 @@ | ||
mod basic; |
Oops, something went wrong.