Two outputs with same inputs for torch.sin()

In the below code, y and y2 plot differently even though they get same input, why is that?

import torch
import matplotlib.pyplot as plt

torch.manual_seed(42)

# Data, inputs and outputs
x_rand = torch.randint(-1000, 5000, (10000, 1), dtype=torch.float)

# Fn 1
x = 0.1 * x_rand
y = torch.sin(0.5 * x)

# Fn 2
x2 = 0.5 * 0.1 * x_rand
y2 = torch.sin(x2)

# Sorting
x_plt, index = torch.sort(x, dim=0)
y_plt = torch.gather(y, dim=0, index=index)

x2_plt, index = torch.sort(x2, dim=0)
y2_plt = torch.gather(y2, dim=0, index=index)

# Ploting
plt.plot(x_plt.numpy(), y_plt.numpy())
plt.plot(x2_plt.numpy(), y2_plt.numpy())
plt.xlim(-20, 20)

image

You are using different x axis values and are thus moving the smaller sine wave. Use the same axis and you will see the outputs overlap:

plt.plot(x_plt.numpy(), y_plt.numpy())
plt.plot(x_plt.numpy(), y2_plt.numpy())

You can also check if by computing the error e.g. via (y - y2).abs().max() which is 0.

1 Like

I feel dumb for looking at this problem for hours, Thank you for taking time to answer this.