0

I would like to train a neural network to mimic the behavior of a 3D chaotic Lorenz system of differential equations :

enter image description here

I use the following steps :

  1. Find vectors X,Y,Z from the numerical solution of the differential equation and then generate input (at time 'k') and output data (at time 'k+1') from these vectors.
  2. Train the model using input and output data. In this step I get an error (See the code below) :

Code :

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
from scipy import integrate
from mpl_toolkits.mplot3d import Axes3D
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, MaxPool2D
from keras import optimizers
from keras.layers import Activation
from keras.utils.generic_utils import get_custom_objects
from keras import backend as K


rcParams.update({'font.size': 18})
plt.rcParams['figure.figsize'] = [12, 12]

## Simulate the Lorenz System

dt = 0.01
T = 8
t = np.arange(0,T+dt,dt)
beta = 8/3
sigma = 10
rho = 28


nn_input = np.zeros((100*(len(t)-1),3))
nn_output = np.zeros_like(nn_input)

fig,ax = plt.subplots(1,1,subplot_kw={'projection': '3d'})


def lorenz_deriv(x_y_z, t0, sigma=sigma, beta=beta, rho=rho):
    x, y, z = x_y_z
    return [sigma * (y - x), x * (rho - z) - y, x * y - beta * z]

np.random.seed(123)
x0 = -15 + 30 * np.random.random((100, 3))

x_t = np.asarray([integrate.odeint(lorenz_deriv, x0_j, t)
                  for x0_j in x0])

for j in range(100):
    nn_input[j*(len(t)-1):(j+1)*(len(t)-1),:] = x_t[j,:-1,:]
    nn_output[j*(len(t)-1):(j+1)*(len(t)-1),:] = x_t[j,1:,:]
    x, y, z = x_t[j,:,:].T
    ax.plot(x, y, z,linewidth=1)
    ax.scatter(x0[j,0],x0[j,1],x0[j,2],color='r')
             
ax.view_init(18, -113)
plt.show()

## Neural Net

# Define activation functions
def logsig(x):
    return K.variable(np.divide(1,(1+np.exp(-K.eval(x)))))

def radbas(x):
    return K.variable(np.exp(-np.power(K.eval(x),2)))

def purelin(x):
    return x

#create model
model = Sequential()

#add model layers
model.add(Dense(10, activation=logsig))
model.add(Dense(10, activation=radbas))
model.add(Dense(10, activation=purelin))

sgd_optimizer = optimizers.SGD(momentum=0.9)
model.compile(optimizer=sgd_optimizer, loss='categorical_crossentropy')
model.fit(nn_input, nn_output, epochs=30)

AttributeError: 'Tensor' object has no attribute 'numpy'

Could someones help me please.

Many thanks in advance!

PS : the code is on Github

enter image description here

YIdirm
  • 21
  • 8
  • Well as far as the current error goes the reason you are getting it is because tensorflow passes tensors to its activation functions, although K.eval's input should be a numpy array not a tensor, which is why you are getting that error because K.eval is trying to run a function that only numpy arrays have. Although beyond that this code has several other problems, such as the input and output dimensions not matching the array's shape that you are passing to the model – Fatal Tempo Oct 31 '21 at 00:50
  • @SomeGuy Many thanks for your answer, I tried for two days to correct my code by applying your recommendations regarding the using of tensors but I could not have had good results and the error still persists. Could you help me please by correcting the above code and resize the inputs and outputs because I'm so pressed by time. Many thanks in advance. – YIdirm Nov 02 '21 at 23:46

0 Answers0