<jupyter_start><jupyter_text>---<jupyter_code>import torch
from torch import nn, optim 
from torch.nn import functional as F 
from torch.autograd import Variable
X_train = FloatTensor(1000, 2).uniform_(0, 1)
y_label_train = X_train.sub(0.5).pow(2).sum(1).lt(1./2./math.pi).float()
y_train = reshapeLabel(y_label_train)

X_test = FloatTensor(1000, 2).uniform_(0, 1)
y_label_test = X_test.sub(0.5).pow(2).sum(1).lt(1./2./math.pi).float()
y_test = reshapeLabel(y_label_test)
# convert to Variable
X_train, y_train = Variable(X_train), Variable(y_train)
X_test, y_test = Variable(X_test), Variable(y_test)
mu, std = X_train.mean(), X_train.std()
X_train.sub_(mu).div_(std)
X_test.sub_(mu).div_(std);

y_train = y_train.float()
y_test = y_test.float()
train_output = model.forward(X_train)
print("Before Train Error: {:.2%}".format(float(train_output.max(1)[1].ne(y_train.max(1)[1]).sum())/train_output.size(0)))
model = nn.Sequential(
        nn.Linear(2,25),
        nn.ReLU(),
        nn.Linear(25,25),
        nn.ReLU(),
        nn.Linear(25,25),
        nn.ReLU(),
        nn.Linear(25,2),
        nn.Tanh(),
)
%%time
lr = 0.01
batch_size = 50
nb_epochs = 10

criterion = nn.MSELoss()

optimizer = optim.SGD(model.parameters(), lr=lr)

for k in range(nb_epochs):
    print("******** Begin Epoch {} ********\n".format(k))
    for b in range(0, X_train.size(0), batch_size):
        output = model.forward(X_train.narrow(0, b, batch_size)) 
        loss = criterion(output , y_train.narrow(0, b, batch_size))
        error = float(output.max(1)[1].ne(y_train.narrow(0, b, batch_size).max(1)[1]).sum())/output.size(0)
        print("Epoch {} Batch {:2.0f}: {:4.2f}, {:6.2%}".format(k, b/batch_size, loss.data[0], error))
        model.zero_grad()
        loss.backward()
        optimizer.step()
    
    print("\n******** After Epoch {} ********\n".format(k))
    output = model.forward(X_train)
    loss = criterion(output, y_train.float())
    error = float(output.max(1)[1].ne(y_train.max(1)[1]).sum())/output.size(0)
    print("Loss: {:4.2f}, Error: {:6.2%}\n".format(loss.data[0], error))
train_output = model.forward(X_train)
print("Train Error: {:.2%}".format(float(train_output.max(1)[1].ne(y_train.max(1)[1]).sum())/train_output.size(0)))

test_output = model.forward(X_test)
print("Test Error: {:.2%}".format(float(test_output.max(1)[1].ne(y_test.max(1)[1]).sum())/test_output.size(0)))<jupyter_output>Train Error: 3.60%
Test Error: 8.50%