1

I am trying to write a training loop in MXNet with the Gluon API and write a validation loop to evaluate a trained network.

I am currently trying to define a network. I need define a network based on the LeNet architecture but instead of 10 output layers for the original MNIST data, I should have 2 output layers.

I'm not really sure how to go about it. I tried including Flatten and Dense when creating the network but I get the below error regardless. Is it to do with certain settings in the Conv2D and MaxPool2D layers?

Edit: After messing around with it more, I now think it has something to do with the loss function...

def get_network():
    """
    Should create the LeNet 10 network but with 2 output units instead of 10 and return a classification loss function
    
    :return: the network and the loss function
    :rtype: (gluon.Block, gluon.Block)
    """
    
    net = None
    loss_fn = None
    
    # create a network
    net = nn.Sequential()
    
    net.add(
        nn.Conv2D(channels=6, kernel_size=5, activation='relu'),
        nn.MaxPool2D(pool_size=2, strides=2),
        nn.Conv2D(channels=16, kernel_size=3, activation='relu'),
        nn.MaxPool2D(pool_size=2, strides=2)  
    )
    
    net.initialize(init=init.Xavier())
    
    # choose and set loss_fn for a classification task
    loss_fn = gluon.loss.SoftmaxCrossEntropyLoss()
    
    return net, loss_fn
n, loss_fn = get_network()

assert isinstance(n[0], nn.Conv2D)
assert isinstance(n[2], nn.Conv2D)
assert isinstance(n[1], nn.MaxPool2D)
assert isinstance(n[3], nn.MaxPool2D)

for l in n[-3:]:
    assert isinstance(l, nn.Dense)

I get the following error:

AssertionError                            Traceback (most recent call last)
<ipython-input-29-e1d75f4e8660> in <module>
      7 
      8 for l in n[-3:]:
----> 9     assert isinstance(l, nn.Dense)

AssertionError:
Mr. For Example
  • 4,173
  • 1
  • 9
  • 19
HollowDev
  • 63
  • 5

1 Answers1

0

You should add 3 Dense layer for the model, following code will running without any error:

from mxnet import init, gluon
from mxnet.gluon import nn

def get_network():
    """
    Should create the LeNet 10 network but with 2 output units instead of 10 and return a classification loss function
    
    :return: the network and the loss function
    :rtype: (gluon.Block, gluon.Block)
    """
    
    net = None
    loss_fn = None
    
    # create a network
    net = nn.Sequential()

    net.add(
      nn.Conv2D(channels=6,kernel_size=5,activation='relu'),
      nn.MaxPool2D(pool_size=2,strides=2),
      nn.Conv2D(channels=16,kernel_size=3,activation='relu'),
      nn.MaxPool2D(pool_size=2,strides=2),
      nn.Dense(120, activation='relu'),
      nn.Dense(84, activation='relu'),
      nn.Dense(2))
    
    net.initialize(init=init.Xavier())
    
    # choose and set loss_fn for a classification task
    loss_fn = gluon.loss.SoftmaxCrossEntropyLoss()
    
    return net, loss_fn

n, loss_fn = get_network()

assert isinstance(n[0], nn.Conv2D)
assert isinstance(n[2], nn.Conv2D)
assert isinstance(n[1], nn.MaxPool2D)
assert isinstance(n[3], nn.MaxPool2D)

for l in n[-3:]:
    assert isinstance(l, nn.Dense)

Also, here is an example code of training LeNet with FashionMNIST

Mr. For Example
  • 4,173
  • 1
  • 9
  • 19