Quickstart#

Here is a simple example of how to use TensorPlay to define a neural network and train it.

import tensorplay as tp
import tensorplay.nn as nn
import tensorplay.optim as optim

# Define a simple network
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(10, 20)
        self.fc2 = nn.Linear(20, 1)

    def forward(self, x):
        x = tp.relu(self.fc1(x))
        return self.fc2(x)

# Initialize model, loss, and optimizer
model = Net()
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# Dummy data
input = tp.randn(32, 10)
target = tp.randn(32, 1)

# Training loop
for epoch in range(100):
    optimizer.zero_grad()
    output = model(input)
    loss = criterion(output, target)
    loss.backward()
    optimizer.step()

    if epoch % 10 == 0:
        print(f"Epoch {epoch}, Loss: {loss.item()}")