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:
- The
import torch
statement imports the PyTorch library. - The
inputs
andoutputs
variables define the inputs and expected outputs for the XOR gate. - 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. - The
criterion
andoptimizer
variables define the loss and optimization functions used to train the model. - The
for
loop iterates 1000 times and trains the model using the inputs and outputs. - The
print
statement prints the output of the model.
Helpful links
More of Python Pytorch
- How do I check the Python version requirements for PyTorch?
- How can I use Python, PyTorch, and YOLOv5 to build an object detection model?
- How can I install Python PyTorch on Ubuntu using ROCm?
- How do I use Pytorch with Python 3.11 on Windows?
- How can I use Yolov5 with PyTorch?
- How can I use Python and PyTorch to parse XML files?
- How do I use PyTorch with Python version 3.11?
- How can I use Python and PyTorch to create a U-Net architecture?
- How do I create a Python PyTorch tutorial?
- How can I use the Softmax function in Python with PyTorch?
See more codes...