optional structure fields #2591
Answered
by
laggui
wangjiawen2013
asked this question in
Q&A
-
Beta Was this translation helpful? Give feedback.
Answered by
laggui
Dec 3, 2024
Replies: 1 comment
-
This can be achieved via a simple Take this example from the ResNet implementation: #[derive(Module, Debug)]
pub struct BasicBlock<B: Backend> {
conv1: Conv2d<B>,
bn1: BatchNorm<B, 2>,
relu: Relu,
conv2: Conv2d<B>,
bn2: BatchNorm<B, 2>,
downsample: Option<Downsample<B>>, // optional
}
impl<B: Backend> BasicBlock<B> {
fn forward(&self, input: Tensor<B, 4>) -> Tensor<B, 4> {
let identity = input.clone();
let out = self.conv1.forward(input);
let out = self.bn1.forward(out);
let out = self.relu.forward(out);
let out = self.conv2.forward(out);
let out = self.bn2.forward(out);
let out = {
// Optional module
match &self.downsample {
Some(downsample) => out + downsample.forward(identity),
None => out + identity,
}
};
// Activation
self.relu.forward(out)
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
laggui
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This can be achieved via a simple
Option
field.Take this example from the ResNet implementation: