Search code examples
pythonnumpytensorflow

Why does order of the data matter for neural network?


Recently, I discovered the really weird behaviour of my an AI model. I wanted to build AI model that would try and guess the implicit functions based on the data I gave it. For example, the equation of the flower is:

enter image description here

And this is how I wrote it in the Numpy array:

K = 0.75
a = 9

def f(x):
    return x - 2 - 2*np.floor((x - 1)/2)

def r1(x):
    return abs(6 * f(a * K * x / 2 / np.pi)) - a/2
    
t = np.linspace(0, 17 * math.pi, 1000)
x = np.cos(t) * r1(t)
y = np.sin(t) * r1(t)

points = np.vstack((x, y)).T

After that, I tried to experiment a bit and allowed my AI to actually try and guess the shape of this flower! At first try, it actually got it written.

Here it is:

enter image description here

Well, I got a great example. After that, I tried to experiment and checked what would happen if I shuffled the point array, and I completely got devasting results!

enter image description here

And I couldn't explain why the order of cartesian coordinates mattered to the approximate flower implicit function. Can anyone explain it?

Here is the code for AI.

# Define the neural network model
model = Sequential()
model.add(Dense(128, input_dim=2, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(2, activation='linear'))

# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')

# Train the model
model.fit(points, points, epochs=100, batch_size=32)


Solution

  • Actually, it's a plotting issue. The order in a plot matters while the one of a scatter don't :

    enter image description here

    import matplotlib.pyplot as plt
    
    shuffled_points = np.random.permutation(points)
    
    plt.scatter(*points.T)          # in .scatter()-method POINT-s are depicted
    plt.scatter(*shuffled_points.T) # in .scatter()-method order does NOT matter
    
    plt.plot(*points.T, color="r")  # in .plot()-method interpolator follows the order
    plt.plot(*shuffled_points.T, color="r") #    method creates a SEQUENCE of LINEs