Sure. Here is the modified PolygloNet model that takes an input tensor with dimensions (batch_size, channels, height, width):
import torch
import torch.nn as nn
import torch.nn.functional as F
class Encoder(nn.Module):
def __init__(self, input_size, hidden_size, num_layers):
super(Encoder, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.conv1 = nn.Conv2d(input_size, hidden_size, kernel_size=3, padding=1)
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(hidden_size, hidden_size, kernel_size=3, padding=1)
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.gru = nn.GRU(hidden_size * 16, hidden_size, num_layers, batch_first=True, bidirectional=True)
def forward(self, input):
# input shape: (batch_size, channels, height, width)
x = self.conv1(input)
x = self.relu1(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.relu2(x)
x = self.pool2(x)
# reshape tensor for GRU input
seq_len, batch_size, hidden_size, height, width = x.size()
x = x.permute(1, 0, 2, 3, 4).contiguous()
x = x.view(batch_size, seq_len, -1)
outputs, hidden = self.gru(x) # outputs shape: (batch_size, seq_len, 2*hidden_size), hidden shape: (2*num_layers, batch_size, hidden_size)
return outputs, hidden
class Attention(nn.Module):
def __init__(self, hidden_size):
super(Attention, self).__init__()
self.attn = nn.Linear(hidden_size*2, hidden_size)
self.v = nn.Linear(hidden_size, 1, bias=False)
def forward(self, hidden, encoder_outputs):
# hidden shape: (2, batch_size, hidden_size)
# encoder_outputs shape: (batch_size, seq_len, 2*hidden_size)
seq_len = encoder_outputs.size(1)
h = hidden[-1].unsqueeze(1).repeat(1, seq_len, 1) # shape: (batch_size, seq_len, hidden_size)
energy = F.tanh(self.attn(torch.cat((h, encoder_outputs), dim=2))) # shape: (batch_size, seq_len, hidden_size)
attention = F.softmax(self.v(energy), dim=1) # shape: (batch_size, seq_len, 1)
context = torch.bmm(attention.permute(0, 2, 1), encoder_outputs) # shape: (batch_size, 1, 2*hidden_size)
return context.permute(1, 0, 2) # shape: (1, batch_size, 2*hidden_size)
class Decoder(nn.Module):
def __init__(self, output_size, hidden_size, num_layers):
super(Decoder, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.attention = Attention(hidden_size)
self.gru = nn.GRU(hidden_size*2, hidden_size, num_layers, batch_first=True)
self.out = nn.Linear(hidden_size, output_size)
def forward(self, input, hidden, encoder_outputs):
# input shape: (batch_size, 1, output_size)
# hidden shape: (num_layers, batch_size, hidden_size)
# encoder_outputs shape: (batch_size, seq_len, 2*hidden_size)
context = self.attention(hidden, encoder_outputs)
# concatenate context vector and input vector
rnn_input = torch.cat((context, input), dim=2)
output, hidden = self.gru(rnn_input, hidden)
output = self.out(output.squeeze(1))
return output.unsqueeze(1), hidden
class PolygloNet(nn.Module):
def __init__(self, input_size, output_sizes, hidden_size, num_layers):
super(PolygloNet, self).__init__()
self.encoder = Encoder(input_size, hidden_size, num_layers)
self.decoders = nn.ModuleList(
[Decoder(output_sizes[i], hidden_size, num_layers) for i in range(len(output_sizes))]
)
def forward(self, input, target_lang):
# input shape: (batch_size, channels, height, width)
encoder_outputs, hidden = self.encoder(input)
decoder_output, _ = self.decoders[target_lang](
torch.zeros(input.size(0), 1, self.decoders[target_lang].hidden_size).to(input.device),
hidden,
encoder_outputs
)
outputs = decoder_output.unsqueeze(0) # shape: (1, batch_size, output_size)
return outputs
Here, we use two 2D convolutional layers in the encoder followed by a bidirectional GRU. The decoder network still uses an attention mechanism to align the input sequence with the output sequence, and we have modified its input and output sizes to match the changes to the input tensor dimensions.
To use this model, you can pass input tensors of size (batch_size, channels, height, width) to the forward method along with the target language index.