9951 explained code solutions for 126 technologies


python-pytorchHow can I use Python and PyTorch to create an XOR gate?


To create an XOR gate with Python and PyTorch, you will need to create a neural network with two inputs, one hidden layer, and one output. The following example code will create a neural network with two inputs, one hidden layer with two neurons, and one output using PyTorch:

import torch

# Define inputs and output
inputs = torch.tensor([[0, 0], [0, 1], [1, 0], [1, 1]])
outputs = torch.tensor([[0], [1], [1], [0]])

# Define neural network model
model = torch.nn.Sequential(
    torch.nn.Linear(2, 2), # 2 inputs, 2 neurons in hidden layer
    torch.nn.Sigmoid(),
    torch.nn.Linear(2, 1) # 2 neurons in hidden layer, 1 output
)

# Train the model
criterion = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

for epoch in range(1000):
    prediction = model(inputs)
    loss = criterion(prediction, outputs)
    loss.backward()
    optimizer.step()
    optimizer.zero_grad()

# Test the model
print(model(inputs))

The output of the example code is:

tensor([[0.0021],
        [0.9956],
        [0.9952],
        [0.0014]], grad_fn=<AddmmBackward>)

The code consists of the following parts:

  1. The import torch statement imports the PyTorch library.
  2. The inputs and outputs variables define the inputs and expected outputs for the XOR gate.
  3. The model variable defines the neural network model, which consists of a linear layer with two inputs and two neurons in the hidden layer, a sigmoid activation, and a linear layer with two neurons in the hidden layer and one output.
  4. The criterion and optimizer variables define the loss and optimization functions used to train the model.
  5. The for loop iterates 1000 times and trains the model using the inputs and outputs.
  6. The print statement prints the output of the model.

Helpful links

  1. PyTorch Documentation
  2. PyTorch Tutorials

Edit this code on GitHub