entropy working
This commit is contained in:
56
hod_1/src/main.rs
Normal file
56
hod_1/src/main.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
use burn::tensor::{backend::Backend, Tensor, TensorData};
|
||||
use burn_ndarray::NdArray;
|
||||
use ndarray::{array, Array1};
|
||||
|
||||
type B = NdArray<f64>;
|
||||
|
||||
fn main() {
|
||||
let device = <B as Backend>::Device::default();
|
||||
|
||||
// ndarray::Array1<f64>
|
||||
let a: Array1<f64> = array![0.4, 0.6];
|
||||
|
||||
// Convert ndarray -> Burn tensor
|
||||
let p = Tensor::<B, 1>::from_data(
|
||||
TensorData::new(a.to_vec(), [a.len()]),
|
||||
&device,
|
||||
);
|
||||
|
||||
let h = entropy(p);
|
||||
println!("{h}");
|
||||
}
|
||||
|
||||
fn entropy(p: Tensor<B, 1>) -> f64 {
|
||||
// Handle p = 0 safely, because 0 * log(0) should contribute 0
|
||||
let zero_mask = p.clone().equal_elem(0.0);
|
||||
let p_safe = p.clone().mask_fill(zero_mask.clone(), 1.0);
|
||||
|
||||
let terms = (p.clone() * p_safe.log()).mask_fill(zero_mask, 0.0);
|
||||
|
||||
(-terms).sum().into_scalar()
|
||||
}
|
||||
|
||||
// pub fn entropy2(p: Tensor<B, 1>) -> f64 {
|
||||
// if p == 0.0 {
|
||||
// return 0.0;
|
||||
// }
|
||||
// - p * p.ln()
|
||||
// }
|
||||
|
||||
// pub fn cross_entropy(p: Tensor<B, 1>, q: f64) -> f64 {
|
||||
// if p == 0.0 {
|
||||
// return 0.0;
|
||||
// }
|
||||
// - p * q.ln()
|
||||
// }
|
||||
|
||||
// pub fn kl_div(p: f64, q: f64) -> f64 {
|
||||
// if p == 0.0 {
|
||||
// return 0.0;
|
||||
// }
|
||||
// p*(p.ln()-q.ln())
|
||||
// }
|
||||
|
||||
// pub fn kl_div2(p: f64, q: f64) -> f64 {
|
||||
// cross_entropy(p,q) - entropy(p)
|
||||
// }
|
||||
Reference in New Issue
Block a user