use crate::error::Error;
use crate::neural_network::Tensor;
use crate::neural_network::layers::TrainingParameters;
use crate::neural_network::layers::layer_weight::LayerWeight;
use crate::neural_network::layers::no_trainable_parameters_layer_functions;
use crate::neural_network::layers::upsampling::Interpolation;
use crate::neural_network::layers::upsampling::resize_engine::{
upsample_backward, upsample_forward, upsample_summary, validate_factors,
};
use crate::neural_network::traits::Layer;
#[derive(Debug)]
pub struct UpSampling1D {
size: usize,
input_shape: Option<Vec<usize>>,
}
impl UpSampling1D {
pub fn new(size: usize) -> Result<Self, Error> {
validate_factors(&[size])?;
Ok(UpSampling1D {
size,
input_shape: None,
})
}
}
impl Layer for UpSampling1D {
fn forward(&mut self, input: &Tensor) -> Result<Tensor, Error> {
let output = upsample_forward(
input,
&[self.size],
Interpolation::Nearest,
3,
"UpSampling1D",
)?;
self.input_shape = Some(input.shape().to_vec());
Ok(output)
}
fn predict(&self, input: &Tensor) -> Result<Tensor, Error> {
upsample_forward(
input,
&[self.size],
Interpolation::Nearest,
3,
"UpSampling1D",
)
}
fn backward(&mut self, grad_output: &Tensor) -> Result<Tensor, Error> {
upsample_backward(
grad_output,
self.input_shape.as_deref(),
&[self.size],
Interpolation::Nearest,
"UpSampling1D",
)
}
fn layer_type(&self) -> &str {
"UpSampling1D"
}
fn output_shape(&self) -> String {
upsample_summary(self.input_shape.as_deref(), &[self.size])
}
no_trainable_parameters_layer_functions!();
}