Aan de slagGa gratis aan de slag

Two-input model

With the data ready, it's time to build the two-input model architecture! To do so, you will set up a model class with the following methods:

  • .__init__(), in which you will define sub-networks by grouping layers; this is where you define the two layers for processing the two inputs, and the classifier that returns a classification score for each class.

  • forward(), in which you will pass both inputs through corresponding pre-defined sub-networks, concatenate the outputs, and pass them to the classifier.

torch.nn is already imported for you as nn. Let's do it!

Deze oefening maakt deel uit van de cursus

Intermediate Deep Learning with PyTorch

Cursus bekijken

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        # Define sub-networks as sequential models
        ____ = ____(
            nn.Conv2d(1, 16, kernel_size=3, padding=1),
            nn.MaxPool2d(kernel_size=2),
            nn.ELU(),
            nn.Flatten(),
            nn.Linear(16*32*32, 128)
        )
        ____ = ____(
            nn.Linear(30, 8),
            nn.ELU(), 
        )
        ____ = ____(
            nn.Linear(128 + 8, 964), 
        )
Code bewerken en uitvoeren