<commit_before>'''Simple classifier model

'''

import logging

import torch
import torch.nn as nn
import torch.nn.functional as F

from .modules.convnets import SimpleConvEncoder as Net


logger = logging.getLogger('cortex.models' + __name__)

classifier_args_ = dict(dim_h=256, batch_norm=True, dropout=0.5, nonlinearity='ReLU',
                        f_size=4, stride=2, pad=1, min_dim=4)

DEFAULTS = dict(
    data=dict(batch_size=128),
    optimizer=dict(
        optimizer='Adam',
        learning_rate=1e-4,
    ),
    model=dict(),
    procedures=dict(criterion=nn.CrossEntropyLoss()),
    train=dict(
        epochs=200,
        summary_updates=100,
        archive_every=10
    )
)


def classify(nets, inputs, criterion=None):
    net = nets['classifier']
    images = inputs['images']
    targets = inputs['targets']
    outputs = net(images, nonlinearity=F.log_softmax)
    loss = criterion(outputs, targets)
    _, predicted = torch.max(outputs.data, 1)
    correct = 100. * predicted.eq(targets.data).cpu().sum() / targets.size(0)
    return loss, dict(loss=loss.data[0], accuracy=correct), None, 'accuracy'


def build_model(data_handler, classifier_args=None):
    classifier_args = classifier_args or {}
    args = classifier_args_
    args.update(**classifier_args)

    shape = data_handler.get_dims('x', 'y', 'c')
    dim_l = data_handler.get_dims('labels')[0]
    net = Net(shape, dim_out=dim_l, **args)
    return dict(classifier=net), classify<commit_msg>updating the classifier with more options<commit_after>'''Simple classifier model

'''

import logging

import torch
import torch.nn as nn
import torch.nn.functional as F


logger = logging.getLogger('cortex.models' + __name__)

resnet_args_ = dict(dim_h=64, batch_norm=True, f_size=3, n_steps=4)
mnist_args_ = dict(dim_h=64, batch_norm=True, f_size=5, pad=2, stride=2, min_dim=7, nonlinearity='LeakyReLU')
convnet_args_ = dict(dim_h=64, batch_norm=True, n_steps=3, nonlinearity='LeakyReLU')


DEFAULTS = dict(
    data=dict(batch_size=128),
    optimizer=dict(
        optimizer='Adam',
        learning_rate=1e-4,
    ),
    model=dict(dropout=0.2, model_type='convnet'),
    procedures=dict(criterion=nn.CrossEntropyLoss()),
    train=dict(
        epochs=200,
        summary_updates=100,
        archive_every=10
    )
)


def classify(nets, inputs, criterion=None):
    net = nets['classifier']
    images = inputs['images']
    targets = inputs['targets']
    outputs = net(images, nonlinearity=F.log_softmax)
    loss = criterion(outputs, targets)
    _, predicted = torch.max(outputs.data, 1)
    correct = 100. * predicted.eq(targets.data).cpu().sum() / targets.size(0)
    return loss, dict(loss=loss.data[0], accuracy=correct), None, 'accuracy'


def build_model(data_handler, model_type='convnet', dropout=0.2, classifier_args=None):
    classifier_args = classifier_args or {}
    shape = data_handler.get_dims('x', 'y', 'c')
    dim_l = data_handler.get_dims('labels')[0]

    if model_type == 'resnet':
        from .modules.resnets import ResEncoder as Encoder
        args = resnet_args_
    elif model_type == 'convnet':
        from .modules.convnets import SimpleConvEncoder as Encoder
        args = convnet_args_
    elif model_type == 'mnist':
        from .modules.convnets import SimpleConvEncoder as Encoder
        args = mnist_args_
    else:
        raise NotImplementedError(model_type)

    args.update(**classifier_args)

    if shape[0] == 64:
        args['n_steps'] = 4

    net = Encoder(shape, dim_out=dim_l, dropout=dropout, **args)
    return dict(classifier=net), classify
{'fraction_non_alphanumeric': 0.128026718619538, 'fraction_numerical': 0.019482326746451433, 'mean_word_length': 3.8502024291497974, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5790879', 'n_tokens_mistral': 1275, 'n_tokens_neox': 1193, 'n_words': 242}