2026 USAAIO Round 1 Sample problems, Problem 11

Problem 11

Use PyTorch to build a multi-layer perceptron model that does the following inference.

On a 2-dim plane, consider a triangle enclosed by (0, 0), (1, 0), and (0, 2).

For your model, each input sample is a data point on a 2-dim plane. The output value is 1 if this data point is an interior point of this triangle and 0 if it is not.

Your model architecture and parameter values shall be ready for doing this inference. That is, there is no training in this problem. While doing inference, the input is a tensor with shape (B, 2), where B is a batch size.

Since this problem does not require you to do training, please feel free to use the following threshold function as your activation function:

\Theta (x) = \begin{cases} 1 & \text{if } x \geq 0 \\ 0 & \text{if } x < 0 \end{cases}
1 Like
import torch
import torch.nn as nn

class Step(nn.Module):
    def forward(self, x):
        return (x >= 0).float()
class TriangleNet(nn.Module):
    def __init__(self):
        super().__init__()
        
        # First layer: 3 linear constraints
        self.linear = nn.Linear(2, 3, bias=True)
        self.step = Step()
        
        # Manually set weights
        with torch.no_grad():
            self.linear.weight[:] = torch.tensor([
                [ 1.0,  0.0],   # x >= 0
                [ 0.0,  1.0],   # y >= 0
                [-2.0, -1.0]    # 2 - 2x - y >= 0
            ])
            self.linear.bias[:] = torch.tensor([0.0, 0.0, 2.0])
    
    def forward(self, x):
        h = self.step(self.linear(x))     # shape (B,3)
        return torch.prod(h, dim=1)       # AND operation
model = TriangleNet()

points = torch.tensor([
    [0.2, 0.3],   # inside
    [1.0, 1.0],   # outside
    [0.5, 0.5],   # inside
    [-0.1, 0.2]   # outside
])

print(model(points))
1 Like