Skip to content

Commit

Permalink
fix(layer/back_propagate): remove redundant sub_assign, fix shaping
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnachittur committed Sep 9, 2023
1 parent 85214ac commit 3980caf
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/neural_network/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,20 @@ impl Layer {
};

// Compute loss
let loss = loss_function.loss(&output, &targets.resize_to(&output));
let d_loss = loss.activate(&self.activation);
let loss = loss_function.loss(&output, &targets);
let mean_loss = loss.mean();

let inputs = self.inputs.as_ref().unwrap();
let num_samples = inputs.rows as f64;

// Compute gradients for weights and biases
self.d_weights = inputs.transpose().matmul(&d_loss).div_scalar(num_samples);
self.d_biases = d_loss
.sum_axis(1)
.div_scalar(num_samples)
.resize_to(&self.biases);
self.d_weights = inputs.transpose().matmul(&loss).div_scalar(num_samples);
self.d_biases = loss.sum_axis(0).div_scalar(num_samples);

// Update weights and biases based on gradients
optimizer.step(&mut self.weights, &mut self.d_weights);
optimizer.step(&mut self.biases, &mut self.d_biases);

// Update weights and biases based on gradients
self.weights.sub_assign(&self.d_weights);
self.biases.sub_assign(&self.d_biases);

mean_loss
}

Expand Down

0 comments on commit 3980caf

Please sign in to comment.