Exemplo n.º 1
0
class Flatten(nn.Module):
    def forward(self, input):
        return input.view(input.size(0), -1)


net = Net()
X = torch.rand(size=(1, 1, 224, 224))
for layer in net.children():
    X = layer(X)
    print(layer.__class__.__name__, 'output shape:\t', X.shape)

lr, num_epochs, batch_size, device = 0.1, 5, 128, d2l.try_gpu()


#Xavier initialization of weights
def init_weights(m):
    if type(m) == nn.Linear or type(m) == nn.Conv2d:
        torch.nn.init.xavier_uniform_(m.weight)


net.apply(init_weights)

#Loading fashion-MNIST data
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)

#criterion
criterion = nn.CrossEntropyLoss()

d2l.train_ch5(net, train_iter, test_iter, criterion, num_epochs, batch_size,
              device, lr)
Exemplo n.º 2
0
import d2l
from mxnet import autograd, gluon, init, np, npx
from mxnet.gluon import nn
npx.set_np()

net = nn.HybridSequential()
net.add(
    nn.Conv2D(channels=6, kernel_size=5, padding=2, activation='sigmoid'),
    nn.AvgPool2D(pool_size=2, strides=2),
    nn.Conv2D(channels=16, kernel_size=5, activation='sigmoid'),
    nn.AvgPool2D(pool_size=2, strides=2),
    # Dense will transform the input of the shape (batch size, channel,
    # height, width) into the input of the shape (batch size,
    # channel * height * width) automatically by default
    nn.Dense(120, activation='sigmoid'),
    nn.Dense(84, activation='sigmoid'),
    nn.Dense(10))

X = np.random.uniform(size=(1, 1, 28, 28))
net.initialize()
net.hybridize()

batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size)

lr, num_epochs = 0.9, 10
d2l.train_ch5(net, train_iter, test_iter, num_epochs, lr)

net.export('test', 10)