AI for Mechanical Engineering: Dynamics


By Keonhyeok Park
http://iailab.kaist.ac.kr/
Industrial AI Lab at KAIST
In [ ]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.preprocessing.sequence import pad_sequences
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Input, RepeatVector

import os

1. Projectile Motion (Trajectory Prediction)

Explanation of Equations Used

When solving the projectile motion with air resistance, several key equations and principles are used to model the motion accurately. Below are the detailed descriptions of these equations:

Decomposing Initial Velocity

The initial velocity $v_0$ is decomposed into horizontal ($ v_{x0} $) and vertical ($ v_{y0} $) components based on the launch angle $ \theta $:


$$v_{x0} = v_0 cos(\theta)$$

$$v_{y0} = v_0 sin(\theta)$$


Air Resistance (Drag Force)

The drag force $ F_d $ acting on the projectile is given by Newton's drag law, which states that the drag force is proportional to the square of the velocity. The drag force can be calculated as:


$$F_d = \frac{1}{2} C_d \rho A v^2$$


where:

  • $ C_d $ is the drag coefficient
  • $ \rho $ is the air density
  • $ A $ is the cross-sectional area of the projectile
  • $ v $ is the instantaneous velocity of the projectile

Acceleration Components

The horizontal ($ a_x $) and vertical ($ a_y $) components of acceleration due to drag and gravity are given by:


$$\begin{align*} a_x &= -\frac{F_d}{m} \cdot \frac{v_x}{v} \\ a_y &= -g - \frac{F_d}{m} \cdot \frac{v_y}{v} \end{align*}$$


where:

  • $ g $ is the acceleration due to gravity (9.81 m/s²)
  • $ m $ is the mass of the projectile
  • $ v_x $ and $ v_y $ are the horizontal and vertical components of the velocity, respectively

Solving Differential Equations

The projectile motion is governed by the second-order differential equations derived from Newton's second law. These equations are solved numerically using the odeint function from the scipy library. The state vector $[x, y, v_x, v_y]$ is updated at each time step.


Stopping Condition

The simulation time is defined as a range from 0 to a maximum time, divided into a number of time steps (e.g., 10,000). The simulation stops when the vertical position ($ y $) becomes less than or equal to zero, indicating the projectile has hit the ground. The index where this occurs is found and the data up to this point is retained:


$$ \text{ground_hit_index} = \text{np.where}(y < 0)[0][0] $$


By using these equations and principles, we can model the trajectory of a projectile under the influence of gravity and air resistance, providing a realistic simulation of its motion.

In [ ]:
def projectile_derivatives(state, t, mass, drag_coefficient, area, air_density):
    x, y, vx, vy = state

    # Calculate speed
    v = np.sqrt(vx**2 + vy**2)

    # Calculate drag force
    Fd = 0.5 * drag_coefficient * air_density * area * v**2

    # Calculate acceleration
    ax = -Fd * (vx / v) / mass
    ay = -9.81 - (Fd * (vy / v) / mass)

    return [vx, vy, ax, ay]

def projectile_trajectory(v0, theta, mass, drag_coefficient, area, air_density, num_points=1000):
    theta_rad = np.radians(theta)  # Convert angle to radians
    vx0 = v0 * np.cos(theta_rad)  # Initial velocity in x-direction
    vy0 = v0 * np.sin(theta_rad)  # Initial velocity in y-direction

    # Initial state: [x, y, vx, vy]
    initial_state = [0, 0, vx0, vy0]

    # Time parameters
    dt = 0.01  # Time step in seconds
    t = 0  # Initial time

    # Lists to store trajectory points
    x_points = []
    y_points = []

    # Initial state
    state = initial_state

    # Simulation loop
    while state[1] >= 0:
        # Append current position to trajectory
        x_points.append(state[0])
        y_points.append(state[1])

        # Compute derivatives
        derivatives = projectile_derivatives(state, t, mass, drag_coefficient, area, air_density)

        # Update state using Euler's method
        state = [state[i] + dt * derivatives[i] for i in range(4)]

        # Update time
        t += dt

    # Convert lists to numpy arrays
    x_points = np.array(x_points)
    y_points = np.array(y_points)

    # Interpolate to get fixed number of points
    distance = np.linspace(0, x_points[-1], num_points)
    y_interpolated = np.interp(distance, x_points, y_points)

    return distance, y_interpolated

v0 = 1000  # Initial speed in m/s
theta = 80  # Launch angle in degrees
mass = 0.145  # Mass of the projectile in kg (e.g., a baseball)
drag_coefficient = 0.47  # Drag coefficient (typical for a sphere)
area = 0.0042  # Cross-sectional area in m^2 (typical for a baseball)
air_density = 1.225  # Air density in kg/m^3 (at sea level)

x, y = projectile_trajectory(v0, theta, mass, drag_coefficient, area, air_density)

# Plotting the trajectory
plt.figure(figsize=(10, 10))
plt.subplot(211)
plt.plot(x, y, '.')
plt.title("Projectile Trajectory with Air Resistance (sampling)")
plt.xlabel("Distance (m)")
plt.ylabel("Height (m)")
plt.grid(True)
plt.subplot(212)
plt.plot(x, y)
plt.title("Projectile Trajectory with Air Resistance")
plt.xlabel("Distance (m)")
plt.ylabel("Height (m)")
plt.grid(True)
plt.show()
No description has been provided for this image

Lab 1: Projectile Motion with Air Resistance

  • Input: Initial angle ($\theta$), initial velocity ($v$)
  • Output: Trajectory

Projectile Data Generation

  • 1,000 dataset
  • Angles: 10 to 80 degrees
  • Velocities: 50 to 1000 m/s
In [ ]:
def generate_projectile_data(n_samples=1000):
    angles = np.linspace(10, 80, n_samples)
    velocities = np.linspace(50, 1000, n_samples)

    data = []

    for v0, theta in zip(velocities, angles):
        x, y = projectile_trajectory(v0, theta, mass, drag_coefficient, area, air_density)
        trajectory = np.vstack((x, y)).T
        data.append((v0, theta, trajectory))

    return data

data = generate_projectile_data()
In [ ]:
random_idx = np.array([0,100,500])
plt.figure(figsize = (15,8))
plt.title('Velocity: {:.2f} / {:.2f} / {:.2f}\nAngle: {:.2f} / {:.2f} / {:.2f}'.format(data[random_idx[0]][0],data[random_idx[1]][0],data[random_idx[2]][0],
                                                                  data[random_idx[0]][1],data[random_idx[1]][1],data[random_idx[2]][1]),
         fontsize=20)
for idx in random_idx:
    plt.plot(data[idx][2][:,0],data[idx][2][:,1], label = idx, linewidth=3, linestyle='-')
plt.legend(title='Random Index',title_fontsize = 15, fontsize = 15)
Out[ ]:
<matplotlib.legend.Legend at 0x7c0712d45780>
No description has been provided for this image
  • Preprocess the dataset
In [ ]:
def preprocess_data(data):
    max_len = max(len(traj[2]) for traj in data)

    X = []
    y = []

    for v0, theta, trajectory in data:
        input_features = np.array([v0, theta])
        trajectory = np.array(trajectory)
        X.append(input_features)  # Repeat input features to match trajectory length
        y.append(trajectory)

    X = np.array(X)
    y = np.array(y)

    input_scaler = MinMaxScaler(feature_range=(0, 1))
    output_scaler = MinMaxScaler(feature_range=(0, 1))

    # Flatten the input and output arrays for scaling
    X_flattened = X.reshape(-1, X.shape[-1])
    y_flattened = y.reshape(-1, y.shape[-1])

    # Fit and transform the scalers
    X_scaled_flattened = input_scaler.fit_transform(X_flattened)
    y_scaled_flattened = output_scaler.fit_transform(y_flattened)

    # Reshape back to the original shape
    X_scaled = X_scaled_flattened.reshape(X.shape)
    y_scaled = y_scaled_flattened.reshape(y.shape)

    return X_scaled, y_scaled, input_scaler, output_scaler

X_scaled, y_scaled, input_scaler, output_scaler = preprocess_data(data)
# Display shapes to verify
print("X_scaled shape:", X_scaled.shape)
print("y_scaled shape:", y_scaled.shape)
X_scaled shape: (1000, 2)
y_scaled shape: (1000, 1000, 2)

LSTM Model Architecture

In [ ]:
model = Sequential()
model.add(Dense(128, activation='relu', input_shape=(2,)))  # (batch_size, 2) -> (batch_size, 128)
model.add(RepeatVector(y_scaled.shape[1])) # (batch_size, 1000, 128)
model.add(LSTM(128, return_sequences=True)) # (batch_size, 1000, 128)
model.add(Dense(2))  # (batch_size, 1000, 2) == (batch_size, timesteps, features)

# Model compile
model.compile(optimizer='adam', loss='mse')
model.summary()
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense (Dense)               (None, 128)               384       
                                                                 
 repeat_vector (RepeatVecto  (None, 1000, 128)         0         
 r)                                                              
                                                                 
 lstm (LSTM)                 (None, 1000, 128)         131584    
                                                                 
 dense_1 (Dense)             (None, 1000, 2)           258       
                                                                 
=================================================================
Total params: 132226 (516.51 KB)
Trainable params: 132226 (516.51 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
In [ ]:
history = model.fit(X_scaled, y_scaled, epochs=50, batch_size=32, validation_split=0.2)
Epoch 1/50
25/25 [==============================] - 11s 137ms/step - loss: 0.0612 - val_loss: 0.1298
Epoch 2/50
25/25 [==============================] - 2s 93ms/step - loss: 0.0439 - val_loss: 0.0590
Epoch 3/50
25/25 [==============================] - 2s 72ms/step - loss: 0.0421 - val_loss: 0.0679
Epoch 4/50
25/25 [==============================] - 2s 60ms/step - loss: 0.0411 - val_loss: 0.0549
Epoch 5/50
25/25 [==============================] - 1s 54ms/step - loss: 0.0423 - val_loss: 0.0759
Epoch 6/50
25/25 [==============================] - 2s 63ms/step - loss: 0.0430 - val_loss: 0.0638
Epoch 7/50
25/25 [==============================] - 2s 63ms/step - loss: 0.0396 - val_loss: 0.0548
Epoch 8/50
25/25 [==============================] - 1s 40ms/step - loss: 0.0389 - val_loss: 0.0520
Epoch 9/50
25/25 [==============================] - 1s 34ms/step - loss: 0.0380 - val_loss: 0.0512
Epoch 10/50
25/25 [==============================] - 1s 42ms/step - loss: 0.0416 - val_loss: 0.0582
Epoch 11/50
25/25 [==============================] - 1s 45ms/step - loss: 0.0386 - val_loss: 0.0527
Epoch 12/50
25/25 [==============================] - 1s 47ms/step - loss: 0.0370 - val_loss: 0.0519
Epoch 13/50
25/25 [==============================] - 1s 35ms/step - loss: 0.0355 - val_loss: 0.0515
Epoch 14/50
25/25 [==============================] - 1s 33ms/step - loss: 0.0461 - val_loss: 0.0611
Epoch 15/50
25/25 [==============================] - 1s 33ms/step - loss: 0.0419 - val_loss: 0.0577
Epoch 16/50
25/25 [==============================] - 1s 33ms/step - loss: 0.0402 - val_loss: 0.0518
Epoch 17/50
25/25 [==============================] - 1s 33ms/step - loss: 0.0384 - val_loss: 0.0502
Epoch 18/50
25/25 [==============================] - 1s 33ms/step - loss: 0.0373 - val_loss: 0.0501
Epoch 19/50
25/25 [==============================] - 1s 34ms/step - loss: 0.0448 - val_loss: 0.0638
Epoch 20/50
25/25 [==============================] - 1s 34ms/step - loss: 0.0368 - val_loss: 0.0565
Epoch 21/50
25/25 [==============================] - 1s 33ms/step - loss: 0.0369 - val_loss: 0.0480
Epoch 22/50
25/25 [==============================] - 1s 33ms/step - loss: 0.0386 - val_loss: 0.0551
Epoch 23/50
25/25 [==============================] - 1s 33ms/step - loss: 0.0390 - val_loss: 0.0644
Epoch 24/50
25/25 [==============================] - 1s 34ms/step - loss: 0.0364 - val_loss: 0.0515
Epoch 25/50
25/25 [==============================] - 1s 45ms/step - loss: 0.0349 - val_loss: 0.0608
Epoch 26/50
25/25 [==============================] - 1s 46ms/step - loss: 0.0385 - val_loss: 0.0525
Epoch 27/50
25/25 [==============================] - 1s 41ms/step - loss: 0.0409 - val_loss: 0.0562
Epoch 28/50
25/25 [==============================] - 1s 33ms/step - loss: 0.0470 - val_loss: 0.0517
Epoch 29/50
25/25 [==============================] - 1s 33ms/step - loss: 0.0398 - val_loss: 0.0505
Epoch 30/50
25/25 [==============================] - 1s 34ms/step - loss: 0.0383 - val_loss: 0.0471
Epoch 31/50
25/25 [==============================] - 1s 35ms/step - loss: 0.0381 - val_loss: 0.0448
Epoch 32/50
25/25 [==============================] - 1s 44ms/step - loss: 0.0368 - val_loss: 0.0595
Epoch 33/50
25/25 [==============================] - 1s 45ms/step - loss: 0.0397 - val_loss: 0.0518
Epoch 34/50
25/25 [==============================] - 1s 47ms/step - loss: 0.0377 - val_loss: 0.0519
Epoch 35/50
25/25 [==============================] - 1s 33ms/step - loss: 0.0353 - val_loss: 0.0499
Epoch 36/50
25/25 [==============================] - 1s 34ms/step - loss: 0.0434 - val_loss: 0.0757
Epoch 37/50
25/25 [==============================] - 1s 34ms/step - loss: 0.0406 - val_loss: 0.0706
Epoch 38/50
25/25 [==============================] - 1s 42ms/step - loss: 0.0399 - val_loss: 0.0997
Epoch 39/50
25/25 [==============================] - 1s 44ms/step - loss: 0.0444 - val_loss: 0.0607
Epoch 40/50
25/25 [==============================] - 1s 47ms/step - loss: 0.0412 - val_loss: 0.0589
Epoch 41/50
25/25 [==============================] - 1s 35ms/step - loss: 0.0410 - val_loss: 0.0483
Epoch 42/50
25/25 [==============================] - 1s 34ms/step - loss: 0.0384 - val_loss: 0.0903
Epoch 43/50
25/25 [==============================] - 1s 34ms/step - loss: 0.0381 - val_loss: 0.0760
Epoch 44/50
25/25 [==============================] - 1s 34ms/step - loss: 0.0374 - val_loss: 0.0830
Epoch 45/50
25/25 [==============================] - 1s 33ms/step - loss: 0.0467 - val_loss: 0.0756
Epoch 46/50
25/25 [==============================] - 1s 34ms/step - loss: 0.0410 - val_loss: 0.0714
Epoch 47/50
25/25 [==============================] - 1s 33ms/step - loss: 0.0394 - val_loss: 0.0646
Epoch 48/50
25/25 [==============================] - 1s 34ms/step - loss: 0.0381 - val_loss: 0.0613
Epoch 49/50
25/25 [==============================] - 1s 34ms/step - loss: 0.0362 - val_loss: 0.0517
Epoch 50/50
25/25 [==============================] - 1s 34ms/step - loss: 0.0327 - val_loss: 0.1777
In [ ]:
plt.plot(history.history['loss'], label='train loss')
plt.plot(history.history['val_loss'], label='val loss')
plt.legend()
plt.show()
No description has been provided for this image
In [ ]:
v0_test = 300
theta_test = 45

input_features = np.array([v0_test, theta_test]).reshape(1, -1)
input_scaled = input_scaler.transform(input_features)
prediction_scaled = model.predict(input_scaled)
predicted_trajectory = output_scaler.inverse_transform(prediction_scaled.reshape(-1, 2))

true_distance, true_height = projectile_trajectory(v0_test, theta_test, mass, drag_coefficient, area, air_density)


plt.plot(predicted_trajectory[:, 0], predicted_trajectory[:, 1], label='Predicted Trajectory')


plt.plot(true_distance, true_height, label='True Trajectory', linestyle='dashed')

plt.xlabel('Distance (m)')
plt.ylabel('Height (m)')
plt.title('Predicted vs True Projectile Trajectory')
plt.legend()
plt.show()
1/1 [==============================] - 0s 400ms/step
No description has been provided for this image

Ineffective Cases

  • Input: Initial angles, initial velocities
  • Output: Trajectory

[Reasons for Ineffectiveness]

  • Predicting multiple future time points based only on the current state may lead to insufficient information, especially when the model needs to learn nonlinearities and complex interactions.
  • Long-term predictions based solely on initial conditions are very challenging. Small prediction errors can accumulate over time, leading to significant inaccuracies.

Lab 2: Projectile Motion with Air Resistance

  • Input: Partial Trajectory (10 timesteps)
  • Output: Next Position (1 timestep)

Data Generation

In [ ]:
def projectile_trajectory(v0, theta, mass, drag_coefficient, area, air_density, num_points=100):
    theta_rad = np.radians(theta)  # Convert angle to radians
    vx0 = v0 * np.cos(theta_rad)  # Initial velocity in x-direction
    vy0 = v0 * np.sin(theta_rad)  # Initial velocity in y-direction

    # Initial state: [x, y, vx, vy]
    initial_state = [0, 0, vx0, vy0]

    # Time parameters
    dt = 0.01  # Time step in seconds
    t = 0  # Initial time

    # Lists to store trajectory points
    x_points = []
    y_points = []


    # Initial state
    state = initial_state

    # Simulation loop
    while state[1] >= 0:
        # Append current position to trajectory
        x_points.append(state[0])
        y_points.append(state[1])

        # Compute derivatives
        derivatives = projectile_derivatives(state, t, mass, drag_coefficient, area, air_density)

        # Update state using Euler's method
        state = [state[i] + dt * derivatives[i] for i in range(4)]

        # Update time
        t += dt

    # Convert lists to numpy arrays
    x_points = np.array(x_points)
    y_points = np.array(y_points)

    # Interpolate to get fixed number of points
    distance = np.linspace(0, x_points[-1], num_points)
    y_interpolated = np.interp(distance, x_points, y_points)
    return distance, y_interpolated
In [ ]:
train_window = 10

def create_inout_sequences(input_data, tw):
    inout_seq = []
    L = len(input_data)
    for i in range(90):
        train_seq = input_data[i:i+tw]
        train_label = input_data[i+tw:i+tw+1]
        inout_seq.append((train_seq ,train_label))
    return inout_seq

def make_inout_seq(angle, speed):
    X, Y = projectile_trajectory(speed, angle, mass, drag_coefficient, area, air_density)
    x = np.zeros((len(X), 2))
    for i in range(len(X)):
        x[i, 0] = X[i]
        x[i, 1] = Y[i]
    scaler = MinMaxScaler(feature_range=(0, 1))
    train_data_normalized = scaler.fit_transform(x.reshape(-1, 2))
    train_window = 10
    train_inout_seq = create_inout_sequences(train_data_normalized.reshape(len(X), 2), train_window)
    return scaler, train_inout_seq

scaler, train_inout_seq = make_inout_seq(15, 500)
In [ ]:
X_train = np.array([seq[0] for seq in train_inout_seq])
y_train = np.array([seq[1] for seq in train_inout_seq]).squeeze()
print(X_train.shape, y_train.shape)
(90, 10, 2) (90, 2)
In [ ]:
plt.figure(figsize=(12,5))
for i in range(15):
    scaler_test, test_inout_seq = make_inout_seq(5*i, 500)
    X_test = np.array([seq[0] for seq in test_inout_seq])
    y_test = np.array([seq[1] for seq in test_inout_seq])

    X_test_traj = scaler_test.inverse_transform(X_test[0].reshape(-1,2))
    y_test_traj = scaler_test.inverse_transform(y_test.reshape(-1,2))
    plt.plot(X_test_traj[:,0], X_test_traj[:,1])
    plt.plot(y_test_traj[:,0], y_test_traj[:,1], label = 'actual', color = 'blue')

handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys(), fontsize = 15)
plt.xlim([0,400])
plt.ylim([0,300])
plt.title("Projectile Trajectory with Air Resistance", fontsize = 15)
plt.xlabel("Distance (m)", fontsize = 15)
plt.ylabel("Height (m)", fontsize = 15)
Out[ ]:
Text(0, 0.5, 'Height (m)')
No description has been provided for this image

LSTM Model Architecture

In [ ]:
# Build LSTM model with Keras
model = Sequential()
model.add(LSTM(100, input_shape=(train_window, 2), return_sequences=True))
model.add(LSTM(100))
model.add(Dense(100))
model.add(Dense(2))

model.compile(optimizer='adam', loss='mse')
model.summary()
Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm_1 (LSTM)               (None, 10, 100)           41200     
                                                                 
 lstm_2 (LSTM)               (None, 100)               80400     
                                                                 
 dense_2 (Dense)             (None, 100)               10100     
                                                                 
 dense_3 (Dense)             (None, 2)                 202       
                                                                 
