Beispiel #1
0
def test_CrossEntropyLoss_get_layer_method_with_default_parameters():
    x = CrossEntropyLoss()

    details = x.get_loss_function()

    assert isinstance(details, dict) is True

    assert issubclass(details["loss_function"], _CrossEntropyLoss) is True

    assert isinstance(details["keyword_arguments"], dict) is True

    assert details["keyword_arguments"]["weight"] is None

    assert details["keyword_arguments"]["reduction"] == "mean"

    assert details["keyword_arguments"]["ignore_index"] == -100
Beispiel #2
0
def test_cce_get_layer_method(weight, reduction, ignore_index):
    x = CrossEntropyLoss(weight=weight,
                         reduction=reduction,
                         ignore_index=ignore_index)

    details = x.get_loss_function()

    assert isinstance(details, dict) == True

    assert issubclass(details["loss_function"], _CrossEntropyLoss) == True

    assert isinstance(details["keyword_arguments"], dict) == True

    assert torch.all(
        torch.eq(details["keyword_arguments"]["weight"],
                 torch.tensor(weight).float())) == True

    assert details["keyword_arguments"]["reduction"] == reduction

    assert details["keyword_arguments"]["ignore_index"] == ignore_index
Beispiel #3
0
# Create a Sequential model Instance
model = Sequential()

#Build your network
model.add(Conv2D(input_shape=(1, 28, 28), filters=128, kernel_size=3))
model.add(ReLU())
model.add(Conv2D(filters=64, kernel_size=3))
model.add(ReLU())
model.add(Conv2D(filters=32, kernel_size=3))
model.add(ReLU())
model.add(Flatten())
model.add(Dense(n_nodes=10))

model.build()
model.compile(optimizer=SGD(),
              loss_function=CrossEntropyLoss(),
              metrics=["accuracy"])
print(model.summary())

#Get the MNIST dataset
train_set = torchvision.datasets.MNIST(root='./data',
                                       train=True,
                                       download=True,
                                       transform=transforms.Compose(
                                           [transforms.ToTensor()]))

# Load the dataset from pytorch's Dataloader function
train_loader = torch.utils.data.DataLoader(train_set, batch_size=1000)
#Get the data
mnist_data = next(iter(train_loader))
#Split into train and test set
Beispiel #4
0
def test_cce_should_throw_value_error(weight, reduction, ignore_index):
    with pytest.raises(ValueError) as ex:
        x = CrossEntropyLoss(weight=weight,
                             reduction=reduction,
                             ignore_index=ignore_index)
Beispiel #5
0
model.add(Conv2D(filters=384, kernel_size=3, stride=1, padding=1))
model.add(ReLU())
model.add(Conv2D(filters=256, kernel_size=3, stride=1, padding=1))
model.add(ReLU())
model.add(MaxPool2D(kernel_size=3, stride=2))
model.add(ReLU())
model.add(Flatten())
model.add(Dense(n_nodes=4096))
model.add(ReLU())
model.add(Dense(n_nodes=4096))
model.add(ReLU())
model.add(Dense(n_nodes=10))
model.add(Softmax())

model.build()
model.compile(optimizer=Adam(), loss_function=CrossEntropyLoss(), metrics=["accuracy"])
print(model.summary())

# Get the training Data
train_set = datasets.MNIST(
    root='./data'
    ,train=True
    ,download=True
    ,transform=transforms.Compose([
        transforms.CenterCrop(224),
        transforms.ToTensor()
    ])
)

# Load the dataset from pytorch's Dataloader function
train_loader  = torch.utils.data.DataLoader(train_set, batch_size=1000)