2025 USA-NA-AIO Round 1, Problem 2, Part 12

Part 12 (5 points, coding task)

After building our deep neural network architecture in Part 11 and before using it to train our model, we need to prepare our training dataset.

Let us look at a simple application of deep neural network in studying harmonic motion in physics.

Write code to construct the following training dataset:

  • Use sample_size to store the number of samples. Set the value as 1000.

  • Define x_train as a tensor whose shape is (sample_size,) and the value on each entry is uniformly drawn between 0 and 1.

  • Define y_train as a tensor whose values are obtained from the following element-wise mapping from x_train:

    y = \sin \left( 2 \pi x \right) + 0.1 \cdot \mathcal N \left( 0, 1 \right) ,

    where \mathcal N \left( 0, 1 \right) is a standard normal random variable.

  • Print the dimensions of x_train and y_train.

  • Print the shapes of x_train and y_train.

### WRITE YOUR SOLUTION HERE ###

sample_size = 1000
x_train = torch.rand(sample_size,)
y_train = torch.sin(2 * np.pi * x_train) + 0.1 * torch.randn_like(x_train)

print(x_train.ndim)
print(y_train.ndim)

print(x_train.shape)
print(y_train.shape)

""" END OF THIS PART """