=================================================================
Total params: 131902 (515.24 KB)
Trainable params: 131902 (515.24 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
In [ ]:
history = model.fit(X_train, y_train, epochs=400, batch_size=32)
Epoch 1/400
3/3 [==============================] - 4s 10ms/step - loss: 0.2754
Epoch 2/400
3/3 [==============================] - 0s 8ms/step - loss: 0.0457
Epoch 3/400
3/3 [==============================] - 0s 7ms/step - loss: 0.0700
Epoch 4/400
3/3 [==============================] - 0s 7ms/step - loss: 0.0224
Epoch 5/400
3/3 [==============================] - 0s 7ms/step - loss: 0.0375
Epoch 6/400
3/3 [==============================] - 0s 7ms/step - loss: 0.0283
Epoch 7/400
3/3 [==============================] - 0s 7ms/step - loss: 0.0095
Epoch 8/400
3/3 [==============================] - 0s 7ms/step - loss: 0.0122
Epoch 9/400
3/3 [==============================] - 0s 7ms/step - loss: 0.0089
Epoch 10/400
3/3 [==============================] - 0s 7ms/step - loss: 0.0027
Epoch 11/400
3/3 [==============================] - 0s 7ms/step - loss: 0.0056
Epoch 12/400
3/3 [==============================] - 0s 7ms/step - loss: 0.0048
Epoch 13/400
3/3 [==============================] - 0s 7ms/step - loss: 0.0017
Epoch 14/400
3/3 [==============================] - 0s 7ms/step - loss: 0.0022
Epoch 15/400
3/3 [==============================] - 0s 7ms/step - loss: 0.0017
Epoch 16/400
3/3 [==============================] - 0s 8ms/step - loss: 7.6032e-04
Epoch 17/400
3/3 [==============================] - 0s 9ms/step - loss: 0.0014
Epoch 18/400
3/3 [==============================] - 0s 7ms/step - loss: 8.4534e-04
Epoch 19/400
3/3 [==============================] - 0s 7ms/step - loss: 5.9502e-04
Epoch 20/400
3/3 [==============================] - 0s 7ms/step - loss: 6.6146e-04
Epoch 21/400
3/3 [==============================] - 0s 7ms/step - loss: 3.2361e-04
Epoch 22/400
3/3 [==============================] - 0s 7ms/step - loss: 3.9021e-04
Epoch 23/400
3/3 [==============================] - 0s 8ms/step - loss: 2.5512e-04
Epoch 24/400
3/3 [==============================] - 0s 7ms/step - loss: 1.3374e-04
Epoch 25/400
3/3 [==============================] - 0s 8ms/step - loss: 1.4151e-04
Epoch 26/400
3/3 [==============================] - 0s 7ms/step - loss: 7.0648e-05
Epoch 27/400
3/3 [==============================] - 0s 7ms/step - loss: 9.5035e-05
Epoch 28/400
3/3 [==============================] - 0s 7ms/step - loss: 5.3864e-05
Epoch 29/400
3/3 [==============================] - 0s 9ms/step - loss: 5.5106e-05
Epoch 30/400
3/3 [==============================] - 0s 7ms/step - loss: 4.8803e-05
Epoch 31/400
3/3 [==============================] - 0s 7ms/step - loss: 4.1152e-05
Epoch 32/400
3/3 [==============================] - 0s 7ms/step - loss: 4.0835e-05
Epoch 33/400
3/3 [==============================] - 0s 7ms/step - loss: 3.1359e-05
Epoch 34/400
3/3 [==============================] - 0s 7ms/step - loss: 3.6417e-05
Epoch 35/400
3/3 [==============================] - 0s 7ms/step - loss: 2.7922e-05
Epoch 36/400
3/3 [==============================] - 0s 7ms/step - loss: 3.3740e-05
Epoch 37/400
3/3 [==============================] - 0s 7ms/step - loss: 2.7318e-05
Epoch 38/400
3/3 [==============================] - 0s 7ms/step - loss: 2.5229e-05
Epoch 39/400
3/3 [==============================] - 0s 7ms/step - loss: 2.4533e-05
Epoch 40/400
3/3 [==============================] - 0s 7ms/step - loss: 2.3937e-05
Epoch 41/400
3/3 [==============================] - 0s 7ms/step - loss: 2.3771e-05
Epoch 42/400
3/3 [==============================] - 0s 7ms/step - loss: 2.2462e-05
Epoch 43/400
3/3 [==============================] - 0s 7ms/step - loss: 2.1365e-05
Epoch 44/400
3/3 [==============================] - 0s 7ms/step - loss: 2.1827e-05
Epoch 45/400
3/3 [==============================] - 0s 8ms/step - loss: 2.1915e-05
Epoch 46/400
3/3 [==============================] - 0s 7ms/step - loss: 2.0278e-05
Epoch 47/400
3/3 [==============================] - 0s 7ms/step - loss: 2.1517e-05
Epoch 48/400
3/3 [==============================] - 0s 7ms/step - loss: 2.1503e-05
Epoch 49/400
3/3 [==============================] - 0s 7ms/step - loss: 2.0990e-05
Epoch 50/400
3/3 [==============================] - 0s 8ms/step - loss: 1.9814e-05
Epoch 51/400
3/3 [==============================] - 0s 7ms/step - loss: 1.8129e-05
Epoch 52/400
3/3 [==============================] - 0s 8ms/step - loss: 2.0218e-05
Epoch 53/400
3/3 [==============================] - 0s 8ms/step - loss: 1.7866e-05
Epoch 54/400
3/3 [==============================] - 0s 9ms/step - loss: 2.0469e-05
Epoch 55/400
3/3 [==============================] - 0s 10ms/step - loss: 1.6357e-05
Epoch 56/400
3/3 [==============================] - 0s 11ms/step - loss: 1.6586e-05
Epoch 57/400
3/3 [==============================] - 0s 9ms/step - loss: 1.6716e-05
Epoch 58/400
3/3 [==============================] - 0s 10ms/step - loss: 1.7081e-05
Epoch 59/400
3/3 [==============================] - 0s 9ms/step - loss: 1.9227e-05
Epoch 60/400
3/3 [==============================] - 0s 9ms/step - loss: 2.3657e-05
Epoch 61/400
3/3 [==============================] - 0s 10ms/step - loss: 2.1468e-05
Epoch 62/400
3/3 [==============================] - 0s 11ms/step - loss: 1.5520e-05
Epoch 63/400
3/3 [==============================] - 0s 10ms/step - loss: 1.6634e-05
Epoch 64/400
3/3 [==============================] - 0s 10ms/step - loss: 1.5897e-05
Epoch 65/400
3/3 [==============================] - 0s 9ms/step - loss: 1.5901e-05
Epoch 66/400
3/3 [==============================] - 0s 9ms/step - loss: 1.4008e-05
Epoch 67/400
3/3 [==============================] - 0s 9ms/step - loss: 1.2894e-05
Epoch 68/400
3/3 [==============================] - 0s 10ms/step - loss: 1.2135e-05
Epoch 69/400
3/3 [==============================] - 0s 14ms/step - loss: 1.1874e-05
Epoch 70/400
3/3 [==============================] - 0s 9ms/step - loss: 1.1589e-05
Epoch 71/400
3/3 [==============================] - 0s 9ms/step - loss: 1.1927e-05
Epoch 72/400
3/3 [==============================] - 0s 9ms/step - loss: 1.2147e-05
Epoch 73/400
3/3 [==============================] - 0s 10ms/step - loss: 1.1717e-05
Epoch 74/400
3/3 [==============================] - 0s 9ms/step - loss: 1.1388e-05
Epoch 75/400
3/3 [==============================] - 0s 9ms/step - loss: 1.0980e-05
Epoch 76/400
3/3 [==============================] - 0s 9ms/step - loss: 1.2069e-05
Epoch 77/400
3/3 [==============================] - 0s 9ms/step - loss: 1.0715e-05
Epoch 78/400
3/3 [==============================] - 0s 10ms/step - loss: 1.3065e-05
Epoch 79/400
3/3 [==============================] - 0s 9ms/step - loss: 1.0699e-05
Epoch 80/400
3/3 [==============================] - 0s 9ms/step - loss: 1.1122e-05
Epoch 81/400
3/3 [==============================] - 0s 9ms/step - loss: 1.7421e-05
Epoch 82/400
3/3 [==============================] - 0s 9ms/step - loss: 1.1984e-05
Epoch 83/400
3/3 [==============================] - 0s 9ms/step - loss: 1.2846e-05
Epoch 84/400
3/3 [==============================] - 0s 9ms/step - loss: 1.0822e-05
Epoch 85/400
3/3 [==============================] - 0s 9ms/step - loss: 9.3800e-06
Epoch 86/400
3/3 [==============================] - 0s 9ms/step - loss: 1.0911e-05
Epoch 87/400
3/3 [==============================] - 0s 13ms/step - loss: 1.0150e-05
Epoch 88/400
3/3 [==============================] - 0s 10ms/step - loss: 9.9528e-06
Epoch 89/400
3/3 [==============================] - 0s 10ms/step - loss: 8.3236e-06
Epoch 90/400
3/3 [==============================] - 0s 10ms/step - loss: 8.1879e-06
Epoch 91/400
3/3 [==============================] - 0s 11ms/step - loss: 7.6161e-06
Epoch 92/400
3/3 [==============================] - 0s 11ms/step - loss: 7.5646e-06
Epoch 93/400
3/3 [==============================] - 0s 10ms/step - loss: 7.9415e-06
Epoch 94/400
3/3 [==============================] - 0s 10ms/step - loss: 7.1889e-06
Epoch 95/400
3/3 [==============================] - 0s 9ms/step - loss: 7.2984e-06
Epoch 96/400
3/3 [==============================] - 0s 9ms/step - loss: 7.2101e-06
Epoch 97/400
3/3 [==============================] - 0s 9ms/step - loss: 7.1218e-06
Epoch 98/400
3/3 [==============================] - 0s 9ms/step - loss: 6.4801e-06
Epoch 99/400
3/3 [==============================] - 0s 12ms/step - loss: 6.2027e-06
Epoch 100/400
3/3 [==============================] - 0s 13ms/step - loss: 5.9365e-06
Epoch 101/400
3/3 [==============================] - 0s 10ms/step - loss: 6.0997e-06
Epoch 102/400
3/3 [==============================] - 0s 10ms/step - loss: 5.7745e-06
Epoch 103/400
3/3 [==============================] - 0s 9ms/step - loss: 5.8552e-06
Epoch 104/400
3/3 [==============================] - 0s 9ms/step - loss: 5.5808e-06
Epoch 105/400
3/3 [==============================] - 0s 9ms/step - loss: 5.7799e-06
Epoch 106/400
3/3 [==============================] - 0s 9ms/step - loss: 5.6470e-06
Epoch 107/400
3/3 [==============================] - 0s 9ms/step - loss: 5.2613e-06
Epoch 108/400
3/3 [==============================] - 0s 9ms/step - loss: 5.1164e-06
Epoch 109/400
3/3 [==============================] - 0s 9ms/step - loss: 5.6274e-06
Epoch 110/400
3/3 [==============================] - 0s 9ms/step - loss: 5.6005e-06
Epoch 111/400
3/3 [==============================] - 0s 9ms/step - loss: 6.3889e-06
Epoch 112/400
3/3 [==============================] - 0s 9ms/step - loss: 5.4285e-06
Epoch 113/400
3/3 [==============================] - 0s 9ms/step - loss: 5.0031e-06
Epoch 114/400
3/3 [==============================] - 0s 10ms/step - loss: 5.1342e-06
Epoch 115/400
3/3 [==============================] - 0s 9ms/step - loss: 5.2845e-06
Epoch 116/400
3/3 [==============================] - 0s 8ms/step - loss: 4.8834e-06
Epoch 117/400
3/3 [==============================] - 0s 9ms/step - loss: 4.3960e-06
Epoch 118/400
3/3 [==============================] - 0s 9ms/step - loss: 4.2387e-06
Epoch 119/400
3/3 [==============================] - 0s 9ms/step - loss: 4.0498e-06
Epoch 120/400
3/3 [==============================] - 0s 12ms/step - loss: 4.1375e-06
Epoch 121/400
3/3 [==============================] - 0s 10ms/step - loss: 4.3151e-06
Epoch 122/400
3/3 [==============================] - 0s 11ms/step - loss: 4.2992e-06
Epoch 123/400
3/3 [==============================] - 0s 11ms/step - loss: 4.1030e-06
Epoch 124/400
3/3 [==============================] - 0s 10ms/step - loss: 4.2882e-06
Epoch 125/400
3/3 [==============================] - 0s 10ms/step - loss: 5.6323e-06
Epoch 126/400
3/3 [==============================] - 0s 9ms/step - loss: 3.8697e-06
Epoch 127/400
3/3 [==============================] - 0s 10ms/step - loss: 3.5723e-06
Epoch 128/400
3/3 [==============================] - 0s 11ms/step - loss: 3.4452e-06
Epoch 129/400
3/3 [==============================] - 0s 11ms/step - loss: 3.4537e-06
Epoch 130/400
3/3 [==============================] - 0s 10ms/step - loss: 3.8407e-06
Epoch 131/400
3/3 [==============================] - 0s 9ms/step - loss: 3.5546e-06
Epoch 132/400
3/3 [==============================] - 0s 9ms/step - loss: 3.8565e-06
Epoch 133/400
3/3 [==============================] - 0s 11ms/step - loss: 3.6444e-06
Epoch 134/400
3/3 [==============================] - 0s 10ms/step - loss: 3.4777e-06
Epoch 135/400
3/3 [==============================] - 0s 9ms/step - loss: 3.3452e-06
Epoch 136/400
3/3 [==============================] - 0s 9ms/step - loss: 3.9623e-06
Epoch 137/400
3/3 [==============================] - 0s 9ms/step - loss: 4.4268e-06
Epoch 138/400
3/3 [==============================] - 0s 11ms/step - loss: 4.0654e-06
Epoch 139/400
3/3 [==============================] - 0s 11ms/step - loss: 4.4941e-06
Epoch 140/400
3/3 [==============================] - 0s 9ms/step - loss: 3.1546e-06
Epoch 141/400
3/3 [==============================] - 0s 10ms/step - loss: 3.7683e-06
Epoch 142/400
3/3 [==============================] - 0s 11ms/step - loss: 3.0542e-06
Epoch 143/400
3/3 [==============================] - 0s 10ms/step - loss: 2.8238e-06
Epoch 144/400
3/3 [==============================] - 0s 10ms/step - loss: 3.3415e-06
Epoch 145/400
3/3 [==============================] - 0s 9ms/step - loss: 3.0545e-06
Epoch 146/400
3/3 [==============================] - 0s 10ms/step - loss: 3.3170e-06
Epoch 147/400
3/3 [==============================] - 0s 12ms/step - loss: 2.8700e-06
Epoch 148/400
3/3 [==============================] - 0s 12ms/step - loss: 2.7761e-06
Epoch 149/400
3/3 [==============================] - 0s 11ms/step - loss: 2.7103e-06
Epoch 150/400
3/3 [==============================] - 0s 12ms/step - loss: 2.6704e-06
Epoch 151/400
3/3 [==============================] - 0s 13ms/step - loss: 3.8398e-06
Epoch 152/400
3/3 [==============================] - 0s 8ms/step - loss: 4.1319e-06
Epoch 153/400
3/3 [==============================] - 0s 9ms/step - loss: 2.9868e-06
Epoch 154/400
3/3 [==============================] - 0s 9ms/step - loss: 3.6264e-06
Epoch 155/400
3/3 [==============================] - 0s 9ms/step - loss: 3.8313e-06
Epoch 156/400
3/3 [==============================] - 0s 11ms/step - loss: 4.0362e-06
Epoch 157/400
3/3 [==============================] - 0s 12ms/step - loss: 3.1535e-06
Epoch 158/400
3/3 [==============================] - 0s 11ms/step - loss: 2.8904e-06
Epoch 159/400
3/3 [==============================] - 0s 10ms/step - loss: 3.3380e-06
Epoch 160/400
3/3 [==============================] - 0s 9ms/step - loss: 3.5698e-06
Epoch 161/400
3/3 [==============================] - 0s 9ms/step - loss: 2.6752e-06
Epoch 162/400
3/3 [==============================] - 0s 9ms/step - loss: 2.3060e-06
Epoch 163/400
3/3 [==============================] - 0s 9ms/step - loss: 3.5703e-06
Epoch 164/400
3/3 [==============================] - 0s 9ms/step - loss: 3.3341e-06
Epoch 165/400
3/3 [==============================] - 0s 9ms/step - loss: 2.5389e-06
Epoch 166/400
3/3 [==============================] - 0s 9ms/step - loss: 2.7025e-06
Epoch 167/400
3/3 [==============================] - 0s 11ms/step - loss: 2.2809e-06
Epoch 168/400
3/3 [==============================] - 0s 10ms/step - loss: 2.0970e-06
Epoch 169/400
3/3 [==============================] - 0s 9ms/step - loss: 1.9233e-06
Epoch 170/400
3/3 [==============================] - 0s 9ms/step - loss: 1.8793e-06
Epoch 171/400
3/3 [==============================] - 0s 10ms/step - loss: 2.2439e-06
Epoch 172/400
3/3 [==============================] - 0s 10ms/step - loss: 2.5244e-06
Epoch 173/400
3/3 [==============================] - 0s 9ms/step - loss: 1.9137e-06
Epoch 174/400
3/3 [==============================] - 0s 11ms/step - loss: 2.9053e-06
Epoch 175/400
3/3 [==============================] - 0s 9ms/step - loss: 3.6664e-06
Epoch 176/400
3/3 [==============================] - 0s 10ms/step - loss: 3.0089e-06
Epoch 177/400
3/3 [==============================] - 0s 10ms/step - loss: 2.1626e-06
Epoch 178/400
3/3 [==============================] - 0s 11ms/step - loss: 2.7252e-06
Epoch 179/400
3/3 [==============================] - 0s 11ms/step - loss: 3.5647e-06
Epoch 180/400
3/3 [==============================] - 0s 11ms/step - loss: 2.0251e-06
Epoch 181/400
3/3 [==============================] - 0s 12ms/step - loss: 2.2876e-06
Epoch 182/400
3/3 [==============================] - 0s 11ms/step - loss: 2.1785e-06
Epoch 183/400
3/3 [==============================] - 0s 13ms/step - loss: 2.4774e-06
Epoch 184/400
3/3 [==============================] - 0s 12ms/step - loss: 3.3419e-06
Epoch 185/400
3/3 [==============================] - 0s 13ms/step - loss: 2.4871e-06
Epoch 186/400
3/3 [==============================] - 0s 11ms/step - loss: 2.2646e-06
Epoch 187/400
3/3 [==============================] - 0s 13ms/step - loss: 1.8824e-06
Epoch 188/400
3/3 [==============================] - 0s 12ms/step - loss: 1.7720e-06
Epoch 189/400
3/3 [==============================] - 0s 12ms/step - loss: 2.0210e-06
Epoch 190/400
3/3 [==============================] - 0s 13ms/step - loss: 2.1262e-06
Epoch 191/400
3/3 [==============================] - 0s 8ms/step - loss: 1.5924e-06
Epoch 192/400
3/3 [==============================] - 0s 8ms/step - loss: 2.0233e-06
Epoch 193/400
3/3 [==============================] - 0s 8ms/step - loss: 2.1536e-06
Epoch 194/400
3/3 [==============================] - 0s 7ms/step - loss: 2.2172e-06
Epoch 195/400
3/3 [==============================] - 0s 8ms/step - loss: 2.4603e-06
Epoch 196/400
3/3 [==============================] - 0s 7ms/step - loss: 1.9421e-06
Epoch 197/400
3/3 [==============================] - 0s 8ms/step - loss: 3.2911e-06
Epoch 198/400
3/3 [==============================] - 0s 7ms/step - loss: 4.0038e-06
Epoch 199/400
3/3 [==============================] - 0s 7ms/step - loss: 2.3025e-06
Epoch 200/400
3/3 [==============================] - 0s 10ms/step - loss: 2.0633e-06
Epoch 201/400
3/3 [==============================] - 0s 9ms/step - loss: 2.6885e-06
Epoch 202/400
3/3 [==============================] - 0s 7ms/step - loss: 2.0328e-06
Epoch 203/400
3/3 [==============================] - 0s 7ms/step - loss: 1.4683e-06
Epoch 204/400
3/3 [==============================] - 0s 7ms/step - loss: 1.7798e-06
Epoch 205/400
3/3 [==============================] - 0s 10ms/step - loss: 2.2390e-06
Epoch 206/400
3/3 [==============================] - 0s 8ms/step - loss: 3.3311e-06
Epoch 207/400
3/3 [==============================] - 0s 8ms/step - loss: 2.6762e-06
Epoch 208/400
3/3 [==============================] - 0s 8ms/step - loss: 3.3784e-06
Epoch 209/400
3/3 [==============================] - 0s 7ms/step - loss: 4.5584e-06
Epoch 210/400
3/3 [==============================] - 0s 8ms/step - loss: 4.1900e-06
Epoch 211/400
3/3 [==============================] - 0s 7ms/step - loss: 3.0608e-06
Epoch 212/400
3/3 [==============================] - 0s 7ms/step - loss: 2.2623e-06
Epoch 213/400
3/3 [==============================] - 0s 7ms/step - loss: 2.1514e-06
Epoch 214/400
3/3 [==============================] - 0s 9ms/step - loss: 2.4836e-06
Epoch 215/400
3/3 [==============================] - 0s 7ms/step - loss: 2.8239e-06
Epoch 216/400
3/3 [==============================] - 0s 8ms/step - loss: 4.0723e-06
Epoch 217/400
3/3 [==============================] - 0s 8ms/step - loss: 3.4342e-06
Epoch 218/400
3/3 [==============================] - 0s 7ms/step - loss: 2.8737e-06
Epoch 219/400
3/3 [==============================] - 0s 7ms/step - loss: 2.9675e-06
Epoch 220/400
3/3 [==============================] - 0s 8ms/step - loss: 4.0445e-06
Epoch 221/400
3/3 [==============================] - 0s 8ms/step - loss: 2.0780e-06
Epoch 222/400
3/3 [==============================] - 0s 8ms/step - loss: 1.9986e-06
Epoch 223/400
3/3 [==============================] - 0s 8ms/step - loss: 2.2424e-06
Epoch 224/400
3/3 [==============================] - 0s 7ms/step - loss: 1.6907e-06
Epoch 225/400
3/3 [==============================] - 0s 7ms/step - loss: 1.7528e-06
Epoch 226/400
3/3 [==============================] - 0s 7ms/step - loss: 2.2419e-06
Epoch 227/400
3/3 [==============================] - 0s 9ms/step - loss: 2.1124e-06
Epoch 228/400
3/3 [==============================] - 0s 7ms/step - loss: 1.8432e-06
Epoch 229/400
3/3 [==============================] - 0s 7ms/step - loss: 1.6074e-06
Epoch 230/400
3/3 [==============================] - 0s 7ms/step - loss: 1.3449e-06
Epoch 231/400
3/3 [==============================] - 0s 7ms/step - loss: 1.2514e-06
Epoch 232/400
3/3 [==============================] - 0s 7ms/step - loss: 1.5562e-06
Epoch 233/400
3/3 [==============================] - 0s 8ms/step - loss: 1.5240e-06
Epoch 234/400
3/3 [==============================] - 0s 7ms/step - loss: 1.7097e-06
Epoch 235/400
3/3 [==============================] - 0s 7ms/step - loss: 2.5362e-06
Epoch 236/400
3/3 [==============================] - 0s 7ms/step - loss: 2.4041e-06
Epoch 237/400
3/3 [==============================] - 0s 8ms/step - loss: 1.9567e-06
Epoch 238/400
3/3 [==============================] - 0s 7ms/step - loss: 1.3213e-06
Epoch 239/400
3/3 [==============================] - 0s 7ms/step - loss: 2.2913e-06
Epoch 240/400
3/3 [==============================] - 0s 8ms/step - loss: 2.1645e-06
Epoch 241/400
3/3 [==============================] - 0s 8ms/step - loss: 1.4690e-06
Epoch 242/400
3/3 [==============================] - 0s 7ms/step - loss: 2.1583e-06
Epoch 243/400
3/3 [==============================] - 0s 9ms/step - loss: 1.3944e-06
Epoch 244/400
3/3 [==============================] - 0s 8ms/step - loss: 1.3120e-06
Epoch 245/400
3/3 [==============================] - 0s 8ms/step - loss: 1.5161e-06
Epoch 246/400
3/3 [==============================] - 0s 8ms/step - loss: 1.2940e-06
Epoch 247/400
3/3 [==============================] - 0s 8ms/step - loss: 1.2821e-06
Epoch 248/400
3/3 [==============================] - 0s 9ms/step - loss: 1.2954e-06
Epoch 249/400
3/3 [==============================] - 0s 8ms/step - loss: 1.1436e-06
Epoch 250/400
3/3 [==============================] - 0s 7ms/step - loss: 1.2465e-06
Epoch 251/400
3/3 [==============================] - 0s 7ms/step - loss: 1.3679e-06
Epoch 252/400
3/3 [==============================] - 0s 10ms/step - loss: 1.4495e-06
Epoch 253/400
3/3 [==============================] - 0s 8ms/step - loss: 1.3301e-06
Epoch 254/400
3/3 [==============================] - 0s 8ms/step - loss: 1.5737e-06
Epoch 255/400
3/3 [==============================] - 0s 8ms/step - loss: 1.3919e-06
Epoch 256/400
3/3 [==============================] - 0s 8ms/step - loss: 1.1467e-06
Epoch 257/400
3/3 [==============================] - 0s 7ms/step - loss: 1.3483e-06
Epoch 258/400
3/3 [==============================] - 0s 7ms/step - loss: 2.0196e-06
Epoch 259/400
3/3 [==============================] - 0s 7ms/step - loss: 2.6921e-06
Epoch 260/400
3/3 [==============================] - 0s 7ms/step - loss: 2.2052e-06
Epoch 261/400
3/3 [==============================] - 0s 8ms/step - loss: 1.4483e-06
Epoch 262/400
3/3 [==============================] - 0s 8ms/step - loss: 2.1281e-06
Epoch 263/400
3/3 [==============================] - 0s 8ms/step - loss: 3.3662e-06
Epoch 264/400
3/3 [==============================] - 0s 7ms/step - loss: 3.2618e-06
Epoch 265/400
3/3 [==============================] - 0s 8ms/step - loss: 2.5954e-06
Epoch 266/400
3/3 [==============================] - 0s 7ms/step - loss: 2.4945e-06
Epoch 267/400
3/3 [==============================] - 0s 7ms/step - loss: 3.3547e-06
Epoch 268/400
3/3 [==============================] - 0s 8ms/step - loss: 5.5381e-06
Epoch 269/400
3/3 [==============================] - 0s 8ms/step - loss: 6.5930e-06
Epoch 270/400
3/3 [==============================] - 0s 8ms/step - loss: 1.7580e-06
Epoch 271/400
3/3 [==============================] - 0s 8ms/step - loss: 1.2027e-06
Epoch 272/400
3/3 [==============================] - 0s 11ms/step - loss: 1.2803e-06
Epoch 273/400
3/3 [==============================] - 0s 8ms/step - loss: 1.7832e-06
Epoch 274/400
3/3 [==============================] - 0s 8ms/step - loss: 2.8064e-06
Epoch 275/400
3/3 [==============================] - 0s 7ms/step - loss: 7.0841e-06
Epoch 276/400
3/3 [==============================] - 0s 8ms/step - loss: 7.2662e-06
Epoch 277/400
3/3 [==============================] - 0s 8ms/step - loss: 1.9148e-05
Epoch 278/400
3/3 [==============================] - 0s 9ms/step - loss: 1.1608e-05
Epoch 279/400
3/3 [==============================] - 0s 9ms/step - loss: 1.3601e-05
Epoch 280/400
3/3 [==============================] - 0s 8ms/step - loss: 8.5386e-06
Epoch 281/400
3/3 [==============================] - 0s 8ms/step - loss: 1.0328e-05
Epoch 282/400
3/3 [==============================] - 0s 8ms/step - loss: 1.1991e-05
Epoch 283/400
3/3 [==============================] - 0s 7ms/step - loss: 7.7127e-06
Epoch 284/400
3/3 [==============================] - 0s 7ms/step - loss: 7.0083e-06
Epoch 285/400
3/3 [==============================] - 0s 8ms/step - loss: 1.2078e-05
Epoch 286/400
3/3 [==============================] - 0s 8ms/step - loss: 6.2675e-06
Epoch 287/400
3/3 [==============================] - 0s 14ms/step - loss: 8.3599e-06
Epoch 288/400
3/3 [==============================] - 0s 11ms/step - loss: 5.8839e-06
Epoch 289/400
3/3 [==============================] - 0s 7ms/step - loss: 3.6037e-06
Epoch 290/400
3/3 [==============================] - 0s 8ms/step - loss: 1.3230e-06
Epoch 291/400
3/3 [==============================] - 0s 8ms/step - loss: 1.4901e-06
Epoch 292/400
3/3 [==============================] - 0s 9ms/step - loss: 2.1717e-06
Epoch 293/400
3/3 [==============================] - 0s 8ms/step - loss: 1.9586e-06
Epoch 294/400
3/3 [==============================] - 0s 8ms/step - loss: 1.5854e-06
Epoch 295/400
3/3 [==============================] - 0s 8ms/step - loss: 1.0590e-06
Epoch 296/400
3/3 [==============================] - 0s 8ms/step - loss: 1.0939e-06
Epoch 297/400
3/3 [==============================] - 0s 9ms/step - loss: 1.0990e-06
Epoch 298/400
3/3 [==============================] - 0s 12ms/step - loss: 1.1627e-06
Epoch 299/400
3/3 [==============================] - 0s 8ms/step - loss: 1.3243e-06
Epoch 300/400
3/3 [==============================] - 0s 8ms/step - loss: 1.3069e-06
Epoch 301/400
3/3 [==============================] - 0s 7ms/step - loss: 1.1750e-06
Epoch 302/400
3/3 [==============================] - 0s 8ms/step - loss: 1.2032e-06
Epoch 303/400
3/3 [==============================] - 0s 9ms/step - loss: 2.5275e-06
Epoch 304/400
3/3 [==============================] - 0s 8ms/step - loss: 1.7112e-06
Epoch 305/400
3/3 [==============================] - 0s 7ms/step - loss: 2.8385e-06
Epoch 306/400
3/3 [==============================] - 0s 8ms/step - loss: 4.4028e-06
Epoch 307/400
3/3 [==============================] - 0s 9ms/step - loss: 5.0815e-06
Epoch 308/400
3/3 [==============================] - 0s 8ms/step - loss: 7.5952e-06
Epoch 309/400
3/3 [==============================] - 0s 8ms/step - loss: 1.1201e-05
Epoch 310/400
3/3 [==============================] - 0s 8ms/step - loss: 1.7898e-05
Epoch 311/400
3/3 [==============================] - 0s 7ms/step - loss: 9.6612e-06
Epoch 312/400
3/3 [==============================] - 0s 7ms/step - loss: 1.1965e-05
Epoch 313/400
3/3 [==============================] - 0s 8ms/step - loss: 1.8997e-05
Epoch 314/400
3/3 [==============================] - 0s 8ms/step - loss: 2.2046e-05
Epoch 315/400
3/3 [==============================] - 0s 7ms/step - loss: 4.5703e-05
Epoch 316/400
3/3 [==============================] - 0s 7ms/step - loss: 3.1477e-05
Epoch 317/400
3/3 [==============================] - 0s 7ms/step - loss: 2.9032e-05
Epoch 318/400
3/3 [==============================] - 0s 7ms/step - loss: 1.7805e-05
Epoch 319/400
3/3 [==============================] - 0s 7ms/step - loss: 2.1616e-05
Epoch 320/400
3/3 [==============================] - 0s 9ms/step - loss: 1.8524e-05
Epoch 321/400
3/3 [==============================] - 0s 8ms/step - loss: 1.7145e-05
Epoch 322/400
3/3 [==============================] - 0s 8ms/step - loss: 1.3309e-05
Epoch 323/400
3/3 [==============================] - 0s 10ms/step - loss: 5.5201e-06
Epoch 324/400
3/3 [==============================] - 0s 9ms/step - loss: 6.8558e-06
Epoch 325/400
3/3 [==============================] - 0s 8ms/step - loss: 1.9082e-06
Epoch 326/400
3/3 [==============================] - 0s 12ms/step - loss: 2.2341e-06
Epoch 327/400
3/3 [==============================] - 0s 10ms/step - loss: 2.3292e-06
Epoch 328/400
3/3 [==============================] - 0s 10ms/step - loss: 3.0375e-06
Epoch 329/400
3/3 [==============================] - 0s 13ms/step - loss: 4.4716e-06
Epoch 330/400
3/3 [==============================] - 0s 16ms/step - loss: 2.0425e-06
Epoch 331/400
3/3 [==============================] - 0s 12ms/step - loss: 1.4447e-06
Epoch 332/400
3/3 [==============================] - 0s 10ms/step - loss: 1.3472e-06
Epoch 333/400
3/3 [==============================] - 0s 17ms/step - loss: 1.3515e-06
Epoch 334/400
3/3 [==============================] - 0s 14ms/step - loss: 1.3087e-06
Epoch 335/400
3/3 [==============================] - 0s 18ms/step - loss: 1.5281e-06
Epoch 336/400
3/3 [==============================] - 0s 13ms/step - loss: 1.5963e-06
Epoch 337/400
3/3 [==============================] - 0s 11ms/step - loss: 1.7587e-06
Epoch 338/400
3/3 [==============================] - 0s 15ms/step - loss: 2.5823e-06
Epoch 339/400
3/3 [==============================] - 0s 16ms/step - loss: 4.7698e-06
Epoch 340/400
3/3 [==============================] - 0s 11ms/step - loss: 9.1577e-06
Epoch 341/400
3/3 [==============================] - 0s 18ms/step - loss: 1.2486e-05
Epoch 342/400
3/3 [==============================] - 0s 13ms/step - loss: 1.1364e-05
Epoch 343/400
3/3 [==============================] - 0s 15ms/step - loss: 1.1751e-05
Epoch 344/400
3/3 [==============================] - 0s 11ms/step - loss: 4.3065e-06
Epoch 345/400
3/3 [==============================] - 0s 12ms/step - loss: 8.7047e-06
Epoch 346/400
3/3 [==============================] - 0s 10ms/step - loss: 6.0707e-06
Epoch 347/400
3/3 [==============================] - 0s 12ms/step - loss: 3.6290e-06
Epoch 348/400
3/3 [==============================] - 0s 13ms/step - loss: 3.3351e-06
Epoch 349/400
3/3 [==============================] - 0s 11ms/step - loss: 3.9788e-06
Epoch 350/400
3/3 [==============================] - 0s 11ms/step - loss: 7.2157e-06
Epoch 351/400
3/3 [==============================] - 0s 12ms/step - loss: 1.0251e-05
Epoch 352/400
3/3 [==============================] - 0s 12ms/step - loss: 3.6522e-06
Epoch 353/400
3/3 [==============================] - 0s 12ms/step - loss: 3.7692e-06
Epoch 354/400
3/3 [==============================] - 0s 11ms/step - loss: 2.2184e-06
Epoch 355/400
3/3 [==============================] - 0s 12ms/step - loss: 3.0729e-06
Epoch 356/400
3/3 [==============================] - 0s 16ms/step - loss: 2.3641e-06
Epoch 357/400
3/3 [==============================] - 0s 11ms/step - loss: 5.6655e-06
Epoch 358/400
3/3 [==============================] - 0s 11ms/step - loss: 6.9344e-06
Epoch 359/400
3/3 [==============================] - 0s 14ms/step - loss: 2.0580e-06
Epoch 360/400
3/3 [==============================] - 0s 16ms/step - loss: 9.2040e-06
Epoch 361/400
3/3 [==============================] - 0s 11ms/step - loss: 3.2863e-06
Epoch 362/400
3/3 [==============================] - 0s 12ms/step - loss: 2.6915e-06
Epoch 363/400
3/3 [==============================] - 0s 9ms/step - loss: 4.5898e-06
Epoch 364/400
3/3 [==============================] - 0s 17ms/step - loss: 8.6440e-06
Epoch 365/400
3/3 [==============================] - 0s 16ms/step - loss: 3.1356e-06
Epoch 366/400
3/3 [==============================] - 0s 10ms/step - loss: 2.5426e-06
Epoch 367/400
3/3 [==============================] - 0s 10ms/step - loss: 4.1343e-06
Epoch 368/400
3/3 [==============================] - 0s 20ms/step - loss: 2.7291e-06
Epoch 369/400
3/3 [==============================] - 0s 15ms/step - loss: 1.7993e-06
Epoch 370/400
3/3 [==============================] - 0s 18ms/step - loss: 3.6052e-06
Epoch 371/400
3/3 [==============================] - 0s 12ms/step - loss: 4.4178e-06
Epoch 372/400
3/3 [==============================] - 0s 17ms/step - loss: 8.7133e-06
Epoch 373/400
3/3 [==============================] - 0s 16ms/step - loss: 7.3080e-06
Epoch 374/400
3/3 [==============================] - 0s 14ms/step - loss: 2.0091e-06
Epoch 375/400
3/3 [==============================] - 0s 17ms/step - loss: 2.0008e-06
Epoch 376/400
3/3 [==============================] - 0s 15ms/step - loss: 2.9835e-06
Epoch 377/400
3/3 [==============================] - 0s 14ms/step - loss: 2.5821e-06
Epoch 378/400
3/3 [==============================] - 0s 22ms/step - loss: 5.1410e-06
Epoch 379/400
3/3 [==============================] - 0s 15ms/step - loss: 3.5277e-06
Epoch 380/400
3/3 [==============================] - 0s 13ms/step - loss: 3.2423e-06
Epoch 381/400
3/3 [==============================] - 0s 16ms/step - loss: 1.7107e-06
Epoch 382/400
3/3 [==============================] - 0s 19ms/step - loss: 2.4648e-06
Epoch 383/400
3/3 [==============================] - 0s 11ms/step - loss: 6.8383e-06
Epoch 384/400
3/3 [==============================] - 0s 15ms/step - loss: 1.5761e-05
Epoch 385/400
3/3 [==============================] - 0s 12ms/step - loss: 2.6183e-05
Epoch 386/400
3/3 [==============================] - 0s 12ms/step - loss: 1.0427e-04
Epoch 387/400
3/3 [==============================] - 0s 11ms/step - loss: 7.9516e-05
Epoch 388/400
3/3 [==============================] - 0s 12ms/step - loss: 2.4360e-05
Epoch 389/400
3/3 [==============================] - 0s 11ms/step - loss: 1.6230e-05
Epoch 390/400
3/3 [==============================] - 0s 18ms/step - loss: 1.3497e-05
Epoch 391/400
3/3 [==============================] - 0s 25ms/step - loss: 1.7710e-05
Epoch 392/400
3/3 [==============================] - 0s 25ms/step - loss: 1.7799e-05
Epoch 393/400
3/3 [==============================] - 0s 18ms/step - loss: 1.1646e-05
Epoch 394/400
3/3 [==============================] - 0s 15ms/step - loss: 1.5521e-05
Epoch 395/400
3/3 [==============================] - 0s 10ms/step - loss: 1.7312e-05
Epoch 396/400
3/3 [==============================] - 0s 10ms/step - loss: 1.1681e-05
Epoch 397/400
3/3 [==============================] - 0s 19ms/step - loss: 1.2667e-05
Epoch 398/400
3/3 [==============================] - 0s 22ms/step - loss: 2.1204e-05
Epoch 399/400
3/3 [==============================] - 0s 22ms/step - loss: 2.6879e-05
Epoch 400/400
3/3 [==============================] - 0s 20ms/step - loss: 2.3264e-05
In [ ]:
plt.plot(history.history['loss'], label='train loss')
# plt.plot(history.history['val_loss'], label='val loss')
plt.legend()
plt.show()
No description has been provided for this image

Rollout Prediction

In [ ]:
plt.figure(figsize=(12,5))
for i in range(15):
    scaler_test, test_inout_seq = make_inout_seq(5*i, 500)
    X_test = np.array([seq[0] for seq in test_inout_seq])
    y_test = np.array([seq[1] for seq in test_inout_seq])
    test_seq = X_test[0]
    for i in range(90):
        pred = model.predict(test_seq[i:][np.newaxis], verbose = 0)
        test_seq = np.concatenate([test_seq, pred], axis = 0).squeeze()

    pred_traj = scaler_test.inverse_transform(test_seq.reshape(-1,2))
    pred_traj = pred_traj[pred_traj[:,1]>=0]
    X_test_traj = scaler_test.inverse_transform(X_test[0].reshape(-1,2))
    y_test_traj = scaler_test.inverse_transform(y_test.reshape(-1,2))
    plt.plot(X_test_traj[:,0], X_test_traj[:,1])
    plt.plot(y_test_traj[:,0], y_test_traj[:,1], label = 'actual', color = 'blue')
    plt.plot(np.concatenate([X_test_traj[train_window-1,0][np.newaxis], pred_traj[:,0]], axis = 0),
             np.concatenate([X_test_traj[train_window-1,1][np.newaxis], pred_traj[:,1]], axis = 0), label = 'pred', color = 'red')
# plt.vlines(X_test_traj[:train_window,0][-1],0,np.max(y_test_traj[:,1])*1.1, color = 'red', linestyle='--')
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys(), fontsize = 15)
plt.title("Projectile Trajectory with Air Resistance", fontsize = 15)
plt.xlabel("Distance (m)", fontsize = 15)
plt.ylabel("Height (m)", fontsize = 15)
plt.grid(True)
plt.xlim([0,400])
plt.ylim([0,300])
Out[ ]:
(0.0, 300.0)
No description has been provided for this image

Lab 3: Projectile Motion with Air Resistance (PINN)

  • Physics-informed Neural Network (PINN)
In [ ]:
g = 9.81
C_d = 0.47
rho = 1.225
A = 0.0042
m = 0.145

class PINN(tf.keras.Model):
    def __init__(self):
        super(PINN, self).__init__()
        self.dense1 = tf.keras.layers.Dense(50, activation='tanh')
        self.dense2 = tf.keras.layers.Dense(50, activation='tanh')
        self.dense3 = tf.keras.layers.Dense(50, activation='tanh')
        self.dense4 = tf.keras.layers.Dense(2)  # Output: [x, y]

    def call(self, t):
        x = self.dense1(t)
        x = self.dense2(x)
        x = self.dense3(x)
        return self.dense4(x)

def loss(model, t, x0, y0, vx0, vy0):
    with tf.GradientTape(persistent=True) as tape:
        tape.watch(t)
        pred = model(t)*300
        x, y = pred[:, 0:1], pred[:, 1:2]

        x_t = tape.gradient(x, t)
        y_t = tape.gradient(y, t)
        x_tt = tape.gradient(x_t, t)
        y_tt = tape.gradient(y_t, t)

    # PDE residuals
    v = tf.sqrt(x_t**2 + y_t**2)
    f_x = x_tt + (0.5 * C_d * rho * A * x_t * v) / m
    f_y = y_tt + g + (0.5 * C_d * rho * A * y_t * v) / m

    ic_x = x[0] - x0
    ic_y = y[0] - y0
    ic_vx = x_t[0] - vx0
    ic_vy = y_t[0] - vy0

    return tf.reduce_mean(tf.square(f_x)) + \
           tf.reduce_mean(tf.square(f_y)) + \
           tf.square(ic_x) + \
           tf.square(ic_y) + \
           tf.square(ic_vx) + \
           tf.square(ic_vy)

v0 = 500.0
theta = np.pi / 12  * 3

x0 = 0.0
y0 = 0.0
vx0 = v0 * np.cos(theta)
vy0 = v0 * np.sin(theta)

t = np.linspace(0, 15, 1000).reshape(-1, 1)
t = tf.convert_to_tensor(t, dtype=tf.float32)
In [ ]:
model.summary()
Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm_1 (LSTM)               (None, 10, 100)           41200     
                                                                 
 lstm_2 (LSTM)               (None, 100)               80400     
                                                                 
 dense_2 (Dense)             (None, 100)               10100     
                                                                 
 dense_3 (Dense)             (None, 2)                 202       
                                                                 
=================================================================
Total params: 131902 (515.24 KB)
Trainable params: 131902 (515.24 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
In [ ]:
model = PINN()
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)

epochs = 10000
for epoch in range(epochs):
    with tf.GradientTape() as tape:
        current_loss = loss(model, t, x0, y0, vx0, vy0)
    gradients = tape.gradient(current_loss, model.trainable_variables)
    optimizer.apply_gradients(zip(gradients, model.trainable_variables))
    if epoch % 500 == 0:
        print(f"Epoch {epoch}: Loss = {current_loss.numpy()}")
WARNING:tensorflow:Calling GradientTape.gradient on a persistent tape inside its context is significantly less efficient than calling it outside the context (it causes the gradient ops to be recorded on the tape, leading to increased CPU and memory usage). Only call GradientTape.gradient inside the context if you actually want to trace the gradient in order to compute higher order derivatives.
WARNING:tensorflow:Calling GradientTape.gradient on a persistent tape inside its context is significantly less efficient than calling it outside the context (it causes the gradient ops to be recorded on the tape, leading to increased CPU and memory usage). Only call GradientTape.gradient inside the context if you actually want to trace the gradient in order to compute higher order derivatives.
WARNING:tensorflow:Calling GradientTape.gradient on a persistent tape inside its context is significantly less efficient than calling it outside the context (it causes the gradient ops to be recorded on the tape, leading to increased CPU and memory usage). Only call GradientTape.gradient inside the context if you actually want to trace the gradient in order to compute higher order derivatives.
WARNING:tensorflow:Calling GradientTape.gradient on a persistent tape inside its context is significantly less efficient than calling it outside the context (it causes the gradient ops to be recorded on the tape, leading to increased CPU and memory usage). Only call GradientTape.gradient inside the context if you actually want to trace the gradient in order to compute higher order derivatives.
WARNING:tensorflow:5 out of the last 5 calls to <function _BaseOptimizer._update_step_xla at 0x7c0698ff27a0> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
WARNING:tensorflow:6 out of the last 6 calls to <function _BaseOptimizer._update_step_xla at 0x7c0698ff27a0> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
Epoch 0: Loss = [340070.44]
Epoch 500: Loss = [111.06255]
Epoch 1000: Loss = [69.13449]
Epoch 1500: Loss = [45.480568]
Epoch 2000: Loss = [23.796137]
Epoch 2500: Loss = [10.01168]
Epoch 3000: Loss = [4.8761215]
Epoch 3500: Loss = [2.8827622]
Epoch 4000: Loss = [2.186963]
Epoch 4500: Loss = [1.841897]
Epoch 5000: Loss = [21.72321]
Epoch 5500: Loss = [1.3087498]
Epoch 6000: Loss = [1.1360217]
Epoch 6500: Loss = [1.0044845]
Epoch 7000: Loss = [1.4741235]
Epoch 7500: Loss = [0.7664854]
Epoch 8000: Loss = [0.8202263]
Epoch 8500: Loss = [7.1471615]
Epoch 9000: Loss = [0.56336296]
Epoch 9500: Loss = [2.0009496]

Evaluation

In [ ]:
t_test = np.linspace(0, 20, 1000).reshape(-1, 1)
t_test = tf.convert_to_tensor(t_test, dtype=tf.float32)
pred = model(t_test)*300
x_pred, y_pred = pred[:, 0].numpy(), pred[:, 1].numpy()

valid_indices = y_pred >= 0
x_pred = x_pred[valid_indices]
y_pred = y_pred[valid_indices]

def true_trajectory_with_drag(dt=0.01):
    t = 0
    x, y = [0], [0]
    vx, vy = vx0, vy0

    while y[-1] >= 0:
        v = np.sqrt(vx**2 + vy**2)
        ax = - (0.5 * C_d * rho * A * v * vx) / m
        ay = - g - (0.5 * C_d * rho * A * v * vy) / m

        vx += ax * dt
        vy += ay * dt

        x.append(x[-1] + vx * dt)
        y.append(y[-1] + vy * dt)
        t += dt
    print(t)
    return np.array(x), np.array(y)

x_true, y_true = true_trajectory_with_drag()

plt.figure(figsize=(12,5))
plt.plot(x_true, y_true, 'blue', label='Actual')
plt.plot(x_pred, y_pred, label='Predicted', color='red')

handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys(), fontsize = 15)
plt.title("Projectile Trajectory with Air Resistance (PINN)", fontsize = 15)
plt.xlabel("Distance (m)", fontsize = 15)
plt.ylabel("Height (m)", fontsize = 15)
plt.grid(True)
plt.xlim([0,400])
plt.ylim([0,310])
12.529999999999777
Out[ ]:
(0.0, 310.0)
No description has been provided for this image

Spring Mass System

Lab 1: Coupled Spring Mass ODE System

The provided diagram depicts a system consisting of three masses ($m_1$, $m_2$, and $m_3$) connected by springs in a linear arrangement. The springs are connected to rigid walls at both ends. To analyze the dynamics of this system, we need to derive the Partial Differential Equations (PDEs) governing the motion of each mass.

  • $m_1$, $m_2$, and $m_3$ are masses connected in series with springs.
  • The springs have stiffness constants $k$.
  • The system is attached to rigid walls on both ends.
  • The displacements of the masses are denoted as $x_1(t)$, $x_2(t)$, and $x_3(t)$ respectively.

Equations of Motion

$$ \begin{cases} m_1 \ddot{x}_1(t) &= -k_1 x_1(t) + k_2 \left( x_2(t) - x_1(t) \right), \\ m_2 \ddot{x}_2(t) &= -k_2 \left( x_2(t) - x_1(t) \right) + k_3 \left( x_3(t) - x_2(t) \right), \\ m_3 \ddot{x}_3(t) &= -k_3 \left( x_3(t) - x_2(t) \right) - k_4 x_3(t). \end{cases} $$

Parameters

$$ \begin{align*} [m_1, m_2, m_3] &= [1.0, 1.0, 1.0], \\ [k_1, k_2, k_3, k_4] &= [2.0, 1.0, 1.0, 2.0], \end{align*} $$

Initial Conditions

$$ \begin{align*} [x_1(0), x_2(0), x_3(0)] &= [1.0, 0.5, 0.0], \\ [\dot{x}_1(0), \dot{x}_2(0), \dot{x}_3(0)] &= [0.0, 0.0, 0.0]. \end{align*} $$

  • Input: Partial Trajectory (10 timesteps)
  • Output: Next Position (1 timestep)
  • Train (0 seconds to 10 seconds)
  • Test (0 seconds to 20 seconds)
In [ ]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Reshape, Concatenate
import matplotlib.animation as animation
from sklearn.preprocessing import MinMaxScaler
In [ ]:
masses = np.array([1.0, 1.0, 1.0])
spring_constants = np.array([2.0, 1.0, 1.0, 2.0])
n_masses = len(masses)
n_springs = len(spring_constants)
In [ ]:
initial_positions = np.array([1,0.5,0])
initial_velocities = np.zeros(n_masses)
initial_conditions = np.concatenate([initial_positions, initial_velocities])
t_span = [0, 10]  # 시간 범위
t_eval = np.linspace(t_span[0], t_span[1], 200)
print(initial_conditions, t_eval.shape)
[1.  0.5 0.  0.  0.  0. ] (200,)
In [ ]:
t_span_test = [0, 20]
t_eval_test = np.linspace(t_span_test[0], t_span_test[1], 400)
In [ ]:
# motion equation
def motion(t, y):
    positions = y[:n_masses]
    velocities = y[n_masses:]
    accelerations = np.zeros(n_masses)

    for i in range(n_masses):
        if i > 0:
            accelerations[i] -= spring_constants[i] * (positions[i] - positions[i-1]) / masses[i]
        if i < n_masses - 1:
            accelerations[i] += spring_constants[i+1] * (positions[i+1] - positions[i]) / masses[i]

    accelerations[0] -= spring_constants[0] * positions[0] / masses[0]
    accelerations[-1] += spring_constants[-1] * (0 - positions[-1]) / masses[-1]

    return np.concatenate([velocities, accelerations])
In [ ]:
solution = solve_ivp(motion, t_span, initial_conditions, t_eval=t_eval, method='RK45')
solution_test = solve_ivp(motion, t_span_test, initial_conditions, t_eval=t_eval_test, method='RK45')
In [ ]:
fig, axs = plt.subplots(1, 1, figsize=(10, 5))

# Position graphs for each mass
for i in range(n_masses):
    axs.plot(t_eval, solution.y[i], label=f'Position of Mass {i+1}')
axs.set_xlabel('Time (s)', fontsize = 15)
axs.set_ylabel('Position', fontsize = 15)
axs.set_title('Position vs Time for Each Mass', fontsize = 15)
axs.vlines(10, -1, 1.1, color='red',linestyle='--')
axs.legend(fontsize = 15, loc='upper right')
axs.set_xlim([0,20])
plt.xlim([0,20])
plt.tight_layout()
plt.show()
No description has been provided for this image
In [ ]:
############################ TEST data ################################
fig, axs = plt.subplots(1, 1, figsize=(10, 5))

# Position graphs for each mass
for i in range(n_masses):
    axs.plot(t_eval_test, solution_test.y[i], label=f'Position of Mass {i+1}')
axs.set_xlabel('Time (s)', fontsize = 15)
axs.set_ylabel('Position', fontsize = 15)
axs.set_title('Position vs Time for Each Mass', fontsize = 15)
axs.legend(fontsize = 15, loc='upper right')
axs.vlines(10, -1, 1.1, color='red',linestyle='--')


plt.tight_layout()
plt.show()
No description has been provided for this image

Data Generation

In [ ]:
def create_inout_sequences(input_data, tw):
    inout_seq = []
    L = len(input_data)
    for i in range(L-tw):
        train_seq = input_data[i:i+tw]
        train_label = input_data[i+tw:i+tw+1]
        inout_seq.append((train_seq ,train_label))
    return inout_seq

data = solution.y[:3].T
scaler = MinMaxScaler(feature_range=(-1,1))
train_data_normalized = scaler.fit_transform(data)
train_window = 10
train_inout_seq = create_inout_sequences(train_data_normalized, train_window)

X_train = np.array([seq[0] for seq in train_inout_seq])
y_train = np.array([seq[1] for seq in train_inout_seq]).squeeze()
print(X_train.shape, y_train.shape)
(190, 10, 3) (190, 3)
In [ ]:
fig, axs = plt.subplots(1, 1, figsize=(10, 4))

# Position graphs for each mass
for i in range(n_masses):
    axs.plot(t_eval, solution.y[i], alpha = 0.3, color = 'black')
    axs.plot(t_eval[140:150], solution.y[i][140:150], label=f'Position of Mass {i+1}')
axs.set_xlabel('Time (s)', fontsize = 15)
axs.set_ylabel('Position', fontsize = 15)
axs.set_title('Position vs Time for Each Mass', fontsize = 15)
axs.legend(fontsize = 15, loc='upper right')
axs.vlines(7, -1, 1.1, color='red',linestyle='--')
axs.vlines(7.5, -1, 1.1, color='red',linestyle='--')
# # Velocity graphs for each mass
# for i in range(n_masses):
#     axs[1].plot(t_eval, solution.y[i + n_masses], label=f'Velocity of Mass {i+1}')
# axs[1].set_xlabel('Time (s)', fontsize = 15)
# axs[1].set_ylabel('Velocity', fontsize = 15)
# axs[1].set_title('Velocity vs Time for Each Mass', fontsize = 15)
# axs[1].legend(fontsize = 15, loc='upper right')
plt.xlim([0,11])
plt.ylim([-1,1.1])
plt.tight_layout()
plt.show()
No description has been provided for this image

LSTM Model Architecture

In [ ]:
# Build LSTM model with Keras
model = Sequential()
model.add(LSTM(100, input_shape=(None, 3), return_sequences=True))
model.add(LSTM(100, activation = 'tanh'))
model.add(Dense(100, activation = 'tanh'))
model.add(Dense(3, activation = 'tanh'))

model.compile(optimizer='adam', loss='mse')
model.summary()
Model: "sequential_4"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm_7 (LSTM)               (None, None, 100)         41600     
                                                                 
 lstm_8 (LSTM)               (None, 100)               80400     
                                                                 
 dense_12 (Dense)            (None, 100)               10100     
                                                                 
 dense_13 (Dense)            (None, 3)                 303       
                                                                 
=================================================================
Total params: 132403 (517.20 KB)
Trainable params: 132403 (517.20 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
In [ ]:
history = model.fit(X_train, y_train, epochs=10, batch_size=32)
Epoch 1/10
6/6 [==============================] - 4s 7ms/step - loss: 0.2528
Epoch 2/10
6/6 [==============================] - 0s 6ms/step - loss: 0.0678
Epoch 3/10
6/6 [==============================] - 0s 7ms/step - loss: 0.0507
Epoch 4/10
6/6 [==============================] - 0s 6ms/step - loss: 0.0358
Epoch 5/10
6/6 [==============================] - 0s 7ms/step - loss: 0.0331
Epoch 6/10
6/6 [==============================] - 0s 7ms/step - loss: 0.0289
Epoch 7/10
6/6 [==============================] - 0s 7ms/step - loss: 0.0251
Epoch 8/10
6/6 [==============================] - 0s 6ms/step - loss: 0.0236
Epoch 9/10
6/6 [==============================] - 0s 6ms/step - loss: 0.0220
Epoch 10/10
6/6 [==============================] - 0s 6ms/step - loss: 0.0208
In [ ]:
plt.plot(history.history['loss'], label='train loss')
# plt.plot(history.history['val_loss'], label='val loss')
plt.legend()
plt.show()
No description has been provided for this image
In [ ]:
data_test = solution_test.y[:3].T
test_data_normalized = scaler.fit_transform(data_test)

test_seq = test_data_normalized[:200]
for i in range(200):
    pred = model.predict(test_seq[i:][np.newaxis], verbose = 0)
    test_seq = np.concatenate([test_seq, pred], axis = 0).squeeze()

pred_traj = scaler.inverse_transform(test_seq.reshape(-1,3))
In [ ]:
plt.figure(figsize=(15,7))
plt.vlines(10,-1,1, color = 'red', linestyle='--')
plt.plot(t_eval_test, data_test[:,0], label='Actual', linewidth = 3, color='black',alpha=0.3)
plt.plot(t_eval_test, pred_traj[:,0],
         label='Predicted', linestyle='--', linewidth =3, color = 'red')
plt.plot(t_eval, solution.y[0], linewidth = 3)
plt.title('Actual vs Predicted Positions (Mass 1)', fontsize = 15)
plt.xlabel('Time', fontsize = 15)
plt.ylabel('Position', fontsize = 15)
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys(), fontsize = 15)
plt.show()
No description has been provided for this image
In [ ]:
plt.figure(figsize=(15,7))
plt.vlines(10,-1,1, color = 'red', linestyle='--')
plt.plot(t_eval_test, data_test[:,1], label='Actual', linewidth = 3, color='black',alpha=0.3)
plt.plot(t_eval_test, pred_traj[:,1],
         label='Predicted', linestyle='--', linewidth =3, color = 'red')
plt.plot(t_eval, solution.y[1], linewidth = 3, color='tab:orange')
plt.title('Actual vs Predicted Positions (Mass 2)', fontsize = 15)
plt.xlabel('Time', fontsize = 15)
plt.ylabel('Position', fontsize = 15)
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys(), fontsize = 15)
plt.show()
No description has been provided for this image
In [ ]:
plt.figure(figsize=(15,7))
plt.vlines(10,-1,1, color = 'red', linestyle='--')
plt.plot(t_eval_test, data_test[:,2], label='Actual', linewidth = 3, color='black',alpha=0.3)
plt.plot(t_eval_test, pred_traj[:,2],
         label='Predicted', linestyle='--', linewidth =3, color = 'red')
plt.plot(t_eval, solution.y[2], linewidth = 3, color='tab:green')
plt.title('Actual vs Predicted Positions (Mass 3)', fontsize = 15)
plt.xlabel('Time', fontsize = 15)
plt.ylabel('Position', fontsize = 15)
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys(), fontsize = 15)
plt.show()
No description has been provided for this image

Lab 2: Coupled Spring Mass ODE System (PINN)

In [ ]:
# !pip install deepxde/
Collecting deepxde
  Downloading DeepXDE-1.11.1-py3-none-any.whl (182 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 182.2/182.2 kB 5.1 MB/s eta 0:00:00
Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (from deepxde) (3.7.1)
Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from deepxde) (1.25.2)
Requirement already satisfied: scikit-learn in /usr/local/lib/python3.10/dist-packages (from deepxde) (1.2.2)
Collecting scikit-optimize>=0.9.0 (from deepxde)
  Downloading scikit_optimize-0.10.2-py2.py3-none-any.whl (107 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 107.8/107.8 kB 13.1 MB/s eta 0:00:00
Requirement already satisfied: scipy in /usr/local/lib/python3.10/dist-packages (from deepxde) (1.11.4)
Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.10/dist-packages (from scikit-optimize>=0.9.0->deepxde) (1.4.2)
Collecting pyaml>=16.9 (from scikit-optimize>=0.9.0->deepxde)
  Downloading pyaml-24.4.0-py3-none-any.whl (24 kB)
Requirement already satisfied: packaging>=21.3 in /usr/local/lib/python3.10/dist-packages (from scikit-optimize>=0.9.0->deepxde) (24.0)
Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn->deepxde) (3.5.0)
Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->deepxde) (1.2.1)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib->deepxde) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->deepxde) (4.53.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->deepxde) (1.4.5)
Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->deepxde) (9.4.0)
Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->deepxde) (3.1.2)
Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib->deepxde) (2.8.2)
Requirement already satisfied: PyYAML in /usr/local/lib/python3.10/dist-packages (from pyaml>=16.9->scikit-optimize>=0.9.0->deepxde) (6.0.1)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib->deepxde) (1.16.0)
Installing collected packages: pyaml, scikit-optimize, deepxde
Successfully installed deepxde-1.11.1 pyaml-24.4.0 scikit-optimize-0.10.2
In [ ]:
import deepxde as dde
import numpy as np
import matplotlib.pyplot as plt

from deepxde.backend.set_default_backend import set_default_backend
set_default_backend("pytorch")

# Masses
m1 = 1.0
m2 = 1.0
m3 = 1.0

# Spring constants
k1 = 2.0
k2 = 1.0
k3 = 1.0
k4 = 2.0


# Initial conditions
# Initial displacements (from equilibrium positions)
d1_0 = 1
d2_0 = 0
d3_0 = 0

# Initial velocities
v1_0 = 0.0
v2_0 = 0.0
v3_0 = 0.0

# Maximum time to simulate
t_max = 5

def dy(t, x):
    return dde.grad.jacobian(x, t)

def pde(t, x):
    # mass 1 location
    x_1 = x[:, 0:1] # Use slicing to extract the column as a tensor
    # mass 2 location
    x_2 = x[:, 1:2] # Use slicing to extract the column as a tensor
    # mass 3 location
    x_3 = x[:, 2:3] # Use slicing to extract the column as a tensor

    dx1_tt = dde.grad.hessian(x, t, i=0,j=0,component=0)
    dx2_tt = dde.grad.hessian(x, t, i=0,j=0,component=1)
    dx3_tt = dde.grad.hessian(x, t, i=0,j=0,component=2)

    pde1 = m1 * dx1_tt + k1 * (x_1) - k2 * (x_2 - x_1)
    pde2 = m2 * dx2_tt + k2 * (x_2 - x_1) - k3 * (x_3 - x_2)
    pde3 = m3 * dx3_tt + k3 * (x_3 - x_2) - k4 * (x_3)

    return [pde1, pde2, pde3]

def boundary_init(t, on_boundary):
    return on_boundary and np.isclose(t[0], 0)

geom = dde.geometry.Interval(0, t_max)

init_d1 = dde.icbc.PointSetBC(np.array([0]), np.array([d1_0]).reshape(-1, 1), component=0)
init_d2 = dde.icbc.PointSetBC(np.array([0]), np.array([d2_0]).reshape(-1, 1), component=1)
init_d3 = dde.icbc.PointSetBC(np.array([0]), np.array([d3_0]).reshape(-1, 1), component=2)

init_v1 = dde.OperatorBC(geom, lambda x, y, _: dy(x, y[:, 0:1]), boundary_init)
init_v2 = dde.OperatorBC(geom, lambda x, y, _: dy(x, y[:, 1:2]), boundary_init)
init_v3 = dde.OperatorBC(geom, lambda x, y, _: dy(x, y[:, 2:3]), boundary_init)

data = dde.data.PDE(
    geom,
    pde,
    [init_d1, init_d2, init_d3, init_v1, init_v2, init_v3],
    num_domain=2000,
    num_boundary=100,
    num_test=1000
)

layer_size = [1] + [20] * 3 + [3]
activation = "tanh"
initializer = "Glorot uniform"
Setting the default backend to "pytorch". You can change it in the ~/.deepxde/config.json file or export the DDE_BACKEND environment variable. Valid options are: tensorflow.compat.v1, tensorflow, pytorch, jax, paddle (all lowercase)
In [ ]:
net = dde.maps.FNN(layer_size, activation, initializer)
model = dde.Model(data, net)
model.compile("adam", lr=1e-3)
Compiling model...
Building feed-forward neural network...
'build' took 0.055637 s

/usr/local/lib/python3.10/dist-packages/deepxde/nn/tensorflow_compat_v1/fnn.py:116: UserWarning: `tf.layers.dense` is deprecated and will be removed in a future version. Please use `tf.keras.layers.Dense` instead.
  return tf.layers.dense(
'compile' took 1.245894 s

In [ ]:
losshistory, train_state = model.train(epochs=5000)
dde.saveplot(losshistory, train_state, issave=False, isplot=False)
Warning: epochs is deprecated and will be removed in a future version. Use iterations instead.
Training model...

Step      Train loss                                                                                    Test loss                                                                                     Test metric
0         [3.84e+00, 2.45e+00, 1.44e+00, 1.00e+00, 0.00e+00, 0.00e+00, 3.11e-01, 5.49e-01, 1.65e-01]    [3.94e+00, 2.49e+00, 1.47e+00, 1.00e+00, 0.00e+00, 0.00e+00, 3.11e-01, 5.49e-01, 1.65e-01]    []  
1000      [1.58e-01, 2.71e-02, 1.85e-03, 9.90e-02, 1.02e-02, 1.11e-03, 2.44e-04, 1.76e-04, 1.79e-04]    [1.64e-01, 2.83e-02, 1.93e-03, 9.90e-02, 1.02e-02, 1.11e-03, 2.44e-04, 1.76e-04, 1.79e-04]    []  
2000      [8.65e-02, 4.44e-02, 3.55e-03, 3.32e-02, 4.17e-03, 9.25e-04, 1.00e-04, 8.54e-11, 4.22e-04]    [9.01e-02, 4.18e-02, 3.36e-03, 3.32e-02, 4.17e-03, 9.25e-04, 1.00e-04, 8.54e-11, 4.22e-04]    []  
3000      [5.10e-02, 2.43e-02, 2.68e-03, 1.25e-02, 2.11e-03, 8.18e-04, 5.48e-04, 5.85e-06, 4.55e-04]    [5.11e-02, 1.86e-02, 2.00e-03, 1.25e-02, 2.11e-03, 8.18e-04, 5.48e-04, 5.85e-06, 4.55e-04]    []  
4000      [1.70e-03, 1.81e-03, 8.37e-04, 1.98e-04, 1.45e-06, 5.09e-04, 4.19e-06, 3.62e-05, 3.44e-04]    [1.48e-03, 1.34e-03, 7.62e-04, 1.98e-04, 1.45e-06, 5.09e-04, 4.19e-06, 3.62e-05, 3.44e-04]    []  
5000      [7.53e-04, 5.86e-04, 7.52e-04, 1.13e-05, 3.57e-05, 4.45e-04, 1.20e-05, 1.33e-05, 3.41e-04]    [6.97e-04, 4.81e-04, 7.19e-04, 1.13e-05, 3.57e-05, 4.45e-04, 1.20e-05, 1.33e-05, 3.41e-04]    []  

Best model at step 5000:
  train loss: 2.95e-03
  test loss: 2.76e-03
  test metric: []

'train' took 16.208631 s

In [ ]:
dde.optimizers.config.set_LBFGS_options(maxiter=500)
model.compile("L-BFGS")
losshistory, train_state = model.train()
Compiling model...
'compile' took 1.068876 s

Training model...

Step      Train loss                                                                                    Test loss                                                                                     Test metric
5000      [7.53e-04, 5.86e-04, 7.52e-04, 1.13e-05, 3.57e-05, 4.45e-04, 1.20e-05, 1.33e-05, 3.41e-04]    [6.97e-04, 4.81e-04, 7.19e-04, 1.13e-05, 3.57e-05, 4.45e-04, 1.20e-05, 1.33e-05, 3.41e-04]    []  
5526      [7.19e-06, 7.84e-05, 6.18e-04, 2.03e-06, 3.96e-05, 3.76e-04, 1.52e-06, 3.06e-05, 2.87e-04]    [7.37e-06, 7.99e-05, 6.25e-04, 2.03e-06, 3.96e-05, 3.76e-04, 1.52e-06, 3.06e-05, 2.87e-04]    []  

Best model at step 5526:
  train loss: 1.44e-03
  test loss: 1.45e-03
  test metric: []

'train' took 11.470140 s

In [ ]:
dde.saveplot(losshistory, train_state, issave = False, isplot = True)
No description has been provided for this image
No description has been provided for this image
In [ ]:
from scipy.integrate import odeint

# Define the differential equations for the coupled spring-mass system for ODE solver
def vectorfield(w, t, p):
    """
    Defines the differential equations for the coupled spring-mass system.

    Arguments:
        w :  vector of the state variables:
                  w = [x1,y1,x2,y2,x3,y3]
        t :  time
        p :  vector of the parameters:
                  p = [m1,m2,m3,k1,k2,k3,k4,L1,L2,L3,L4]
    """
    x1, y1, x2, y2, x3, y3 = w
    m1, m2, m3, k1, k2, k3, k4 = p

    # Create f = (x1',y1',x2',y2',x3',y3'):
    f = [y1,
         (-k1 * (x1) + k2 * (x2 - x1)) / m1,
         y2,
         (-k2 * (x2 - x1) + k3 * (x3 - x2)) / m2,
         y3,
         (-k3 * (x3 - x2) - k4 * (x3)) / m3]
    return f

# ODE solver parameters
abserr = 1.0e-8
relerr = 1.0e-6
stoptime = t_max
numpoints = 250

t = geom.random_points(5000)

t[:,0].sort()

# Pack up the parameters and initial conditions:
p = [m1, m2, m3, k1, k2, k3, k4]
w0 = [d1_0, v1_0, d2_0, v2_0, d3_0, v3_0]

# Call the ODE solver.
wsol = odeint(vectorfield, w0, t[:,0], args=(p,), atol=abserr, rtol=relerr)
In [ ]:
# Predict using the trained model
result = model.predict(t)

usol1 = np.array(result[:, 0])
usol2 = np.array(result[:, 1])
usol3 = np.array(result[:, 2])

# Plot the results
lw = 2
plt.figure(figsize=(12, 6))
plt.plot(t, wsol[:, 0], alpha=1, label='Mass1 GT', c='r', lw=lw)
plt.plot(t, wsol[:, 2], alpha=1, label='Mass2 GT', c='b', lw=lw)
plt.plot(t, wsol[:, 4], alpha=1, label='Mass3 GT', c='g', lw=lw)
plt.plot(t, usol1, alpha=1, label='Mass1 Prediction', linestyle='dashed', c='k', lw=lw)
plt.plot(t, usol2, alpha=1, label='Mass2 Prediction', linestyle='dashed', c='m', lw=lw)
plt.plot(t, usol3, alpha=1, label='Mass3 Prediction', linestyle='dashed', c='y', lw=lw)
plt.legend(fontsize=15)
plt.xticks(fontsize=25)
plt.yticks(fontsize=25)
plt.xlabel('Time (s)', fontsize=30)
plt.ylabel('Displacement (m)', fontsize=30)
plt.show()
No description has been provided for this image

Lab 3: LSTM with Physics

  • Input: Partial Trajectory (10 timesteps)
  • Output: Next Positions (3 timestep)
  • Train (0 seconds to 5 seconds)
  • Test (0 seconds to 10 seconds)
In [ ]:
masses = np.array([1.0, 1.0, 1.0])
spring_constants = np.array([2.0, 1.0, 1.0, 2.0])
n_masses = len(masses)
n_springs = len(spring_constants)
In [ ]:
initial_positions = np.array([1,0,0])
initial_velocities = np.zeros(n_masses)
initial_conditions = np.concatenate([initial_positions, initial_velocities])
t_span = [0, 5]  # 시간 범위
t_eval = np.linspace(t_span[0], t_span[1], 100)
print(initial_conditions, t_eval.shape)
[1. 0. 0. 0. 0. 0.] (100,)
In [ ]:
t_span_test = [0, 10]
t_eval_test = np.linspace(t_span_test[0], t_span_test[1], 200)
In [ ]:
# motion equation
def motion(t, y):
    positions = y[:n_masses]
    velocities = y[n_masses:]
    accelerations = np.zeros(n_masses)

    for i in range(n_masses):
        if i > 0:
            accelerations[i] -= spring_constants[i] * (positions[i] - positions[i-1]) / masses[i]
        if i < n_masses - 1:
            accelerations[i] += spring_constants[i+1] * (positions[i+1] - positions[i]) / masses[i]

    accelerations[0] -= spring_constants[0] * positions[0] / masses[0]
    accelerations[-1] += spring_constants[-1] * (0 - positions[-1]) / masses[-1]

    return np.concatenate([velocities, accelerations])
In [ ]:
solution = solve_ivp(motion, t_span, initial_conditions, t_eval=t_eval, method='RK45')
solution_test = solve_ivp(motion, t_span_test, initial_conditions, t_eval=t_eval_test, method='RK45')

Data Generation

In [ ]:
def create_inout_sequences(input_data, tw):
    inout_seq = []
    L = len(input_data)
    for i in range(L-tw-2):
        train_seq = input_data[i:i+tw]
        train_label = input_data[i+tw:i+tw+3]
        inout_seq.append((train_seq ,train_label))
    return inout_seq

data = solution.y[:3].T
scaler = MinMaxScaler(feature_range=(-1,1))
train_data_normalized = scaler.fit_transform(data)
train_window = 10
train_inout_seq = create_inout_sequences(train_data_normalized, train_window)

X_train = np.array([seq[0] for seq in train_inout_seq])
y_train = np.array([seq[1] for seq in train_inout_seq]).squeeze()
print(X_train.shape, y_train.shape)
(88, 10, 3) (88, 3, 3)

LSTM Model Architecture

In [ ]:
from tensorflow.keras import backend as K

def PhysicsGuidedLoss(y_true, y_pred):
    # Loss 1
    mse_loss = K.mean((y_true-y_pred)**2)

    # Loss 2
    delta_t = t_eval[1]

    a_pred = (y_pred[:,2]-2*y_pred[:,1]+y_pred[:,0]) / (delta_t**2)

    x1_pred, x2_pred, x3_pred = y_pred[:, :, 0], y_pred[:, :, 1], y_pred[:, :, 2]

    physics_loss_1 = a_pred[:, np.newaxis, 0] - (-spring_constants[0] * x1_pred + spring_constants[1] * (x2_pred - x1_pred)) / masses[0]
    physics_loss_2 = a_pred[:, np.newaxis, 1] - (-spring_constants[1] * (x2_pred - x1_pred) + spring_constants[2] * (x3_pred - x2_pred)) / masses[1]
    physics_loss_3 = a_pred[:, np.newaxis, 2] - (-spring_constants[2] * (x3_pred - x2_pred) - spring_constants[3] * x3_pred) / masses[2]

    physics_loss = tf.reduce_mean(tf.square(physics_loss_1) + tf.square(physics_loss_2) + tf.square(physics_loss_3))

    # Total Loss
    total_loss = mse_loss + physics_loss*1e-4

    return total_loss
In [ ]:
# Build LSTM model with Keras
model = Sequential()
model.add(LSTM(100, input_shape=(10, 3), return_sequences=True))
model.add(LSTM(100, activation = 'tanh'))
model.add(Dense(100, activation = 'tanh'))
model.add(Dense(9, activation = 'tanh'))
model.add(Reshape((3,3)))

model.compile(optimizer='adam', loss=PhysicsGuidedLoss)
model.summary()
Model: "sequential_21"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_44 (LSTM)               (None, 10, 100)           41600     
_________________________________________________________________
lstm_45 (LSTM)               (None, 100)               80400     
_________________________________________________________________
dense_46 (Dense)             (None, 100)               10100     
_________________________________________________________________
dense_47 (Dense)             (None, 9)                 909       
_________________________________________________________________
reshape_19 (Reshape)         (None, 3, 3)              0         
=================================================================
Total params: 133,009
Trainable params: 133,009
Non-trainable params: 0
_________________________________________________________________
In [ ]:
history = model.fit(X_train, y_train, epochs=500, batch_size=32)
Epoch 1/500
3/3 [==============================] - 0s 4ms/step - loss: 1.0550
Epoch 2/500
3/3 [==============================] - 0s 4ms/step - loss: 0.6787
Epoch 3/500
3/3 [==============================] - 0s 4ms/step - loss: 0.5466
Epoch 4/500
3/3 [==============================] - 0s 3ms/step - loss: 0.5029
Epoch 5/500
3/3 [==============================] - 0s 4ms/step - loss: 0.4967
Epoch 6/500
3/3 [==============================] - 0s 3ms/step - loss: 0.4465
Epoch 7/500
3/3 [==============================] - 0s 3ms/step - loss: 0.4287
Epoch 8/500
3/3 [==============================] - 0s 3ms/step - loss: 0.4222
Epoch 9/500
3/3 [==============================] - 0s 3ms/step - loss: 0.4055
Epoch 10/500
3/3 [==============================] - 0s 3ms/step - loss: 0.3792
Epoch 11/500
3/3 [==============================] - 0s 3ms/step - loss: 0.3706
Epoch 12/500
3/3 [==============================] - 0s 3ms/step - loss: 0.3536
Epoch 13/500
3/3 [==============================] - 0s 2ms/step - loss: 0.3341
Epoch 14/500
3/3 [==============================] - 0s 2ms/step - loss: 0.3147
Epoch 15/500
3/3 [==============================] - 0s 2ms/step - loss: 0.2932
Epoch 16/500
3/3 [==============================] - 0s 2ms/step - loss: 0.2686
Epoch 17/500
3/3 [==============================] - 0s 2ms/step - loss: 0.2418
Epoch 18/500
3/3 [==============================] - 0s 2ms/step - loss: 0.2135
Epoch 19/500
3/3 [==============================] - 0s 2ms/step - loss: 0.1833
Epoch 20/500
3/3 [==============================] - 0s 2ms/step - loss: 0.1550
Epoch 21/500
3/3 [==============================] - 0s 2ms/step - loss: 0.1344
Epoch 22/500
3/3 [==============================] - 0s 2ms/step - loss: 0.1162
Epoch 23/500
3/3 [==============================] - 0s 2ms/step - loss: 0.1032
Epoch 24/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0899
Epoch 25/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0766
Epoch 26/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0657
Epoch 27/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0560
Epoch 28/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0487
Epoch 29/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0430
Epoch 30/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0376
Epoch 31/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0331
Epoch 32/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0283
Epoch 33/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0257
Epoch 34/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0229
Epoch 35/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0213
Epoch 36/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0206
Epoch 37/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0194
Epoch 38/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0191
Epoch 39/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0191
Epoch 40/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0244
Epoch 41/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0294
Epoch 42/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0368
Epoch 43/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0285
Epoch 44/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0286
Epoch 45/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0465
Epoch 46/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0542
Epoch 47/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0668
Epoch 48/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0521
Epoch 49/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0871
Epoch 50/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0818
Epoch 51/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0865
Epoch 52/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0778
Epoch 53/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0854
Epoch 54/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0678
Epoch 55/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0503
Epoch 56/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0375
Epoch 57/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0319
Epoch 58/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0373
Epoch 59/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0258
Epoch 60/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0197
Epoch 61/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0224
Epoch 62/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0167
Epoch 63/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0140
Epoch 64/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0130
Epoch 65/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0135
Epoch 66/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0101
Epoch 67/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0093
Epoch 68/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0085
Epoch 69/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0090
Epoch 70/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0088
Epoch 71/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0085
Epoch 72/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0076
Epoch 73/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0072
Epoch 74/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0073
Epoch 75/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0069
Epoch 76/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0066
Epoch 77/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0064
Epoch 78/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0062
Epoch 79/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0062
Epoch 80/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0061
Epoch 81/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0061
Epoch 82/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0058
Epoch 83/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0059
Epoch 84/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0056
Epoch 85/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0057
Epoch 86/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0059
Epoch 87/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0060
Epoch 88/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0060
Epoch 89/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0058
Epoch 90/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0056
Epoch 91/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0060
Epoch 92/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0057
Epoch 93/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0055
Epoch 94/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0063
Epoch 95/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0059
Epoch 96/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0060
Epoch 97/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0070
Epoch 98/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0087
Epoch 99/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0070
Epoch 100/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0071
Epoch 101/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0091
Epoch 102/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0083
Epoch 103/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0069
Epoch 104/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0066
Epoch 105/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0063
Epoch 106/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0056
Epoch 107/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0057
Epoch 108/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0056
Epoch 109/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0055
Epoch 110/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0056
Epoch 111/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0055
Epoch 112/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0054
Epoch 113/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0051
Epoch 114/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0045
Epoch 115/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0044
Epoch 116/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0046
Epoch 117/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0052
Epoch 118/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0052
Epoch 119/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0049
Epoch 120/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0044
Epoch 121/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0042
Epoch 122/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0040
Epoch 123/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0042
Epoch 124/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0039
Epoch 125/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0038
Epoch 126/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0037
Epoch 127/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0037
Epoch 128/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0036
Epoch 129/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0036
Epoch 130/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 131/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0036
Epoch 132/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 133/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 134/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0034
Epoch 135/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0034
Epoch 136/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0034
Epoch 137/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 138/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0034
Epoch 139/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 140/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0034
Epoch 141/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0036
Epoch 142/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0034
Epoch 143/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0034
Epoch 144/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0033
Epoch 145/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0033
Epoch 146/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0033
Epoch 147/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 148/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 149/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0031
Epoch 150/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 151/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 152/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0031
Epoch 153/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0031
Epoch 154/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0031
Epoch 155/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0031
Epoch 156/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0030
Epoch 157/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0031
Epoch 158/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0030
Epoch 159/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0030
Epoch 160/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0031
Epoch 161/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0030
Epoch 162/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0030
Epoch 163/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0030
Epoch 164/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0029
Epoch 165/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0030
Epoch 166/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 167/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 168/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0031
Epoch 169/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 170/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0037
Epoch 171/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0049
Epoch 172/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0044
Epoch 173/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0040
Epoch 174/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0049
Epoch 175/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0045
Epoch 176/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0053
Epoch 177/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0049
Epoch 178/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0077
Epoch 179/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0060
Epoch 180/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0094
Epoch 181/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0102
Epoch 182/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0139
Epoch 183/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0151
Epoch 184/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0196
Epoch 185/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0365
Epoch 186/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0334
Epoch 187/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0462
Epoch 188/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0575
Epoch 189/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0699
Epoch 190/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0600
Epoch 191/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0364
Epoch 192/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0457
Epoch 193/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0243
Epoch 194/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0259
Epoch 195/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0221
Epoch 196/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0153
Epoch 197/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0196
Epoch 198/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0133
Epoch 199/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0132
Epoch 200/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0123
Epoch 201/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0105
Epoch 202/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0094
Epoch 203/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0083
Epoch 204/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0079
Epoch 205/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0065
Epoch 206/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0057
Epoch 207/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0052
Epoch 208/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0047
Epoch 209/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0043
Epoch 210/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0041
Epoch 211/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0038
Epoch 212/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0036
Epoch 213/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 214/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0034
Epoch 215/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0034
Epoch 216/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0033
Epoch 217/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 218/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 219/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 220/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0031
Epoch 221/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0030
Epoch 222/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0030
Epoch 223/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0029
Epoch 224/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0028
Epoch 225/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0028
Epoch 226/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0028
Epoch 227/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0028
Epoch 228/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0027
Epoch 229/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0027
Epoch 230/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0027
Epoch 231/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0027
Epoch 232/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0027
Epoch 233/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0026
Epoch 234/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0026
Epoch 235/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0026
Epoch 236/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0026
Epoch 237/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 238/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 239/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 240/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 241/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 242/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 243/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 244/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 245/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 246/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 247/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 248/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 249/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 250/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 251/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 252/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 253/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 254/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 255/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 256/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 257/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 258/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 259/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 260/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 261/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 262/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 263/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 264/500
3/3 [==============================] - 0s 3ms/step - loss: 0.0022
Epoch 265/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 266/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 267/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 268/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 269/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 270/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 271/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 272/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 273/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 274/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 275/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 276/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 277/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 278/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 279/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 280/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 281/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 282/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 283/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 284/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 285/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 286/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 287/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 288/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 289/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 290/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 291/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 292/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0020
Epoch 293/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0020
Epoch 294/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0020
Epoch 295/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0019
Epoch 296/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0020
Epoch 297/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0020
Epoch 298/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0020
Epoch 299/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0020
Epoch 300/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 301/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0026
Epoch 302/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0033
Epoch 303/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0079
Epoch 304/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0164
Epoch 305/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0280
Epoch 306/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0337
Epoch 307/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0252
Epoch 308/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0259
Epoch 309/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0250
Epoch 310/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0249
Epoch 311/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0213
Epoch 312/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0188
Epoch 313/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0175
Epoch 314/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0190
Epoch 315/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0171
Epoch 316/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0116
Epoch 317/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0088
Epoch 318/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0078
Epoch 319/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0068
Epoch 320/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0098
Epoch 321/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0082
Epoch 322/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0106
Epoch 323/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0101
Epoch 324/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0102
Epoch 325/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0106
Epoch 326/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0119
Epoch 327/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0086
Epoch 328/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0064
Epoch 329/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0068
Epoch 330/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0079
Epoch 331/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0069
Epoch 332/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0057
Epoch 333/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0057
Epoch 334/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0071
Epoch 335/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0129
Epoch 336/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0123
Epoch 337/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0094
Epoch 338/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0063
Epoch 339/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0056
Epoch 340/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0043
Epoch 341/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0039
Epoch 342/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0034
Epoch 343/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 344/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0031
Epoch 345/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0028
Epoch 346/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0026
Epoch 347/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 348/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 349/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 350/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 351/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 352/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 353/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 354/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 355/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 356/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0026
Epoch 357/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 358/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 359/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 360/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 361/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 362/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 363/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 364/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 365/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 366/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 367/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 368/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 369/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 370/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 371/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 372/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 373/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 374/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 375/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0026
Epoch 376/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0028
Epoch 377/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0029
Epoch 378/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0027
Epoch 379/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0031
Epoch 380/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 381/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 382/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0027
Epoch 383/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 384/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0030
Epoch 385/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0028
Epoch 386/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 387/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 388/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 389/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 390/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0020
Epoch 391/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 392/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 393/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0026
Epoch 394/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0028
Epoch 395/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0026
Epoch 396/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0026
Epoch 397/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0031
Epoch 398/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 399/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0036
Epoch 400/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0046
Epoch 401/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0049
Epoch 402/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0036
Epoch 403/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0036
Epoch 404/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0030
Epoch 405/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0033
Epoch 406/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 407/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0031
Epoch 408/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0028
Epoch 409/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 410/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 411/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 412/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 413/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0026
Epoch 414/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0027
Epoch 415/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0028
Epoch 416/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 417/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0048
Epoch 418/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 419/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0037
Epoch 420/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0037
Epoch 421/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0029
Epoch 422/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0039
Epoch 423/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0039
Epoch 424/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0041
Epoch 425/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0027
Epoch 426/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 427/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 428/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 429/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 430/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 431/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 432/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 433/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 434/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0021
Epoch 435/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 436/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0022
Epoch 437/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0023
Epoch 438/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 439/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0027
Epoch 440/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0038
Epoch 441/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0043
Epoch 442/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0041
Epoch 443/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0061
Epoch 444/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 445/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 446/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0031
Epoch 447/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0030
Epoch 448/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0030
Epoch 449/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 450/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0038
Epoch 451/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0034
Epoch 452/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0033
Epoch 453/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0029
Epoch 454/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0029
Epoch 455/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 456/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0028
Epoch 457/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0025
Epoch 458/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0035
Epoch 459/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0040
Epoch 460/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0058
Epoch 461/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0068
Epoch 462/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0080
Epoch 463/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0088
Epoch 464/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0050
Epoch 465/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0054
Epoch 466/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0049
Epoch 467/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0036
Epoch 468/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0045
Epoch 469/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0048
Epoch 470/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0050
Epoch 471/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0046
Epoch 472/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0045
Epoch 473/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0037
Epoch 474/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0030
Epoch 475/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0024
Epoch 476/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0027
Epoch 477/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0044
Epoch 478/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0039
Epoch 479/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0043
Epoch 480/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0047
Epoch 481/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0040
Epoch 482/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 483/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0044
Epoch 484/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0032
Epoch 485/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0047
Epoch 486/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0061
Epoch 487/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0064
Epoch 488/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0116
Epoch 489/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0133
Epoch 490/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0089
Epoch 491/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0063
Epoch 492/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0055
Epoch 493/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0047
Epoch 494/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0050
Epoch 495/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0041
Epoch 496/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0042
Epoch 497/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0052
Epoch 498/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0053
Epoch 499/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0052
Epoch 500/500
3/3 [==============================] - 0s 2ms/step - loss: 0.0044
In [ ]:
plt.plot(history.history['loss'], label='train loss')
# plt.plot(history.history['val_loss'], label='val loss')
plt.legend()
plt.show()
No description has been provided for this image
In [ ]:
data_test = solution_test.y[:3].T
test_data_normalized = scaler.fit_transform(data_test)

test_seq = test_data_normalized[:10]
for i in range(200):
    pred = model.predict(test_seq[3*i:][np.newaxis], verbose = 0)
    test_seq = np.concatenate([test_seq, pred.squeeze()], axis = 0).squeeze()

pred_traj = scaler.inverse_transform(test_seq.reshape(-1,3))
In [ ]:
plt.figure(figsize=(15,7))
plt.vlines(0.45,-1,1, color = 'red', linestyle='--')
plt.plot(t_eval_test, data_test[:200,0], label='Actual', linewidth = 3, color='black',alpha=0.3)
plt.plot(t_eval_test, pred_traj[:200,0],
         label='Predicted', linestyle='-', linewidth =3, color = 'red')
plt.plot(t_eval[:10], solution.y[0][:10], linewidth = 3)
plt.title('Actual vs Predicted Positions (Mass 1)', fontsize = 15)
plt.xlabel('Time', fontsize = 15)
plt.ylabel('Position', fontsize = 15)
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys(), fontsize = 15)
plt.show()
No description has been provided for this image
In [ ]:
plt.figure(figsize=(15,7))
plt.vlines(0.45,-1,1, color = 'red', linestyle='--')
plt.plot(t_eval_test, data_test[:200,1], label='Actual', linewidth = 3, color='black',alpha=0.3)
plt.plot(t_eval_test, pred_traj[:200,1],
         label='Predicted', linestyle='-', linewidth =3, color = 'red')
plt.plot(t_eval[:10], solution.y[1][:10], linewidth = 3, color='tab:orange')
plt.title('Actual vs Predicted Positions (Mass 2)', fontsize = 15)
plt.xlabel('Time', fontsize = 15)
plt.ylabel('Position', fontsize = 15)
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys(), fontsize = 15)
plt.show()
No description has been provided for this image
In [ ]:
plt.figure(figsize=(15,7))
plt.vlines(0.45,-1,1, color = 'red', linestyle='--')
plt.plot(t_eval_test, data_test[:200,2], label='Actual', linewidth = 3, color='black',alpha=0.3)
plt.plot(t_eval_test, pred_traj[:200,2],
         label='Predicted', linestyle='-', linewidth =3, color = 'red')
plt.plot(t_eval[:10], solution.y[2][:10], linewidth = 3, color='tab:green')
plt.title('Actual vs Predicted Positions (Mass 3)', fontsize = 15)
plt.xlabel('Time', fontsize = 15)
plt.ylabel('Position', fontsize = 15)
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys(), fontsize = 15)
plt.show()
No description has been provided for this image

Trajectory of Double Pendulum

What is double pendulum problem?



Complexitiy of the Dobule Pendulum Problem


- Nonlinearity

  • The double pendulum's behavior is governed by nonlinear equations, leading to complex, unpredictable dynamics.

- Sensitivity to Initial Conditions

  • Minor variations in the initial state can result in drastically different motions.

-Lack of Periodicity

  • Unlike simple pendulums, the double pendulum lacks consistent cycles

- Dependence on Multiple Parameters

  • The interplay of parameters like mass, length, and gravity affects the pendulum's motion.
In [ ]:
import numpy as np
from scipy.integrate import odeint
import math
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
from matplotlib import animation, rc
from matplotlib import pyplot as plt
from IPython.display import HTML

from sklearn.metrics import mean_absolute_error, mean_squared_error
In [ ]:
# Constants
m1 = 2.0  # Mass 1
m2 = 1.0  # Mass 2
L1 = 1.5  # Length 1
L2 = 1.0  # Length 2
g = 9.8  # Acceleration due to gravity

Configuration vector $u ~=~ [\theta_1, \dot{\theta}_1, \theta_2, \dot{\theta}_2 ]$ has two angles $\theta_1$ and $\theta_2$ and the angular velocity $\dot{\theta}_1$ and $\dot{\theta}_2$ so

$$ \dot{\theta}_1 = \frac{d \theta_1}{dt} ~~~ and ~~~~ \dot{\theta}_2 = \frac{d \theta_2}{dt} $$

For the purposes of numerical solution, the equations are written in the Lagrangian formalism.

$$ \ddot{\theta}_1 ~=~ \frac{- (m_1+m_2)g sin\theta_1 + m_2 g sin\theta_2 sin\Delta - m_2 sin\Delta (L_2 \dot\theta_2^2 + L_1\dot\theta_1^2cos\Delta)}{ (m_1+m_2)L_1 - m_2L_1cos^2\Delta} $$

$$ \ddot{\theta}_2 ~=~ \frac{(m_1+m_2)(L_1\dot\theta_1^2sin\Delta - gsin\dot\theta_2 + gsin\dot\theta_1cos\Delta) + m_2 L_2 \dot\theta_2^2 sin\Delta cos\Delta}{(m_1+m_2)L_2 - m_2L_2cos^2\Delta} $$

where $m_i, L_i$ and $\theta_i$ denote the masses, lengths, and angles from the vertical of the upper $(i = 1)$ and lower $(i = 2)$ pendulum, and $\theta_1 - \theta_2 = \Delta$.

In [ ]:
def derivs(state, t):
    state_derivs = np.zeros_like(state)
    theta1, theta1_dot, theta2, theta2_dot = state
    delta = theta1 - theta2
    c = np.cos(delta)
    s = np.sin(delta)

    state_derivs[0] = theta1_dot  # d(theta1)/dt
    state_derivs[1] = ((- (m1 + m2) * g * np.sin(theta1)
                        + m2 * g * np.sin(theta2) * c
                        - m2 * s * (L2 * theta2_dot**2 + L1 * theta1_dot**2 * c))
                       / ((m1 + m2) * L1 - m2 * L1 * c**2))

    state_derivs[2] = theta2_dot  # d(theta2)/dt
    state_derivs[3] = (((m1 + m2) * (L1 * theta1_dot**2 * s
                                     - g * np.sin(theta2)
                                     + g * np.sin(theta1) * c)
                        + m2 * L2 * theta2_dot**2 * s * c)
                       / ((m1 + m2) * L2 - m2 * L2 * c**2))

    return state_derivs

Lab 1: LSTM for Prediction

In [ ]:
# Simulation parameters
tfinal = 20.0       # Final time. Simulation time = 0 to tfinal.
Nt = 1000
t = np.linspace(0, tfinal, Nt)

theta1 = -80
theta2 = -150

# Hard coded initial velocities, can change to random
initial_velo_1 = 0.0
initial_velo_2 = 0.0

# Running the simulation

state = np.radians([theta1, initial_velo_1, theta2, initial_velo_2])
sol = odeint(derivs, state, t)
In [ ]:
#Generate the training set
train_window = 50

def create_inout_sequences(input_data, tw):
    x = []
    y = []
    L = len(input_data)
    for i in range(L-tw):
        seq_tmp = input_data[i:i+tw]
        label_tmp = input_data[i+tw:i+tw+1]
        x.append(seq_tmp)
        y.append(label_tmp)
    return np.array(x), np.array(y)

def preprocess_sol(sol, train_window):
    scaler = MinMaxScaler(feature_range=(-1, 1))  # -1 ~ 1 normalization
    train_data_normalized = scaler.fit_transform(sol)
    train_data_normalized = np.array(train_data_normalized)
    train_X, train_y= create_inout_sequences(train_data_normalized, train_window)
    return train_X, train_y             # (990, 10, 4)
In [ ]:
scaler = MinMaxScaler(feature_range=(-1, 1))  # -1 ~ 1 normalization
train_data_normalized = scaler.fit_transform(sol)
train_data_normalized = np.array(train_data_normalized)
data, label= create_inout_sequences(train_data_normalized, train_window)


train_data = np.array(data).reshape((-1,train_window,4))[:500]
test_data = np.array(data).reshape((-1,train_window,4))[500:]
train_label = np.array(label).reshape((-1,4))[:500]
test_label = np.array(label).reshape((-1,4))[500:]

Model Architecture

In [ ]:
n_lstm1 = 100
n_lstm2 = 100

n_hidden = 100
n_output = 4

model = tf.keras.models.Sequential([
    tf.keras.layers.Input(shape=(train_window,4)),   # (timestep, features)
    tf.keras.layers.LSTM(n_lstm1, return_sequences = True),
    tf.keras.layers.LSTM(n_lstm2),
    tf.keras.layers.Dense(n_hidden),
    tf.keras.layers.Dense(n_output),
])

model.summary()

model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=5e-4),
              loss = 'mean_squared_error',
              metrics = ['mse'])
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm (LSTM)                 (None, 50, 100)           42000     
                                                                 
 lstm_1 (LSTM)               (None, 100)               80400     
                                                                 
 dense (Dense)               (None, 100)               10100     
                                                                 
 dense_1 (Dense)             (None, 4)                 404       
                                                                 
=================================================================
Total params: 132904 (519.16 KB)
Trainable params: 132904 (519.16 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
In [ ]:
history = model.fit(train_data, train_label, epochs = 50, batch_size=32)
Epoch 1/50
16/16 [==============================] - 18s 17ms/step - loss: 0.1169 - mse: 0.1169
Epoch 2/50
16/16 [==============================] - 0s 16ms/step - loss: 0.0152 - mse: 0.0152
Epoch 3/50
16/16 [==============================] - 0s 13ms/step - loss: 0.0092 - mse: 0.0092
Epoch 4/50
16/16 [==============================] - 0s 13ms/step - loss: 0.0063 - mse: 0.0063
Epoch 5/50
16/16 [==============================] - 0s 13ms/step - loss: 0.0050 - mse: 0.0050
Epoch 6/50
16/16 [==============================] - 0s 13ms/step - loss: 0.0043 - mse: 0.0043
Epoch 7/50
16/16 [==============================] - 0s 14ms/step - loss: 0.0037 - mse: 0.0037
Epoch 8/50
16/16 [==============================] - 0s 13ms/step - loss: 0.0037 - mse: 0.0037
Epoch 9/50
16/16 [==============================] - 0s 14ms/step - loss: 0.0032 - mse: 0.0032
Epoch 10/50
16/16 [==============================] - 0s 14ms/step - loss: 0.0026 - mse: 0.0026
Epoch 11/50
16/16 [==============================] - 0s 11ms/step - loss: 0.0022 - mse: 0.0022
Epoch 12/50
16/16 [==============================] - 0s 13ms/step - loss: 0.0019 - mse: 0.0019
Epoch 13/50
16/16 [==============================] - 0s 13ms/step - loss: 0.0017 - mse: 0.0017
Epoch 14/50
16/16 [==============================] - 0s 12ms/step - loss: 0.0016 - mse: 0.0016
Epoch 15/50
16/16 [==============================] - 0s 15ms/step - loss: 0.0015 - mse: 0.0015
Epoch 16/50
16/16 [==============================] - 0s 12ms/step - loss: 0.0013 - mse: 0.0013
Epoch 17/50
16/16 [==============================] - 0s 14ms/step - loss: 0.0013 - mse: 0.0013
Epoch 18/50
16/16 [==============================] - 0s 11ms/step - loss: 0.0012 - mse: 0.0012
Epoch 19/50
16/16 [==============================] - 0s 12ms/step - loss: 0.0010 - mse: 0.0010
Epoch 20/50
16/16 [==============================] - 0s 15ms/step - loss: 9.6003e-04 - mse: 9.6003e-04
Epoch 21/50
16/16 [==============================] - 0s 17ms/step - loss: 9.9965e-04 - mse: 9.9965e-04
Epoch 22/50
16/16 [==============================] - 0s 16ms/step - loss: 8.7295e-04 - mse: 8.7295e-04
Epoch 23/50
16/16 [==============================] - 0s 16ms/step - loss: 8.2940e-04 - mse: 8.2940e-04
Epoch 24/50
16/16 [==============================] - 0s 17ms/step - loss: 0.0010 - mse: 0.0010
Epoch 25/50
16/16 [==============================] - 0s 14ms/step - loss: 9.3321e-04 - mse: 9.3321e-04
Epoch 26/50
16/16 [==============================] - 0s 23ms/step - loss: 8.0516e-04 - mse: 8.0516e-04
Epoch 27/50
16/16 [==============================] - 0s 19ms/step - loss: 7.6328e-04 - mse: 7.6328e-04
Epoch 28/50
16/16 [==============================] - 0s 18ms/step - loss: 7.3858e-04 - mse: 7.3858e-04
Epoch 29/50
16/16 [==============================] - 0s 18ms/step - loss: 7.1061e-04 - mse: 7.1061e-04
Epoch 30/50
16/16 [==============================] - 0s 22ms/step - loss: 6.9571e-04 - mse: 6.9571e-04
Epoch 31/50
16/16 [==============================] - 0s 19ms/step - loss: 6.8209e-04 - mse: 6.8209e-04
Epoch 32/50
16/16 [==============================] - 0s 24ms/step - loss: 6.1466e-04 - mse: 6.1466e-04
Epoch 33/50
16/16 [==============================] - 0s 24ms/step - loss: 6.4583e-04 - mse: 6.4583e-04
Epoch 34/50
16/16 [==============================] - 0s 26ms/step - loss: 6.5755e-04 - mse: 6.5755e-04
Epoch 35/50
16/16 [==============================] - 0s 21ms/step - loss: 6.6700e-04 - mse: 6.6700e-04
Epoch 36/50
16/16 [==============================] - 0s 21ms/step - loss: 5.9414e-04 - mse: 5.9414e-04
Epoch 37/50
16/16 [==============================] - 0s 19ms/step - loss: 5.7895e-04 - mse: 5.7895e-04
Epoch 38/50
16/16 [==============================] - 0s 14ms/step - loss: 6.0625e-04 - mse: 6.0625e-04
Epoch 39/50
16/16 [==============================] - 0s 13ms/step - loss: 5.6232e-04 - mse: 5.6232e-04
Epoch 40/50
16/16 [==============================] - 0s 11ms/step - loss: 5.5464e-04 - mse: 5.5464e-04
Epoch 41/50
16/16 [==============================] - 0s 15ms/step - loss: 5.2657e-04 - mse: 5.2657e-04
Epoch 42/50
16/16 [==============================] - 0s 15ms/step - loss: 5.1258e-04 - mse: 5.1258e-04
Epoch 43/50
16/16 [==============================] - 0s 13ms/step - loss: 5.3614e-04 - mse: 5.3614e-04
Epoch 44/50
16/16 [==============================] - 0s 12ms/step - loss: 5.6907e-04 - mse: 5.6907e-04
Epoch 45/50
16/16 [==============================] - 0s 13ms/step - loss: 5.3525e-04 - mse: 5.3525e-04
Epoch 46/50
16/16 [==============================] - 0s 14ms/step - loss: 5.6363e-04 - mse: 5.6363e-04
Epoch 47/50
16/16 [==============================] - 0s 13ms/step - loss: 5.2594e-04 - mse: 5.2594e-04
Epoch 48/50
16/16 [==============================] - 0s 12ms/step - loss: 4.9778e-04 - mse: 4.9778e-04
Epoch 49/50
16/16 [==============================] - 0s 14ms/step - loss: 4.6732e-04 - mse: 4.6732e-04
Epoch 50/50
16/16 [==============================] - 0s 13ms/step - loss: 4.3712e-04 - mse: 4.3712e-04

Evaluation

In [ ]:
# Prediction
preds = test_data[0].copy()        # (window, 4)
for i in range(len(test_data)):
    x = model.predict(np.expand_dims(preds[i:], axis = 0), verbose = 0)               # (1, 4)
    preds = np.concatenate((preds, x), axis = 0)
actual_predictions = scaler.inverse_transform(preds)            # (1000,4)
In [ ]:
mae = mean_absolute_error(sol[550:], actual_predictions[50:])
mse = mean_squared_error(sol[550:], actual_predictions[50:])
print('Mean absolute error (MAE): {:.4f}'.format(mae))
print('Mean absolute error (MSE): {:.4f}'.format(mse))
Mean absolute error (MAE): 2.9267
Mean absolute error (MSE): 23.1320
In [ ]:
data = sol
data2 = np.concatenate((sol[:500], actual_predictions), axis = 0)
In [ ]:
def make_anim2(data1, data2):
    def init():
        line1.set_data([], [])
        line2.set_data([], [])
        line3.set_data([], [])
        line4.set_data([], [])
        line5.set_data([], [])
        line6.set_data([], [])
        line7.set_data([], [])
        line8.set_data([], [])
        line9.set_data([], [])
        line10.set_data([], [])
        time_string.set_text('')
        return line3, line4, line5, line1, line2, line8, line9, line10, line6, line7, time_string

    def animate(i):
        trail1 = 6  # length of motion trail of weight 1
        trail2 = 8  # length of motion trail of weight 2
        dt = t[1] - t[0]  # time step

        # Update first set of lines
        line1.set_data(xp1_1[i:max(1,i-trail1):-1], yp1_1[i:max(1,i-trail1):-1])
        line2.set_data(xp2_1[i:max(1,i-trail2):-1], yp2_1[i:max(1,i-trail2):-1])
        line3.set_data([xp1_1[i], xp2_1[i]], [yp1_1[i], yp2_1[i]])
        line4.set_data([xp1_1[i], 0], [yp1_1[i], 0])
        line5.set_data([0, 0], [0, 0])

        # Update second set of lines
        line6.set_data(xp1_2[i:max(1,i-trail1):-1], yp1_2[i:max(1,i-trail1):-1])
        line7.set_data(xp2_2[i:max(1,i-trail2):-1], yp2_2[i:max(1,i-trail2):-1])
        line8.set_data([xp1_2[i], xp2_2[i]], [yp1_2[i], yp2_2[i]])
        line9.set_data([xp1_2[i], 0], [yp1_2[i], 0])
        line10.set_data([0, 0], [0, 0])

        time_string.set_text(time_template % (i * dt))
        return line3, line4, line5, line1, line2, line8, line9, line10, line6, line7, time_string

    fig = plt.figure(figsize = (10,8))
    ax = plt.axes(xlim=(-L1-L2-0.5, L1+L2+0.5), ylim=(-2.5, 1.5))

    # First set of lines (data1)
    line1, = ax.plot([], [], 'o-', color='#d2eeff', markersize=12, markerfacecolor='#0077BE', lw=2,
                     markevery=10000, markeredgecolor='k')
    line2, = ax.plot([], [], 'o-', color='#ffebd8', markersize=12, markerfacecolor='#f66338', lw=2,
                     markevery=10000, markeredgecolor='k', label = 'Ground Truth')
    line3, = ax.plot([], [], color='k', linestyle='-', linewidth=2)
    line4, = ax.plot([], [], color='k', linestyle='-', linewidth=2)
    line5, = ax.plot([], [], 'o', color='k', markersize=10)

    # Second set of lines (data2)
    line6, = ax.plot([], [], 'o-', color='#AEEEEE', markersize=12, markerfacecolor='#5F9EA0', lw=2,
                     markevery=10000, markeredgecolor='k')
    line7, = ax.plot([], [], 'o-', color='#FFC1C1', markersize=12, markerfacecolor='#DC143C', lw=2,
                     markevery=10000, markeredgecolor='k', label='Prediction')
    line8, = ax.plot([], [], color='k', linestyle='-', linewidth=2)
    line9, = ax.plot([], [], color='k', linestyle='-', linewidth=2)
    line10, = ax.plot([], [], 'o', color='k', markersize=10)

    time_template = 'Time = %.1f s'
    time_string = ax.text(0.05, 0.9, '', transform=ax.transAxes, fontsize = 15)
    ax.get_xaxis().set_ticks([])
    ax.get_yaxis().set_ticks([])

    # Calculate coordinates for data1
    u0_1 = data1[:, 0]
    u1_1 = data1[:, 1]
    u2_1 = data1[:, 2]
    u3_1 = data1[:, 3]
    xp1_1 = L1 * np.sin(u0_1)
    yp1_1 = -L1 * np.cos(u0_1)
    xp2_1 = xp1_1 + L2 * np.sin(u2_1)
    yp2_1 = yp1_1 - L2 * np.cos(u2_1)

    # Calculate coordinates for data2
    u0_2 = data2[:, 0]
    u1_2 = data2[:, 1]
    u2_2 = data2[:, 2]
    u3_2 = data2[:, 3]
    xp1_2 = L1 * np.sin(u0_2)
    yp1_2 = -L1 * np.cos(u0_2)
    xp2_2 = xp1_2 + L2 * np.sin(u2_2)
    yp2_2 = yp1_2 - L2 * np.cos(u2_2)

    ax.legend(loc='upper right', fontsize = 15)  # Add the legend to the plot

    anim = animation.FuncAnimation(fig, animate, init_func=init,
                                   frames=Nt, interval=1000 * (t[1] - t[0]) * 0.8, blit=True)
    return anim
In [ ]:
anim_overlap = make_anim2(data,data2)
No description has been provided for this image
In [ ]:
HTML(anim_overlap.to_html5_video())
Out[ ]:

Lab 2: LSTM for Prediction

In [ ]:
theta1 = -60
theta2 = -30

state = np.radians([theta1, initial_velo_1, theta2, initial_velo_2])
sol = odeint(derivs, state, t)
In [ ]:
scaler = MinMaxScaler(feature_range=(-1, 1))  # -1 ~ 1 normalization
train_data_normalized = scaler.fit_transform(sol)
train_data_normalized = np.array(train_data_normalized)
data, label= create_inout_sequences(train_data_normalized, train_window)


train_data = np.array(data).reshape((-1,train_window,4))[:500]
test_data = np.array(data).reshape((-1,train_window,4))[500:]
train_label = np.array(label).reshape((-1,4))[:500]
test_label = np.array(label).reshape((-1,4))[500:]
In [ ]:
n_lstm1 = 100
n_lstm2 = 100

n_hidden = 100
n_output = 4

model = tf.keras.models.Sequential([
    tf.keras.layers.Input(shape=(train_window,4)),   # (timestep, features)
    tf.keras.layers.LSTM(n_lstm1, return_sequences = True),
    tf.keras.layers.LSTM(n_lstm2),
    tf.keras.layers.Dense(n_hidden, activation = 'relu'),
    tf.keras.layers.Dense(n_output),
])

model.summary()

model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=5e-4),
              loss = 'mean_squared_error',
              metrics = ['mse'])
Model: "sequential_8"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_12 (LSTM)               (None, 50, 100)           42000     
_________________________________________________________________
lstm_13 (LSTM)               (None, 100)               80400     
_________________________________________________________________
dense_19 (Dense)             (None, 100)               10100     
_________________________________________________________________
dense_20 (Dense)             (None, 4)                 404       
=================================================================
Total params: 132,904
Trainable params: 132,904
Non-trainable params: 0
_________________________________________________________________
In [ ]:
history = model.fit(train_data, train_label, epochs = 50, batch_size=32)
Epoch 1/50
16/16 [==============================] - 0s 8ms/step - loss: 0.1624 - mse: 0.1624
Epoch 2/50
16/16 [==============================] - 0s 6ms/step - loss: 0.0355 - mse: 0.0355
Epoch 3/50
16/16 [==============================] - 0s 5ms/step - loss: 0.0132 - mse: 0.0132
Epoch 4/50
16/16 [==============================] - 0s 5ms/step - loss: 0.0051 - mse: 0.0051
Epoch 5/50
16/16 [==============================] - 0s 4ms/step - loss: 0.0025 - mse: 0.0025
Epoch 6/50
16/16 [==============================] - 0s 4ms/step - loss: 0.0015 - mse: 0.0015
Epoch 7/50
16/16 [==============================] - 0s 4ms/step - loss: 0.0010 - mse: 0.0010
Epoch 8/50
16/16 [==============================] - 0s 4ms/step - loss: 7.0223e-04 - mse: 7.0223e-04
Epoch 9/50
16/16 [==============================] - 0s 4ms/step - loss: 5.3060e-04 - mse: 5.3060e-04
Epoch 10/50
16/16 [==============================] - 0s 4ms/step - loss: 4.2560e-04 - mse: 4.2560e-04
Epoch 11/50
16/16 [==============================] - 0s 4ms/step - loss: 3.7026e-04 - mse: 3.7026e-04
Epoch 12/50
16/16 [==============================] - 0s 4ms/step - loss: 3.0953e-04 - mse: 3.0953e-04
Epoch 13/50
16/16 [==============================] - 0s 4ms/step - loss: 2.7290e-04 - mse: 2.7290e-04
Epoch 14/50
16/16 [==============================] - 0s 4ms/step - loss: 2.7526e-04 - mse: 2.7526e-04
Epoch 15/50
16/16 [==============================] - 0s 4ms/step - loss: 2.2734e-04 - mse: 2.2734e-04
Epoch 16/50
16/16 [==============================] - 0s 4ms/step - loss: 1.7449e-04 - mse: 1.7449e-04
Epoch 17/50
16/16 [==============================] - 0s 4ms/step - loss: 1.6558e-04 - mse: 1.6558e-04
Epoch 18/50
16/16 [==============================] - 0s 4ms/step - loss: 1.4255e-04 - mse: 1.4255e-04
Epoch 19/50
16/16 [==============================] - 0s 4ms/step - loss: 1.2956e-04 - mse: 1.2956e-04
Epoch 20/50
16/16 [==============================] - 0s 4ms/step - loss: 1.2325e-04 - mse: 1.2325e-04
Epoch 21/50
16/16 [==============================] - 0s 4ms/step - loss: 1.1879e-04 - mse: 1.1879e-04
Epoch 22/50
16/16 [==============================] - 0s 4ms/step - loss: 1.0502e-04 - mse: 1.0502e-04
Epoch 23/50
16/16 [==============================] - 0s 4ms/step - loss: 9.0710e-05 - mse: 9.0710e-05
Epoch 24/50
16/16 [==============================] - 0s 4ms/step - loss: 9.1427e-05 - mse: 9.1427e-05
Epoch 25/50
16/16 [==============================] - 0s 4ms/step - loss: 7.6402e-05 - mse: 7.6402e-05
Epoch 26/50
16/16 [==============================] - 0s 4ms/step - loss: 7.1577e-05 - mse: 7.1577e-05
Epoch 27/50
16/16 [==============================] - 0s 4ms/step - loss: 7.5727e-05 - mse: 7.5727e-05
Epoch 28/50
16/16 [==============================] - 0s 4ms/step - loss: 6.2102e-05 - mse: 6.2102e-05
Epoch 29/50
16/16 [==============================] - 0s 4ms/step - loss: 6.5392e-05 - mse: 6.5392e-05
Epoch 30/50
16/16 [==============================] - 0s 4ms/step - loss: 1.4642e-04 - mse: 1.4642e-04
Epoch 31/50
16/16 [==============================] - 0s 4ms/step - loss: 1.2346e-04 - mse: 1.2346e-04
Epoch 32/50
16/16 [==============================] - 0s 4ms/step - loss: 5.9203e-05 - mse: 5.9203e-05
Epoch 33/50
16/16 [==============================] - 0s 4ms/step - loss: 7.2741e-05 - mse: 7.2741e-05
Epoch 34/50
16/16 [==============================] - 0s 4ms/step - loss: 5.7099e-05 - mse: 5.7099e-05
Epoch 35/50
16/16 [==============================] - 0s 4ms/step - loss: 5.0061e-05 - mse: 5.0061e-05
Epoch 36/50
16/16 [==============================] - 0s 4ms/step - loss: 4.6756e-05 - mse: 4.6756e-05
Epoch 37/50
16/16 [==============================] - 0s 4ms/step - loss: 4.2137e-05 - mse: 4.2137e-05
Epoch 38/50
16/16 [==============================] - 0s 4ms/step - loss: 4.3470e-05 - mse: 4.3470e-05
Epoch 39/50
16/16 [==============================] - 0s 4ms/step - loss: 4.4952e-05 - mse: 4.4952e-05
Epoch 40/50
16/16 [==============================] - 0s 4ms/step - loss: 6.5932e-05 - mse: 6.5932e-05
Epoch 41/50
16/16 [==============================] - 0s 4ms/step - loss: 5.0471e-05 - mse: 5.0471e-05
Epoch 42/50
16/16 [==============================] - 0s 4ms/step - loss: 7.8491e-05 - mse: 7.8491e-05
Epoch 43/50
16/16 [==============================] - 0s 4ms/step - loss: 8.9247e-05 - mse: 8.9247e-05
Epoch 44/50
16/16 [==============================] - 0s 4ms/step - loss: 4.7255e-05 - mse: 4.7255e-05
Epoch 45/50
16/16 [==============================] - 0s 4ms/step - loss: 3.0996e-05 - mse: 3.0996e-05
Epoch 46/50
16/16 [==============================] - 0s 4ms/step - loss: 3.0916e-05 - mse: 3.0916e-05
Epoch 47/50
16/16 [==============================] - 0s 4ms/step - loss: 3.2619e-05 - mse: 3.2619e-05
Epoch 48/50
16/16 [==============================] - 0s 4ms/step - loss: 2.7975e-05 - mse: 2.7975e-05
Epoch 49/50
16/16 [==============================] - 0s 4ms/step - loss: 2.4267e-05 - mse: 2.4267e-05
Epoch 50/50
16/16 [==============================] - 0s 4ms/step - loss: 2.1051e-05 - mse: 2.1051e-05

Evaluation

In [ ]:
# Prediction
preds = test_data[0].copy()        # (window, 4)
for i in range(len(test_data)):
    x = model.predict(np.expand_dims(preds[i:], axis = 0), verbose = 0)               # (1, 4)
    preds = np.concatenate((preds, x), axis = 0)
actual_predictions = scaler.inverse_transform(preds)            # (1000,4)
In [ ]:
mae = mean_absolute_error(sol[550:], actual_predictions[50:])
mse = mean_squared_error(sol[550:], actual_predictions[50:])
print('Mean absolute error (MAE): {:.4f}'.format(mae))
print('Mean absolute error (MSE): {:.4f}'.format(mse))
Mean absolute error (MAE): 0.9432
Mean absolute error (MSE): 2.1888
In [ ]:
data = sol
data2 = np.concatenate((sol[:500], actual_predictions), axis = 0)
In [ ]:
anim_overlap = make_anim2(data,data2)
No description has been provided for this image
In [ ]:
HTML(anim_overlap.to_html5_video())
Out[ ]:

Lab 3: LSTM for Prediction

In [ ]:
theta1 = -30
theta2 = 30

state = np.radians([theta1, initial_velo_1, theta2, initial_velo_2])
sol = odeint(derivs, state, t)
In [ ]:
scaler = MinMaxScaler(feature_range=(-1, 1))  # -1 ~ 1 normalization
train_data_normalized = scaler.fit_transform(sol)
train_data_normalized = np.array(train_data_normalized)
data, label= create_inout_sequences(train_data_normalized, train_window)


train_data = np.array(data).reshape((-1,train_window,4))[:500]
test_data = np.array(data).reshape((-1,train_window,4))[500:]
train_label = np.array(label).reshape((-1,4))[:500]
test_label = np.array(label).reshape((-1,4))[500:]
In [ ]:
n_lstm1 = 100
n_lstm2 = 100

n_hidden = 100
n_output = 4

model = tf.keras.models.Sequential([
    tf.keras.layers.Input(shape=(train_window,4)),   # (timestep, features)
    tf.keras.layers.LSTM(n_lstm1, return_sequences = True),
    tf.keras.layers.LSTM(n_lstm2),
    tf.keras.layers.Dense(n_hidden, activation = 'relu'),
    tf.keras.layers.Dense(n_output),
])

model.summary()

model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=5e-4),
              loss = 'mean_squared_error',
              metrics = ['mse'])
Model: "sequential_9"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_14 (LSTM)               (None, 50, 100)           42000     
_________________________________________________________________
lstm_15 (LSTM)               (None, 100)               80400     
_________________________________________________________________
dense_21 (Dense)             (None, 100)               10100     
_________________________________________________________________
dense_22 (Dense)             (None, 4)                 404       
=================================================================
Total params: 132,904
Trainable params: 132,904
Non-trainable params: 0
_________________________________________________________________
In [ ]:
history = model.fit(train_data, train_label, epochs = 50, batch_size=32)
Epoch 1/50
16/16 [==============================] - 0s 6ms/step - loss: 0.1416 - mse: 0.1416
Epoch 2/50
16/16 [==============================] - 0s 5ms/step - loss: 0.0306 - mse: 0.0306
Epoch 3/50
16/16 [==============================] - 0s 5ms/step - loss: 0.0093 - mse: 0.0093
Epoch 4/50
16/16 [==============================] - 0s 4ms/step - loss: 0.0026 - mse: 0.0026
Epoch 5/50
16/16 [==============================] - 0s 4ms/step - loss: 0.0011 - mse: 0.0011
Epoch 6/50
16/16 [==============================] - 0s 4ms/step - loss: 6.9544e-04 - mse: 6.9544e-04
Epoch 7/50
16/16 [==============================] - 0s 4ms/step - loss: 4.3827e-04 - mse: 4.3827e-04
Epoch 8/50
16/16 [==============================] - 0s 4ms/step - loss: 3.1440e-04 - mse: 3.1440e-04
Epoch 9/50
16/16 [==============================] - 0s 4ms/step - loss: 2.4217e-04 - mse: 2.4217e-04
Epoch 10/50
16/16 [==============================] - 0s 4ms/step - loss: 2.0434e-04 - mse: 2.0434e-04
Epoch 11/50
16/16 [==============================] - 0s 4ms/step - loss: 1.6710e-04 - mse: 1.6710e-04
Epoch 12/50
16/16 [==============================] - 0s 4ms/step - loss: 1.4269e-04 - mse: 1.4269e-04
Epoch 13/50
16/16 [==============================] - 0s 4ms/step - loss: 1.1817e-04 - mse: 1.1817e-04
Epoch 14/50
16/16 [==============================] - 0s 4ms/step - loss: 1.1526e-04 - mse: 1.1526e-04
Epoch 15/50
16/16 [==============================] - 0s 4ms/step - loss: 1.0087e-04 - mse: 1.0087e-04
Epoch 16/50
16/16 [==============================] - 0s 4ms/step - loss: 8.5247e-05 - mse: 8.5247e-05
Epoch 17/50
16/16 [==============================] - 0s 4ms/step - loss: 7.2473e-05 - mse: 7.2473e-05
Epoch 18/50
16/16 [==============================] - 0s 4ms/step - loss: 6.5258e-05 - mse: 6.5258e-05
Epoch 19/50
16/16 [==============================] - 0s 4ms/step - loss: 6.1568e-05 - mse: 6.1568e-05
Epoch 20/50
16/16 [==============================] - 0s 4ms/step - loss: 6.1986e-05 - mse: 6.1986e-05
Epoch 21/50
16/16 [==============================] - 0s 4ms/step - loss: 5.9442e-05 - mse: 5.9442e-05
Epoch 22/50
16/16 [==============================] - 0s 4ms/step - loss: 5.5785e-05 - mse: 5.5785e-05
Epoch 23/50
16/16 [==============================] - 0s 4ms/step - loss: 4.6594e-05 - mse: 4.6594e-05
Epoch 24/50
16/16 [==============================] - 0s 4ms/step - loss: 4.1214e-05 - mse: 4.1214e-05
Epoch 25/50
16/16 [==============================] - 0s 5ms/step - loss: 4.2270e-05 - mse: 4.2270e-05
Epoch 26/50
16/16 [==============================] - 0s 4ms/step - loss: 3.8315e-05 - mse: 3.8315e-05
Epoch 27/50
16/16 [==============================] - 0s 4ms/step - loss: 3.7433e-05 - mse: 3.7433e-05
Epoch 28/50
16/16 [==============================] - 0s 4ms/step - loss: 3.4852e-05 - mse: 3.4852e-05
Epoch 29/50
16/16 [==============================] - 0s 4ms/step - loss: 3.4745e-05 - mse: 3.4745e-05
Epoch 30/50
16/16 [==============================] - 0s 4ms/step - loss: 3.5739e-05 - mse: 3.5739e-05
Epoch 31/50
16/16 [==============================] - 0s 4ms/step - loss: 2.8382e-05 - mse: 2.8382e-05
Epoch 32/50
16/16 [==============================] - 0s 4ms/step - loss: 2.6741e-05 - mse: 2.6741e-05
Epoch 33/50
16/16 [==============================] - 0s 4ms/step - loss: 2.5360e-05 - mse: 2.5360e-05
Epoch 34/50
16/16 [==============================] - 0s 4ms/step - loss: 2.6677e-05 - mse: 2.6677e-05
Epoch 35/50
16/16 [==============================] - 0s 4ms/step - loss: 2.8800e-05 - mse: 2.8800e-05
Epoch 36/50
16/16 [==============================] - 0s 4ms/step - loss: 2.5780e-05 - mse: 2.5780e-05
Epoch 37/50
16/16 [==============================] - 0s 4ms/step - loss: 2.4731e-05 - mse: 2.4731e-05
Epoch 38/50
16/16 [==============================] - 0s 4ms/step - loss: 2.4720e-05 - mse: 2.4720e-05
Epoch 39/50
16/16 [==============================] - 0s 4ms/step - loss: 2.1085e-05 - mse: 2.1085e-05
Epoch 40/50
16/16 [==============================] - 0s 4ms/step - loss: 2.0980e-05 - mse: 2.0980e-05
Epoch 41/50
16/16 [==============================] - 0s 4ms/step - loss: 1.8759e-05 - mse: 1.8759e-05
Epoch 42/50
16/16 [==============================] - 0s 4ms/step - loss: 1.7276e-05 - mse: 1.7276e-05
Epoch 43/50
16/16 [==============================] - 0s 4ms/step - loss: 1.8990e-05 - mse: 1.8990e-05
Epoch 44/50
16/16 [==============================] - 0s 4ms/step - loss: 1.8337e-05 - mse: 1.8337e-05
Epoch 45/50
16/16 [==============================] - 0s 4ms/step - loss: 1.8306e-05 - mse: 1.8306e-05
Epoch 46/50
16/16 [==============================] - 0s 4ms/step - loss: 1.7507e-05 - mse: 1.7507e-05
Epoch 47/50
16/16 [==============================] - 0s 4ms/step - loss: 1.8865e-05 - mse: 1.8865e-05
Epoch 48/50
16/16 [==============================] - 0s 4ms/step - loss: 1.8915e-05 - mse: 1.8915e-05
Epoch 49/50
16/16 [==============================] - 0s 4ms/step - loss: 1.8182e-05 - mse: 1.8182e-05
Epoch 50/50
16/16 [==============================] - 0s 4ms/step - loss: 1.8597e-05 - mse: 1.8597e-05

Evaluation

In [ ]:
# Prediction
preds = test_data[0].copy()        # (window, 4)
for i in range(len(test_data)):
    x = model.predict(np.expand_dims(preds[i:], axis = 0), verbose = 0)               # (1, 4)
    preds = np.concatenate((preds, x), axis = 0)
actual_predictions = scaler.inverse_transform(preds)            # (1000,4)
In [ ]:
mae = mean_absolute_error(sol[550:], actual_predictions[50:])
mse = mean_squared_error(sol[550:], actual_predictions[50:])
print('Mean absolute error (MAE): {:.4f}'.format(mae))
print('Mean absolute error (MSE): {:.4f}'.format(mse))
Mean absolute error (MAE): 0.0422
Mean absolute error (MSE): 0.0039
In [ ]:
data = sol
data2 = np.concatenate((sol[:500], actual_predictions), axis = 0)
In [ ]:
anim_overlap = make_anim2(data,data2)
No description has been provided for this image
In [ ]:
HTML(anim_overlap.to_html5_video())
Out[ ]:

Vehicle Speed Estimation

Data download: - Vehicle video

Objective

  • Develop a deep learning model to accurately estimate vehicle velocity.
  • Output precise velocity predictions to enhance autonomous driving systems.

Limitations of Traditional Methods

  • Sensor Dependency: Rely heavily on GPS, LIDAR, and RADAR, which can be expensive and prone to errors in certain conditions
  • Speed Calculation using Wheel Rotation: Slippage, skidding, tire wear, etc.
In [ ]:
import random
from collections import OrderedDict
from pathlib import Path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from natsort import natsorted

import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import *
from tensorflow.keras.applications import *
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import MeanSquaredError
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.utils import Sequence

import cv2
import os
from pathlib import Path
In [ ]:
# Setting random seed
random.seed(hash("setting random seeds") % 2**32 - 1)

# Configuration dictionary
p = {
    'project_name': 'vehicle-speed-estimation',
    'batch_size': 32,
    'w': 240,
    'h': 320,
    'model': 'efficientnetb0',
    'split': 0.2,
    'mean': .1,
    'std': .5,
    'divide_y': 10
}

# File paths for images and labels
IMAGES = './data/frames_test/'
LABELS = './data/train_vel_test.txt'
In [ ]:
def preprocess_image(image, w, h, mean, std):
    # Resize
    image = tf.image.resize(image, [w, h])
    # Rescale
    image = image / 255.0
    # Normalize
    image = (image - mean) / std
    return image

class DS(tf.keras.utils.Sequence):
    def __init__(self, images, labels, w, h, mean, std):
        self.images = Path(images)
        self.labels = [float(line.split()[0]) for line in open(labels).readlines()]
        self.n_images = len(list(self.images.glob('*.png')))
        self.indices = list(range(self.n_images - 1))
        self.batch_size = p['batch_size']
        self.w = w
        self.h = h
        self.mean = mean
        self.std = std

    def __len__(self):
        return int(np.ceil(len(self.indices) / self.batch_size))

    def __getitem__(self, idx):
        batch_indices = self.indices[idx * self.batch_size:(idx + 1) * self.batch_size]
        x = []
        y = []
        for index in batch_indices:
            f1 = f'{self.images}/{str(index)}.png'
            f2 = f'{self.images}/{str(index+1)}.png'
            image1 = Image.open(f1)
            image2 = Image.open(f2)
            image1 = preprocess_image(np.array(image1), self.w, self.h, self.mean, self.std)
            image2 = preprocess_image(np.array(image2), self.w, self.h, self.mean, self.std)
            x.append(tf.concat((image1, image2), axis=-1))
            y.append(self.labels[index] / p['divide_y'])
        return tf.stack(x), tf.convert_to_tensor(y)

# Example usage of DS class
train_ds = DS(IMAGES, LABELS, p['w'], p['h'], p['mean'], p['std'])
valid_ds = DS(IMAGES, LABELS, p['w'], p['h'], p['mean'], p['std'])
test_ds = DS('./data/frames_test/', './data/train_vel_test.txt', p['w'], p['h'], p['mean'], p['std'])
In [ ]:
image_files = natsorted(os.listdir(IMAGES))
X = np.zeros((len(image_files)-1, p['w'], p['h'], 6))
y = np.array([float(line.split()[0]) for line in open(LABELS).readlines()])
for i in range(len(image_files)-1):
    image1 = Image.open(IMAGES+image_files[i])
    image2 = Image.open(IMAGES+image_files[i+1])
    image1 = preprocess_image(np.array(image1), p['w'], p['h'], p['mean'], p['std'])
    image2 = preprocess_image(np.array(image2), p['w'], p['h'], p['mean'], p['std'])
    X[i] = tf.concat((image1, image2), axis = -1)
  • Input: A sequence of 2 past frames of road scene images.
  • Output: The predicted velocity of the vehicle.

In [ ]:
# Model input
input_layer = Input(shape=(p['w'], p['h'], 6))

x1 = Conv2D(filters = 32, kernel_size = (3,3), activation = 'relu', padding = 'SAME')(input_layer)
x1 = MaxPool2D((2,2))(x1)
x1 = Conv2D(filters = 64, kernel_size = (3,3), activation = 'relu', padding = 'SAME')(x1)
x1 = MaxPool2D((2,2))(x1)
x1 = Conv2D(filters = 128, kernel_size = (3,3), activation = 'relu', padding = 'SAME')(x1)
x1 = MaxPool2D((2,2))(x1)
x1 = Conv2D(filters = 256, kernel_size = (3,3), activation = 'relu', padding = 'SAME')(x1)
x1 = MaxPool2D((2,2))(x1)
x1 = Conv2D(filters = 512, kernel_size = (3,3), activation = 'relu', padding = 'SAME')(x1)
x1 = MaxPool2D((2,2))(x1)
x_f = Flatten()(x1)
x2 = Dense(128)(x_f)
encoded = Dense(6)(x2)

# Output layer
output_layer = Dense(1)(encoded)

# Build and compile the model
model = Model(inputs=input_layer, outputs=output_layer)
model.compile(optimizer=Adam(), loss=MeanSquaredError(), metrics=['mae'])
In [ ]:
model.summary()
Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_2 (InputLayer)        [(None, 240, 320, 6)]     0         
                                                                 
 conv2d (Conv2D)             (None, 240, 320, 32)      1760      
                                                                 
 max_pooling2d (MaxPooling2  (None, 120, 160, 32)      0         
 D)                                                              
                                                                 
 conv2d_1 (Conv2D)           (None, 120, 160, 64)      18496     
                                                                 
 max_pooling2d_1 (MaxPoolin  (None, 60, 80, 64)        0         
 g2D)                                                            
                                                                 
 conv2d_2 (Conv2D)           (None, 60, 80, 128)       73856     
                                                                 
 max_pooling2d_2 (MaxPoolin  (None, 30, 40, 128)       0         
 g2D)                                                            
                                                                 
 conv2d_3 (Conv2D)           (None, 30, 40, 256)       295168    
                                                                 
 max_pooling2d_3 (MaxPoolin  (None, 15, 20, 256)       0         
 g2D)                                                            
                                                                 
 conv2d_4 (Conv2D)           (None, 15, 20, 512)       1180160   
                                                                 
 max_pooling2d_4 (MaxPoolin  (None, 7, 10, 512)        0         
 g2D)                                                            
                                                                 
 flatten (Flatten)           (None, 35840)             0         
                                                                 
 dense_2 (Dense)             (None, 128)               4587648   
                                                                 
 dense_3 (Dense)             (None, 6)                 774       
                                                                 
 dense_4 (Dense)             (None, 1)                 7         
                                                                 
=================================================================
Total params: 6157869 (23.49 MB)
Trainable params: 6157869 (23.49 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
In [ ]:
model.fit(train_ds, validation_data=valid_ds, epochs=30)
Epoch 1/30
32/32 [==============================] - 33s 1s/step - loss: 17.6449 - mae: 1.5617 - val_loss: 0.2779 - val_mae: 0.4795
Epoch 2/30
32/32 [==============================] - 32s 985ms/step - loss: 0.2535 - mae: 0.4446 - val_loss: 0.4094 - val_mae: 0.5390
Epoch 3/30
32/32 [==============================] - 32s 1s/step - loss: 0.1848 - mae: 0.3340 - val_loss: 0.2180 - val_mae: 0.4108
Epoch 4/30
32/32 [==============================] - 33s 1s/step - loss: 0.1763 - mae: 0.3325 - val_loss: 0.2491 - val_mae: 0.3879
Epoch 5/30
32/32 [==============================] - 33s 1s/step - loss: 0.1606 - mae: 0.3152 - val_loss: 0.1917 - val_mae: 0.3832
Epoch 6/30
32/32 [==============================] - 32s 1s/step - loss: 0.1132 - mae: 0.2766 - val_loss: 0.1353 - val_mae: 0.3169
Epoch 7/30
32/32 [==============================] - 32s 1s/step - loss: 0.1325 - mae: 0.3026 - val_loss: 0.1262 - val_mae: 0.2752
Epoch 8/30
32/32 [==============================] - 33s 1s/step - loss: 0.1040 - mae: 0.2684 - val_loss: 0.0834 - val_mae: 0.2400
Epoch 9/30
32/32 [==============================] - 34s 1s/step - loss: 0.0798 - mae: 0.2382 - val_loss: 0.0676 - val_mae: 0.2165
Epoch 10/30
32/32 [==============================] - 33s 1s/step - loss: 0.1079 - mae: 0.2566 - val_loss: 0.1935 - val_mae: 0.3947
Epoch 11/30
32/32 [==============================] - 33s 1s/step - loss: 0.1178 - mae: 0.2973 - val_loss: 0.0526 - val_mae: 0.1844
Epoch 12/30
32/32 [==============================] - 32s 1s/step - loss: 0.0638 - mae: 0.1985 - val_loss: 0.0405 - val_mae: 0.1587
Epoch 13/30
32/32 [==============================] - 33s 1s/step - loss: 0.0516 - mae: 0.1833 - val_loss: 0.0448 - val_mae: 0.1488
Epoch 14/30
32/32 [==============================] - 33s 1s/step - loss: 0.0413 - mae: 0.1583 - val_loss: 0.0216 - val_mae: 0.1181
Epoch 15/30
32/32 [==============================] - 33s 1s/step - loss: 0.0248 - mae: 0.1270 - val_loss: 0.0426 - val_mae: 0.1708
Epoch 16/30
32/32 [==============================] - 34s 1s/step - loss: 0.0573 - mae: 0.1933 - val_loss: 0.0239 - val_mae: 0.1117
Epoch 17/30
32/32 [==============================] - 32s 1s/step - loss: 0.0228 - mae: 0.1210 - val_loss: 0.0179 - val_mae: 0.1052
Epoch 18/30
32/32 [==============================] - 33s 1s/step - loss: 0.0161 - mae: 0.1027 - val_loss: 0.0082 - val_mae: 0.0763
Epoch 19/30
32/32 [==============================] - 33s 1s/step - loss: 0.0111 - mae: 0.0880 - val_loss: 0.0071 - val_mae: 0.0658
Epoch 20/30
32/32 [==============================] - 32s 996ms/step - loss: 0.0122 - mae: 0.0896 - val_loss: 0.0147 - val_mae: 0.1031
Epoch 21/30
32/32 [==============================] - 32s 995ms/step - loss: 0.0097 - mae: 0.0822 - val_loss: 0.0052 - val_mae: 0.0599
Epoch 22/30
32/32 [==============================] - 33s 1s/step - loss: 0.0223 - mae: 0.1231 - val_loss: 0.0281 - val_mae: 0.1551
Epoch 23/30
32/32 [==============================] - 33s 1s/step - loss: 0.0166 - mae: 0.1042 - val_loss: 0.0168 - val_mae: 0.1017
Epoch 24/30
32/32 [==============================] - 32s 1s/step - loss: 0.0183 - mae: 0.1084 - val_loss: 0.0118 - val_mae: 0.0778
Epoch 25/30
32/32 [==============================] - 33s 1s/step - loss: 0.0102 - mae: 0.0801 - val_loss: 0.0127 - val_mae: 0.0939
Epoch 26/30
32/32 [==============================] - 32s 994ms/step - loss: 0.0089 - mae: 0.0747 - val_loss: 0.0045 - val_mae: 0.0533
Epoch 27/30
32/32 [==============================] - 33s 1s/step - loss: 0.0063 - mae: 0.0628 - val_loss: 0.0044 - val_mae: 0.0507
Epoch 28/30
32/32 [==============================] - 32s 1s/step - loss: 0.0056 - mae: 0.0587 - val_loss: 0.0078 - val_mae: 0.0743
Epoch 29/30
32/32 [==============================] - 34s 1s/step - loss: 0.0092 - mae: 0.0796 - val_loss: 0.0033 - val_mae: 0.0465
Epoch 30/30
32/32 [==============================] - 33s 1s/step - loss: 0.0067 - mae: 0.0672 - val_loss: 0.0030 - val_mae: 0.0438
Out[ ]:
<tensorflow.python.keras.callbacks.History at 0x7f96e60b47d0>

Evaluation

In [ ]:
image_files = natsorted(os.listdir('./data/frames_test/'))
X = np.zeros((len(image_files)-1, p['w'], p['h'], 6))
y = np.array([float(line.split()[0]) for line in open('./data/train_vel_test.txt').readlines()])
for i in range(len(image_files)-1):
    image1 = Image.open('./data/frames_test/'+image_files[i])
    image2 = Image.open('./data/frames_test/'+image_files[i+1])
    image1 = preprocess_image(np.array(image1), p['w'], p['h'], p['mean'], p['std'])
    image2 = preprocess_image(np.array(image2), p['w'], p['h'], p['mean'], p['std'])
    X[i] = tf.concat((image1, image2), axis = -1)

pred = model.predict(X, verbose = 0)
plt.plot(pred*p['divide_y'], 'red')
plt.plot(y[:-1])
Out[ ]:
[<matplotlib.lines.Line2D at 0x7f9c95751f90>]
No description has been provided for this image

Visualization

In [ ]:
import os
from pathlib import Path
import cv2
import imageio

def annotate_frame_with_prediction(frame, prediction, ground_truth):
    """Annotate the frame with the prediction and ground truth values."""
    font = cv2.FONT_HERSHEY_SIMPLEX
    org_pred = (50, 50)
    org_gt = (50, 100)
    font_scale = 1
    color_pred = (255, 0, 0)
    color_gt = (0, 255, 0)
    thickness = 2
    annotated_frame = cv2.putText(frame, f'Prediction: {prediction:.2f} mph', org_pred, font, font_scale, color_pred, thickness, cv2.LINE_AA)
    annotated_frame = cv2.putText(annotated_frame, f'Ground Truth: {ground_truth:.2f} mph', org_gt, font, font_scale, color_gt, thickness, cv2.LINE_AA)
    return annotated_frame

def create_gif_with_predictions(input_dir, labels_file, output_gif, model):
    """Create a GIF from annotated frames with predictions and ground truth."""
    frames = []
    frame_files = sorted([f for f in os.listdir(input_dir) if f.endswith('.png')], key=lambda x: int(x.split('.')[0]))
    labels = [float(line.split()[0]) for line in open(labels_file).readlines()]

    for i in range(len(frame_files) - 1):
        frame_file_1 = frame_files[i]
        frame_file_2 = frame_files[i + 1]

        frame_path_1 = os.path.join(input_dir, frame_file_1)
        frame_path_2 = os.path.join(input_dir, frame_file_2)

        frame_1 = cv2.imread(frame_path_1)
        frame_2 = cv2.imread(frame_path_2)

        # Preprocess the frames as needed for the model
        frame_1_resized = preprocess_image(frame_1, p['w'], p['h'], p['mean'], p['std'])
        frame_2_resized = preprocess_image(frame_2, p['w'], p['h'], p['mean'], p['std'])

        # Combine frames along the channel dimension
        combined_frames = tf.concat((frame_1_resized, frame_2_resized), axis=-1)
        frame_input = tf.reshape(combined_frames, (1, p['w'], p['h'], 3*2))

        # Make prediction
        prediction = model.predict(frame_input)[0][0]

        # Ground truth
        ground_truth = labels[i] / p['divide_y']

        # Annotate the first frame with prediction and ground truth
        annotated_frame = annotate_frame_with_prediction(frame_1, prediction*10, ground_truth*10)
        resized_annotated_frame = cv2.resize(annotated_frame, (p['w'], p['h']))

        # Convert BGR (OpenCV format) to RGB (imageio format)
        annotated_frame_rgb = cv2.cvtColor(resized_annotated_frame, cv2.COLOR_BGR2RGB)
        frames.append(annotated_frame_rgb)

    # Save frames as a GIF
    imageio.mimsave(output_gif, frames, fps=20)

# Example usage:
input_dir = "./data/frames_test"
labels_file = "./data/train_vel_test.txt"
output_gif = "./data/output.gif"

create_gif_with_predictions(input_dir, labels_file, output_gif, model)