use std::num::NonZeroUsize;
use candle_core::Tensor;
mod categorical;
mod gaussian;
mod transformed;
pub use categorical::{CategoricalDistribution, CategoricalDistributionError};
pub use gaussian::{GaussianDistribution, GaussianDistributionError};
pub use transformed::{
AffineTransform, AffineTransformError, DistributionTransform, TanhTransform,
TransformedDistribution, TransformedDistributionError,
};
pub struct DistEval {
log_prob: Tensor,
entropy: Tensor,
}
impl DistEval {
pub fn new(log_prob: Tensor, entropy: Tensor) -> Self {
Self { log_prob, entropy }
}
pub fn log_prob(&self) -> &Tensor {
&self.log_prob
}
pub fn entropy(&self) -> &Tensor {
&self.entropy
}
}
pub trait Distribution {
type Error;
fn sample(&self, outputs: &Tensor) -> Result<Tensor, Self::Error>;
fn mode(&self, outputs: &Tensor) -> Result<Tensor, Self::Error>;
fn dist_eval(&self, outputs: &Tensor, actions: &Tensor) -> Result<DistEval, Self::Error>;
}
#[derive(Clone, Debug)]
pub struct ExpectationTerms {
actions: Tensor,
log_probabilities: Tensor,
weights: Tensor,
}
impl ExpectationTerms {
pub fn new(
actions: Tensor,
log_probabilities: Tensor,
weights: Tensor,
) -> candle_core::Result<Self> {
let (batch_size, candidate_count) = log_probabilities.dims2()?;
if candidate_count == 0 {
return Err(candle_core::Error::EmptyTensor {
op: "distribution expectation candidate axis",
});
}
if weights.dims() != log_probabilities.dims() {
return Err(candle_core::Error::UnexpectedShape {
msg: "expectation weights must match log probabilities".into(),
expected: log_probabilities.shape().clone(),
got: weights.shape().clone(),
});
}
if actions.rank() < 2 {
return Err(candle_core::Error::DimOutOfRange {
shape: actions.shape().clone(),
dim: 1,
op: "distribution expectation actions",
});
}
if actions.dim(0)? != batch_size || actions.dim(1)? != candidate_count {
let mut expected = actions.dims().to_vec();
expected[0] = batch_size;
expected[1] = candidate_count;
return Err(candle_core::Error::UnexpectedShape {
msg: "expectation actions must match the batch and candidate dimensions".into(),
expected: expected.into(),
got: actions.shape().clone(),
});
}
Ok(Self {
actions,
log_probabilities,
weights,
})
}
pub fn actions(&self) -> &Tensor {
&self.actions
}
pub fn log_probabilities(&self) -> &Tensor {
&self.log_probabilities
}
pub fn weights(&self) -> &Tensor {
&self.weights
}
pub fn into_parts(self) -> (Tensor, Tensor, Tensor) {
(self.actions, self.log_probabilities, self.weights)
}
}
pub trait DifferentiableExpectation: Distribution {
fn expectation(
&self,
outputs: &Tensor,
samples: NonZeroUsize,
) -> Result<ExpectationTerms, Self::Error>;
fn default_target_entropy(&self, outputs: &Tensor) -> Result<f64, Self::Error>;
}
#[cfg(test)]
mod tests {
use super::ExpectationTerms;
use candle_core::{DType, Device, Tensor};
#[test]
fn expectation_terms_enforce_common_batch_and_candidate_dimensions() {
let actions = Tensor::zeros(&[2, 3, 4, 5], DType::F32, &Device::Cpu).unwrap();
let probabilities = Tensor::zeros((2, 3), DType::F32, &Device::Cpu).unwrap();
assert!(
ExpectationTerms::new(actions, probabilities.clone(), probabilities.clone()).is_ok()
);
let wrong_candidates = Tensor::zeros(&[2, 4, 4, 5], DType::F32, &Device::Cpu).unwrap();
assert!(matches!(
ExpectationTerms::new(
wrong_candidates,
probabilities.clone(),
probabilities.clone()
),
Err(candle_core::Error::UnexpectedShape { .. })
));
let wrong_weights = Tensor::zeros((2, 1), DType::F32, &Device::Cpu).unwrap();
assert!(matches!(
ExpectationTerms::new(
Tensor::zeros((2, 3), DType::U32, &Device::Cpu).unwrap(),
probabilities,
wrong_weights
),
Err(candle_core::Error::UnexpectedShape { .. })
));
}
}