pytorch xor code example
Example: XOR by pytorch
epochs = 2001
steps = X.size(0)
for i in range(epochs):
for j in range(steps):
data_point = np.random.randint(X.size(0))
x_var = Variable(X[data_point], requires_grad=False)
y_var = Variable(Y[data_point], requires_grad=False)
optimizer.zero_grad()
y_hat = model(x_var)
loss = loss_func.forward(y_hat, y_var)
loss.backward()
optimizer.step()
if i % 500 == 0:
print "Epoch: {0}, Loss: {1}, ".format(i, loss.data.numpy()[0])