def train(train_iter, test_iter, net, loss, optimizer, device, num_epochs):
    net = net.to(device)
    print("training on ", device)
    batch_count = 0
    for epoch in range(num_epochs):
        train_l_sum, train_acc_sum, n, start = 0.0, 0.0, 0, time.time()
        for X, y in train_iter:
            X = X.to(device)
            y = y.to(device)
            y_hat = net(X)
            # print(y_hat, y_hat.size())  # 256*10
            # print('-'*100)
            # print(y, y.shape)   # 256
            l = loss(y_hat, y)
            optimizer.zero_grad()
            l.backward()
            optimizer.step()
            train_l_sum += l.cpu().item()
            train_acc_sum += (y_hat.argmax(dim=1) == y).sum().cpu().item()
            # argmax(dim=1) 返回每行最大值的索引,(y_hat.argmax(dim=1) == y) ->[True, False,....], sum()->True+False=1
            n += y.shape[0]
            batch_count += 1
        test_acc = d2l.evaluate_accuracy(test_iter, net)
        print(
            'epoch %d, loss %.4f, train acc %.3f, test acc %.3f, time %.1f sec'
            % (epoch + 1, train_l_sum / batch_count, train_acc_sum / n,
               test_acc, time.time() - start))
Ejemplo n.º 2
0
def train_ch5(net,
              train_iter,
              test_iter,
              criterion,
              num_epochs,
              batch_size,
              device,
              lr=None):
    """Train and evaluate a model with CPU or GPU."""
    print('training on', device)
    net.to(device)
    optimizer = optim.SGD(net.parameters(), lr=lr)
    for epoch in range(num_epochs):
        train_l_sum = torch.tensor([0.0], dtype=torch.float32, device=device)
        train_acc_sum = torch.tensor([0.0], dtype=torch.float32, device=device)
        n, start = 0, time.time()
        for X, y in train_iter:
            net.train()
            optimizer.zero_grad()
            X, y = X.to(device), y.to(device)
            y_hat = net(X)
            loss = criterion(y_hat, y)
            loss.backward()
            optimizer.step()

            with torch.no_grad():
                y = y.long()
                train_l_sum += loss.float()
                train_acc_sum += (torch.sum(
                    (torch.argmax(y_hat, dim=1) == y))).float()
                n += y.shape[0]
        test_acc = d2l.evaluate_accuracy(test_iter, net, device)
        print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f, '
              'time %.1f sec' % (epoch + 1, train_l_sum / n, train_acc_sum / n,
                                 test_acc, time.time() - start))
Ejemplo n.º 3
0
def train_ch3(net,
              train_iter,
              test_iter,
              loss,
              num_epochs,
              batch_size,
              params=None,
              lr=None,
              optimizer=None):
    for epoch in range(num_epochs):
        train_l_sum, train_acc_sum, n = 0.0, 0.0, 0
        for X, y in train_iter:
            y_hat = net(X)
            l = loss(y_hat, y).sum()

            if optimizer is not None:
                optimizer.zero_grad()
            elif params is not None and params[0].grad is not None:
                for param in params:
                    param.grad.data.zero_()

            l.backward()
            if optimizer is None:
                d2l.sgd(params, lr, batch_size)
            else:
                optimizer.step()

            train_l_sum += l.item()
            train_acc_sum += (y_hat.argmax(dim=1) == y).sum().item()
            n += y.shape[0]

        test_acc = d2l.evaluate_accuracy(test_iter, net)
        print('epoch %d, loss %.4f, train_acc %.3f, test_acc %.3f' %
              (epoch + 1, train_l_sum / n, train_acc_sum / n, test_acc))
Ejemplo n.º 4
0
def train(net, train_iter, test_iter, batch_size, optimizer, device,
          num_epochs):
    net = net.to(device)
    print("training on ", device)
    loss = torch.nn.CrossEntropyLoss()
    batch_count = 0
    for epoch in range(num_epochs):
        train_loss_sum, train_acc_sum, n, start = 0.0, 0.0, 0, time.time()
        # iterate during each epoch
        for X, y in train_iter:
            X = X.to(device)
            y = y.to(device)
            y_hat = net(X)
            l = loss(y_hat, y)  # calc loss
            optimizer.zero_grad()  # clear grad. info
            l.backward()  # backward propagation
            optimizer.step()  # optimization
            train_loss_sum += l.cpu().item()  # accumulate training loss
            # accumulate training accurate predicted samples
            train_acc_sum += (y_hat.argmax(dim=1) == y).sum().cpu().item()
            n += y.shape[
                0]  # accumulate total number of samples during training process
            batch_count += 1  # number of batch counts
        # evaluate after one epoch's training is done
        test_acc = d2l.evaluate_accuracy(test_iter, net)
        print(
            'epoch %d, loss %.4f, train acc %.3f, test acc %.3f, time %.1f sec'
            % (epoch + 1, train_loss_sum / batch_count, train_acc_sum / n,
               test_acc, time.time() - start))
