Skip to content

Commit

Permalink
Add a nn layer for conv-transpose2d. (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentMazare authored Sep 7, 2023
1 parent 6527ab8 commit a17a7c4
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion candle-nn/src/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ impl Default for Conv2dConfig {
}
}

#[allow(dead_code)]
#[derive(Debug)]
pub struct Conv2d {
weight: Tensor,
Expand Down Expand Up @@ -122,6 +121,56 @@ impl crate::Module for Conv2d {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ConvTranspose2dConfig {
pub padding: usize,
pub output_padding: usize,
pub stride: usize,
pub dilation: usize,
// TODO: support groups.
}

#[derive(Debug)]
pub struct ConvTranspose2d {
weight: Tensor,
bias: Option<Tensor>,
config: ConvTranspose2dConfig,
}

impl ConvTranspose2d {
pub fn new(weight: Tensor, bias: Option<Tensor>, config: ConvTranspose2dConfig) -> Self {
Self {
weight,
bias,
config,
}
}

pub fn config(&self) -> &ConvTranspose2dConfig {
&self.config
}
}

impl crate::Module for ConvTranspose2d {
fn forward(&self, x: &Tensor) -> Result<Tensor> {
let x = x.conv_transpose2d(
&self.weight,
self.config.padding,
self.config.output_padding,
self.config.stride,
self.config.dilation,
)?;
match &self.bias {
None => Ok(x),
Some(bias) => {
let b = bias.dims1()?;
let bias = bias.reshape((1, b, 1, 1))?;
Ok(x.broadcast_add(&bias)?)
}
}
}
}

pub fn conv1d(
in_channels: usize,
out_channels: usize,
Expand Down

0 comments on commit a17a7c4

Please sign in to comment.