Fri Jul 12 2024
Understanding the CBHG Module in Tacotron from the Code
xs = xs.transpose(1, 2) + convs
Here it does a transpose to change the dimensions back to , then adds the residual connection.
The HighwayNet Layer
This section mainly covers what HighwayNet is.
The main purpose of HighwayNet is to skip useless layers and speed up information flow.
According to the original HighwayNet paper, the authors describe the nonlinear transformation in a traditional plain neural network as y = , where H abstractly represents the nonlinear transformation and W_H represents the parameters in that transformation.
HighwayNet adds two more nonlinear transformations: and . T is called the transform gate and C is called the carry gate. The meaning behind these two names comes through in the following equation:
y =
As you can see, the transform gate controls the original output of this nonlinear transformation layer, while the carry gate controls the original input. In other words, the transform gate controls how much of the transformation should be output, and the carry gate controls how much of the input should be carried straight to the output.
In the original paper, the authors set C to 1-T, so the equation becomes:
y =
Now let's look at the code implementation:
class HighwayNet(torch.nn.Module):
def __init__(self, idim):
super(HighwayNet, self).__init__()
self.idim = idim
self.H = nn.Linear(idim, idim)
self.T = nn.Linear(idim, idim)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
# x: (batch, Tmax, idim)
H = self.H(x)
T = self.sigmoid(self.T(x))
C = 1 - T
y = H * T + x * C
return y
