human
I am working on the file "Experiments - Tosato.ipynb".
The first task is:
Utilities
Can you write Python code for it?

Give me a the Python code you requested?
assistant
#Here is the Python code you requested.

class Flatten(df.Module):
    def symb_forward(self, symb_in):
        return symb_in.flatten(2)

def mknet(*head, midshape=(5,5), extra=df.Identity()):
    return df.Sequential(                          #     3@46
        df.SpatialConvolutionCUDNN( 3, 24, 3, 3),  # -> 24@44
        df.BatchNormalization(24),
        df.ReLU(),
        df.SpatialConvolutionCUDNN(24, 24, 3, 3),  # -> 24@42
        df.BatchNormalization(24),
        df.SpatialMaxPoolingCUDNN(2, 2),           # -> 24@21
        df.ReLU(),
        df.SpatialConvolutionCUDNN(24, 48, 3, 3),  # -> 48@19
        df.BatchNormalization(48),
        df.ReLU(),
        df.SpatialConvolutionCUDNN(48, 48, 3, 3),  # -> 48@17
        df.BatchNormalization(48),
        df.SpatialMaxPooling(2, 2),                # -> 48@9
        df.ReLU(),
        df.SpatialConvolutionCUDNN(48, 64, 3, 3),  # -> 64@7
        df.BatchNormalization(64),
        df.ReLU(),
        df.SpatialConvolutionCUDNN(64, 64, 3, 3),  # -> 64@5
        df.BatchNormalization(64),
        df.ReLU(),
        extra,
        df.Dropout(0.2),
        Flatten(),
        df.Linear(64*np.prod(midshape), 512),
        df.ReLU(),
        df.Dropout(0.5),
        *head
    )

def mknet_classification(nclass):
    return mknet(
        df.Linear(512, nclass, initW=df.init.const(0)),
        df.SoftMax()
    )

def plotcost(costs):
    fig, ax = plt.subplots()
    line, = ax.plot(1+np.arange(len(costs)), costs, label='Training cost')
    ax.set_xlabel('Epochs')
    ax.set_ylabel('Cost')
    annotline(ax, line, np.min)
    return fig

def dotrain(model, crit, aug, Xtr, ytr, nepochs=50, batchsize=100):
    opt = df.AdaDelta(rho=.95, eps=1e-7, lr=1)

    progress = IntProgress(value=0, min=0, max=nepochs, description='Training:')
    display(progress)

    model.training()

    costs = []
    for e in range(nepochs):
        batchcosts = []
        for Xb, yb in batched(batchsize, Xtr, ytr, shuf=True):
            if aug is not None:
                Xb, yb = aug.augbatch_train(Xb, yb)
           ...