TensorTonic
Study Plans
Study Plan
Apply an AlphaGo Residual Block
Reinforcement LearningComputer VisionNeural Networks
Medium

Apply the two-convolution residual block used in the AlphaGo Zero tower. For input XXX, convolution weights W1W_1W1​ and W2W_2W2​, channel scales γ1\gamma_1γ1​ and γ2\gamma_2γ2​, channel shifts β1\beta_1β1​ and β2\beta_2β2​, and batch normalization BN⁡\operatorname{BN}BN over batch and spatial axes, compute

H=ReLU⁡(BN⁡(Conv⁡3×3(X;W1);γ1,β1))H=\operatorname{ReLU}(\operatorname{BN}(\operatorname{Conv}_{3\times3}(X;W_1);\gamma_1,\beta_1))H=ReLU(BN(Conv3×3​(X;W1​);γ1​,β1​)) Y=ReLU⁡(X+BN⁡(Conv⁡3×3(H;W2);γ2,β2))Y=\operatorname{ReLU}(X+\operatorname{BN}(\operatorname{Conv}_{3\times3}(H;W_2);\gamma_2,\beta_2))Y=ReLU(X+BN(Conv3×3​(H;W2​);γ2​,β2​))

Here, H is the residual branch activation and Y is the block output. Both convolutions use stride 1, padding 1, and no bias.

For an activation z in one channel, batch normalization is

BN⁡(z;γ,β)=γz−μσ2+ε+β\operatorname{BN}(z;\gamma,\beta)=\gamma\frac{z-\mu}{\sqrt{\sigma^2+\varepsilon}}+\betaBN(z;γ,β)=γσ2+ε​z−μ​+β

Here, mu and sigma squared are that channel's mean and population variance over the batch and spatial dimensions. Gamma and beta are its supplied scale and shift, and epsilon is the supplied positive normalization constant.

Return Y as a PyTorch float32 tensor with the same shape and device as x.

Loading visualization...

Examples

Input: x = [[[[2,-1],[0,3]]]], conv1_weight = [[[[0,0,0],[0,0,0],[0,0,0]]]], scale1 = [1], shift1 = [0], conv2_weight = [[[[0,0,0],[0,0,0],[0,0,0]]]], scale2 = [1], shift2 = [0], epsilon = 0.00001

Output: [[[[2.0,0.0],[0.0,3.0]]]]

Explanation: The convolution branch is zero, so the final ReLU acts on the shortcut.

Input: x = [[[[-2]]]], conv1_weight = [[[[0,0,0],[0,0,0],[0,0,0]]]], scale1 = [1], shift1 = [0], conv2_weight = [[[[0,0,0],[0,0,0],[0,0,0]]]], scale2 = [1], shift2 = [0], epsilon = 0.0001

Output: [[[[0.0]]]]

Input: x shape = (2,2,2,2), conv1_weight shape = (2,2,3,3), conv2_weight shape = (2,2,3,3), scale1 = [0.5856,0.6118], shift1 = [-0.0436,-0.0539], scale2 = [0.9551,0.978], shift2 = [0.3145,-0.1452], epsilon = 0.00001

Output: 32-bit floating-point tensor with shape (2,2,2,2)

Hint 1

F.conv2d supports the padding needed to preserve spatial dimensions.

Hint 2

keepdim=True keeps channel statistics broadcastable.

Hint 3

Reshape a channel vector to (1, channels, 1, 1) for broadcasting.

Requirements

  • Use PyTorch float32 computation and output.
  • Preserve every input tensor and the input device.

Constraints

  • Input x is a nonempty floating-point PyTorch tensor of shape (batch, channels, height, width).
  • Both convolution weights have shape (channels, channels, 3, 3); each scale and shift has shape (channels,).
  • All supplied tensors are floating-point tensors on the same device; their input dtypes may differ.
  • Epsilon is a positive finite number.
Try Similar Problems
Compute the Policy and Value HeadsMediumAssemble a Compact Dual-Head NetworkHardEncode AlphaGo Zero Board HistoryMediumApply One Policy-Value Head Training StepHardImplement a Simple CNN Layer (NumPy)MediumImplement 2D Max Pooling in PythonMediumTurn Root Visits into a Search PolicyMediumImplement the AlphaGo Policy-Value LossMediumDQN Loss with Target NetworkMediumTransposed ConvolutionMediumDepthwise Separable ConvolutionMediumVGG Convolution BlockMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Apply an AlphaGo Residual Block

Reinforcement LearningComputer VisionNeural Networks
Medium

Apply the two-convolution residual block used in the AlphaGo Zero tower. For input XXX, convolution weights W1W_1W1​ and W2W_2W2​, channel scales γ1\gamma_1γ1​ and γ2\gamma_2γ2​, channel shifts β1\beta_1β1​ and β2\beta_2β2​, and batch normalization BN⁡\operatorname{BN}BN over batch and spatial axes, compute

H=ReLU⁡(BN⁡(Conv⁡3×3(X;W1);γ1,β1))H=\operatorname{ReLU}(\operatorname{BN}(\operatorname{Conv}_{3\times3}(X;W_1);\gamma_1,\beta_1))H=ReLU(BN(Conv3×3​(X;W1​);γ1​,β1​)) Y=ReLU⁡(X+BN⁡(Conv⁡3×3(H;W2);γ2,β2))Y=\operatorname{ReLU}(X+\operatorname{BN}(\operatorname{Conv}_{3\times3}(H;W_2);\gamma_2,\beta_2))Y=ReLU(X+BN(Conv3×3​(H;W2​);γ2​,β2​))

Here, H is the residual branch activation and Y is the block output. Both convolutions use stride 1, padding 1, and no bias.

For an activation z in one channel, batch normalization is

BN⁡(z;γ,β)=γz−μσ2+ε+β\operatorname{BN}(z;\gamma,\beta)=\gamma\frac{z-\mu}{\sqrt{\sigma^2+\varepsilon}}+\betaBN(z;γ,β)=γσ2+ε​z−μ​+β

Here, mu and sigma squared are that channel's mean and population variance over the batch and spatial dimensions. Gamma and beta are its supplied scale and shift, and epsilon is the supplied positive normalization constant.

Return Y as a PyTorch float32 tensor with the same shape and device as x.

Loading visualization...

Examples

Input: x = [[[[2,-1],[0,3]]]], conv1_weight = [[[[0,0,0],[0,0,0],[0,0,0]]]], scale1 = [1], shift1 = [0], conv2_weight = [[[[0,0,0],[0,0,0],[0,0,0]]]], scale2 = [1], shift2 = [0], epsilon = 0.00001

Output: [[[[2.0,0.0],[0.0,3.0]]]]

Explanation: The convolution branch is zero, so the final ReLU acts on the shortcut.

Input: x = [[[[-2]]]], conv1_weight = [[[[0,0,0],[0,0,0],[0,0,0]]]], scale1 = [1], shift1 = [0], conv2_weight = [[[[0,0,0],[0,0,0],[0,0,0]]]], scale2 = [1], shift2 = [0], epsilon = 0.0001

Output: [[[[0.0]]]]

Input: x shape = (2,2,2,2), conv1_weight shape = (2,2,3,3), conv2_weight shape = (2,2,3,3), scale1 = [0.5856,0.6118], shift1 = [-0.0436,-0.0539], scale2 = [0.9551,0.978], shift2 = [0.3145,-0.1452], epsilon = 0.00001

Output: 32-bit floating-point tensor with shape (2,2,2,2)

Hint 1

F.conv2d supports the padding needed to preserve spatial dimensions.

Hint 2

keepdim=True keeps channel statistics broadcastable.

Hint 3

Reshape a channel vector to (1, channels, 1, 1) for broadcasting.

Requirements

  • Use PyTorch float32 computation and output.
  • Preserve every input tensor and the input device.

Constraints

  • Input x is a nonempty floating-point PyTorch tensor of shape (batch, channels, height, width).
  • Both convolution weights have shape (channels, channels, 3, 3); each scale and shift has shape (channels,).
  • All supplied tensors are floating-point tensors on the same device; their input dtypes may differ.
  • Epsilon is a positive finite number.
Try Similar Problems
Compute the Policy and Value HeadsMediumAssemble a Compact Dual-Head NetworkHardEncode AlphaGo Zero Board HistoryMediumApply One Policy-Value Head Training StepHardImplement a Simple CNN Layer (NumPy)MediumImplement 2D Max Pooling in PythonMediumTurn Root Visits into a Search PolicyMediumImplement the AlphaGo Policy-Value LossMediumDQN Loss with Target NetworkMediumTransposed ConvolutionMediumDepthwise Separable ConvolutionMediumVGG Convolution BlockMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.