v9.1.1
Menu

Count Parameters in Conv Layer

easyPROConvolutional Neural Networks (CNNs)Deep LearningDeep Learning 1

Problem Statement

Calculate the number of learnable parameters in a Conv2d layer.

Background

A convolutional layer's parameters consist of weights and biases:

  • For KK filters of size F×FF \times F applied to an input with CinC_{in} channels
  • Weight parameters: F×F×Cin×KF \times F \times C_{in} \times K
  • Bias parameters: KK (one per filter)

Your Task

Write a function count_conv_params(filter_size, in_channels, out_channels) returning the total parameter count (weights + biases).

Output Format

Return an integer representing the total number of learnable parameters.

Example

Input
filter_size=3, in_channels=1, out_channels=10
Output
100
Reasoning
  • Inputs: filter_size F=3F = 3 means each filter is a 3×33 \times 3 window; in_channels Cin=1C_{in} = 1 means the input has a single channel; out_channels K=10K = 10 means the layer learns 10 separate filters.
  • Weights per filter: a filter must cover the whole F×FF \times F window in every input channel, so it holds 3×3×1=93 \times 3 \times 1 = 9 weights.
  • All weights: 10 independent filters give 9×10=909 \times 10 = 90 weights.
  • Biases: each filter adds one scalar offset to its output map, so there are K=10K = 10 biases, independent of FF and CinC_{in}.
  • Output: 90+10=90 + 10 = 100, a single integer.
  • Notice that the weight count grows with the square of the filter size and linearly in both channel counts: with Cin=3C_{in} = 3 and K=64K = 64 the same 3×33 \times 3 filters already need 27×64+64=179227 \times 64 + 64 = 1792 parameters. The image's height and width never enter the count.

Constraints

  • 1 <= filter_size <= 11
  • 1 <= in_channels <= 512
  • 1 <= out_channels <= 512

Learn this in

More from Deep Learning 1

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.