Ejemplo n.º 5
0
def train(train_iter, test_iter, net, loss, optimizer, device, num_epochs):
    net = net.to(device)
    print('training on', device)
    batch_count = 0
    for epoch in range(num_epochs):
        train_l_sum, train_acc_sum, n, start = 0.0, 0.0, 0, time.time()
        for X, y in train_iter:
            X = X.to(device)
            y = y.to(device)
            y_hat = net(X)
            l = loss(y_hat, y)
            optimizer.zero_grad()
            l.backward()
            optimizer.step()
            train_l_sum += l.cpu().item()
            train_acc_sum += (y_hat.argmax(dim=1) == y).sum().cpu().item()
            n += y.shape[0]
            batch_count += 1
        test_acc = d2l.evaluate_accuracy(test_iter, net)
        print(
            'epoch %d, loss %.4f, train acc %.3f, test acc %.f, time %.1f sec'
            % (epoch + 1, train_l_sum / batch_count, train_acc_sum / n,
               test_acc, time.time() - start))
Ejemplo n.º 6
0
    def forward(self, x):  # x-shape:(batch_size, 1, 28, 28)
        x = self.output(x.view(x.shape[0], n_features))  # x-shape:(batch_size, n_features)
        return x


net = Net(n_features, n_outputs)

# 交叉熵损失函数
loss = nn.CrossEntropyLoss()

# 优化器
optimizer = torch.optim.SGD(net.parameters(), lr=0.1)

# 训练
n_epochs = 100
start = time()

for epoch in range(n_epochs):
    train_l_sum, train_acc_sum, n = 0.0, 0.0, 0
    for X, y in train_iter:
        y_hat = net(X)
        l = loss(y_hat, y).sum()
        optimizer.zero_grad()
        l.backward()
        optimizer.step()
        train_l_sum += l.item()
        train_acc_sum += (y_hat.argmax(dim=1) == y).sum().item()
        n += y.shape[0]
    test_acc = d2l.evaluate_accuracy(test_iter, net)
    print('epoch:%d | loss:%.4f | train acc:%.3f | test acc:%.3f | time:%.2f'
          % (epoch + 1, train_l_sum / n, train_acc_sum / n, test_acc, time()-start))
Ejemplo n.º 7
0
    return softmax(torch.mm(X.view((-1, num_inputs)), W) + b)


#%%定义损失函数
y_hat = torch.tensor([[0.1, 0.3, 0.6], [0.3, 0.2, 0.5]])
y = torch.LongTensor([0, 2])
y_hat.gather(1, y.view(-1, 1))


def cross_entropy(y_hat, y):
    return -torch.log(y_hat.gather(1, y.view(-1, 1)))


#%%计算分类准确率
def accuracy(y_hat, y):
    return (y_hat.argmax(dim=1) == y).float().mean().item()


print(accuracy(y_hat, y))
print(d2l.evaluate_accuracy(test_iter, net))
#%%训练模型
num_epochs, lr = 5, 0.1
d2l.train_ch3(net, train_iter, test_iter, cross_entropy, num_epochs,
              batch_size, [W, b], lr)
#%%预测
X, y = iter(test_iter).next()
true_labels = d2l.get_fashion_mnist_labels(y.numpy())
pred_labels = d2l.get_fashion_mnist_labels(net(X).argmax(dim=1).numpy())
titles = [true + '\n' + pred for true, pred in zip(true_labels, pred_labels)]
d2l.show_fashion_mnist(X[0:9], titles[0:9])
Ejemplo n.º 8
0
sys.path.append("..")
import d2lzh_pytorch as d2l

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])  #默认的值

test_augs = transforms.Compose([
    transforms.Resize(size=256),
    transforms.CenterCrop(size=224),
    transforms.ToTensor(), normalize
])

test_iter = DataLoader(ImageFolder("testimg", transform=test_augs), 1)

# PATH = '1591342359.343528.pth'
# PATH = '1591345605.9707272.pth'
# model = models.resnet18(pretrained=False, num_classes=2)
# model.load_state_dict(torch.load(PATH))

# with torch.no_grad():
#     # for X, y in test_iter:
#     #     outputs = scratch_net(X.to(device))
#     #
#     #     # outputs = torch.squeeze(outputs.view(1,-1))
#     #     print(outputs)
test_acc = d2l.evaluate_accuracy(test_iter, scratch_net)
print('  test acc %.3f ' % (test_acc))
Ejemplo n.º 9
0
    transforms.ToTensor(), normalize
])

test_iter = DataLoader(ImageFolder("testimg", transform=test_augs), 1)

PATH = '1591349252.4838984.pth'
# PATH = '1591345605.9707272.pth'
model = models.resnet18(pretrained=False, num_classes=2)
model.load_state_dict(torch.load(PATH))

net = model
with torch.no_grad():
    device = None
    for X, y in test_iter:
        if isinstance(net, torch.nn.Module):
            net.eval()  # 评估模式, 这会关闭dropout
            outputs = net(X.to(device))
            print(outputs, '0')
            net.train()  # 改回训练模式
        else:  # 自定义的模型
            if ('is_training'
                    in net.__code__.co_varnames):  # 如果有is_training这个参数
                # 将is_training设置成False
                outputs = net(X, is_training=False)
                print(outputs, '1')
            else:
                outputs = net(X)
                print(outputs, '2')
    test_acc = d2l.evaluate_accuracy(test_iter, model)
    print('  test acc %.3f ' % (test_acc))