pub fn backward(&mut self, dvalues: NdArray<f32>) -> Result<(), DenseLayerError> {
        let inputs = self
            .training
            .as_mut()
            .map(|t| std::mem::take(&mut t.inputs))
            .ok_or(DenseLayerError::NoInputs)?;

        // we know that training is some at this point
        let training = self.training.as_mut().unwrap();

        let inputs_t = inputs.transpose();
        inputs_t
            .matmul_f32(&dvalues, &mut training.dweights)
            .map_err(DenseLayerError::MatMulFail)?;

        let s = dvalues.clone().transpose();
        training.dbiases = crate::sum(&s);

        // Regularization
        if let Some(l1) = training.weight_regularizer_l1 {
            regularize_l1(l1, &mut training.dweights, &self.weights);
        }
        if let Some(l2) = training.weight_regularizer_l2 {
            regularize_l2(l2, &mut training.dweights, &self.weights);
        }

        if let Some(l1) = training.bias_regularizer_l1 {
            regularize_l1(l1, &mut training.dbiases, &self.biases);
        }
        if let Some(l2) = training.bias_regularizer_l2 {
            regularize_l2(l2, &mut training.dbiases, &self.biases);
        }

        // Gradients
        dvalues
            .matmul_f32(&self.weights.clone().transpose(), &mut training.dinputs)
            .map_err(DenseLayerError::MatMulFail)?;

        Ok(())
    }
/// Consumes the last `inputs` replacing it with an empty array.
Consumes the last `inputs` replacing it with an empty array.
Consumes the last ` inputs ` replacing it with an empty array .
pub fn backward(&mut self, dvalues: NdArray<f32>) -> Result<(), DenseLayerError> {
        let inputs = self
            .training
            .as_mut()
            .map(|t| std::mem::take(&mut t.inputs))
            .ok_or(DenseLayerError::NoInputs)?;
        // we know that training is some at this point
        let training = self.training.as_mut().unwrap();
        let inputs_t = inputs.transpose();
        inputs_t
            .matmul_f32(&dvalues, &mut training.dweights)
            .map_err(DenseLayerError::MatMulFail)?;
        let s = dvalues.clone().transpose();
        training.dbiases = crate::sum(&s);
        // Regularization
        if let Some(l1) = training.weight_regularizer_l1 {
            regularize_l1(l1, &mut training.dweights, &self.weights);
        }
        if let Some(l2) = training.weight_regularizer_l2 {
            regularize_l2(l2, &mut training.dweights, &self.weights);
        }
        if let Some(l1) = training.bias_regularizer_l1 {
            regularize_l1(l1, &mut training.dbiases, &self.biases);
        }
        if let Some(l2) = training.bias_regularizer_l2 {
            regularize_l2(l2, &mut training.dbiases, &self.biases);
        }
        // Gradients
        dvalues
            .matmul_f32(&self.weights.clone().transpose(), &mut training.dinputs)
            .map_err(DenseLayerError::MatMulFail)?;
        Ok(())
    }
pub fn backward ( & mut self , dvalues : NdArray < f32 > ) -> Result < ( ) , DenseLayerError > { let inputs = self . training . as_mut ( ) . map ( | t | std :: mem :: take ( & mut t . inputs ) ) . ok_or ( DenseLayerError :: NoInputs ) ? ; // we know that training is some at this point let training = self . training . as_mut ( ) . unwrap ( ) ; let inputs_t = inputs . transpose ( ) ; inputs_t . matmul_f32 ( & dvalues , & mut training . dweights ) . map_err ( DenseLayerError :: MatMulFail ) ? ; let s = dvalues . clone ( ) . transpose ( ) ; training . dbiases = crate :: sum ( & s ) ; // Regularization if let Some ( l1 ) = training . weight_regularizer_l1 { regularize_l1 ( l1 , & mut training . dweights , & self . weights ) ; } if let Some ( l2 ) = training . weight_regularizer_l2 { regularize_l2 ( l2 , & mut training . dweights , & self . weights ) ; } if let Some ( l1 ) = training . bias_regularizer_l1 { regularize_l1 ( l1 , & mut training . dbiases , & self . biases ) ; } if let Some ( l2 ) = training . bias_regularizer_l2 { regularize_l2 ( l2 , & mut training . dbiases , & self . biases ) ; } // Gradients dvalues . matmul_f32 ( & self . weights . clone ( ) . transpose ( ) , & mut training . dinputs ) . map_err ( DenseLayerError :: MatMulFail ) ? ; Ok ( ( ) ) }
Consumes the last `inputs` replacing it with an empty array.
Consumes the last ` inputs ` replacing it with an empty array .
&mut self
dvalues
NdArray<f32>