Approximating data by using NN with tensorflow

Viewed 23

I am having some problems when changing an equation in a working code.

The terms of the equation mean:

  • idmat:Identity matrix of size K. (dimension 215 x 215)

  • self.__psiNNx: the output from a 3-layer neural network (data_x has passed through the NN to get an approximation) (dimension 30000 x 215)

  • self.__psiNNy: the output from a 3-layer neural network (data_y has passed through the NN to get an approximation) (dimension 30000 x 215)

  • K_reg: mapping matrix calculated using self.__psiNNx and self.__psiNNy. (dimension 215 x 215)

import numpy as np
import matplotlib.pyplot as plt

import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Lambda
import tensorflow.keras.backend as K
from sklearn.utils.extmath import randomized_svd

#Import Data
data = genfromtxt('x_data_L22_nx_64_tend_20000_deltaT_05_traj_1.csv', delimiter=',')
data=data.T


model_koopman = edmd_dict.EDMD_DL()
dim=64
ddim = 150 # number of trainable dictionary elements
hdim = 50 # number of hidden nodes per layer
kmatdim = 1+dim+ddim # dimension of K matrix (ddim + constant + identity map)
model_koopman.build(dim=64, ddim=ddim, hdim=hdim, init_lr=1e-2)

# Train model
# Can use bigger batch sizes (e.g. 1000 to 5000) if training on GPU

x_data= data[0:-1,:]  # X(t,)
y_data= data[1:,:] #  X(t+dt,)

losses = model_koopman.train(num_epochs=500, batch_size=64, 
                             log_interval=100,verbose=True, x_data=x_data, y_data=y_data)

The main code works fine, the only thing that I replaced was the following lines:

xtx_inv = tf.matrix_inverse(self.__reg*idmat + 
                                tf.matmul(tf.transpose(self.__psiNNx), self.__psiNNx))
xty = tf.matmul(tf.transpose(self.__psiNNx), self.__psiNNy)
self.__K_reg = tf.matmul(xtx_inv, xty)
self.__assignK = tf.assign(self.__K, self.__K_reg)

For a SVD calculation

Syx, Uyx, Vyx =tf.linalg.svd(tf.matmul(self.__psiNNy, tf.transpose(self.__psiNNx)),full_matrices=True, compute_uv=True)
self.__K_reg= tf.matmul(Uyx,tf.transpose(Vyx))
self.__assignK = tf.assign(self.__K, self.__K_reg)

But the code gives the following problem:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
~/opt/anaconda3/envs/edmd_DL_new/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1333     try:
-> 1334       return fn(*args)
   1335     except errors.OpError as e:

~/opt/anaconda3/envs/edmd_DL_new/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1318       return self._call_tf_sessionrun(
-> 1319           options, feed_dict, fetch_list, target_list, run_metadata)
   1320 

~/opt/anaconda3/envs/edmd_DL_new/lib/python3.6/site-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1406         self._session, options, feed_dict, fetch_list, target_list,
-> 1407         run_metadata)
   1408 

InvalidArgumentError: ConcatOp : Dimensions of inputs should match: shape[0] = [215,1] vs. shape[1] = [30000,64]
     [[{{node Model_1/concat}} = ConcatV2[N=3, T=DT_DOUBLE, Tidx=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](Model_1/ones_like, _arg_Placeholder_1_0_1, Model_1/Output_projection/add, Model_1/concat/axis)]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-44-d9ca4247fadc> in <module>
      8 
      9 losses = model_koopman.train(num_epochs=500, batch_size=64, 
---> 10                              log_interval=100,verbose=True, x_data=x_data, y_data=y_data)

I have solved the problem by transposing tf.transpose(self.__psiNNy) and tf.transpose(self.__psiNNx). But not sure that this is correct.

0 Answers
Related