I have input data (x,y,v) each with the shape of (200,10000). I.e. I have 200 samples where x,y and v are of size (10000,).
-
The code basically predicts velocity by getting x and y as 1D array.
-
I have added print command in the neural_network() and found out that
the 6th layer weights are NAN
The code is as follows:
<code>import tensorflow as tf
import numpy as np
import json
class predict_velocity():
def __init__(self, x, y, v, layers):
X = np.concatenate([x, y], 1)
self.lb = X.min(0)
self.ub = X.max(0)
self.x = x
self.y = y
self.v = v
self.layers = layers
# Initialize NN
self.weights, self.biases = self.initialize_NN(layers)
# tf placeholders and graph
self.sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(allow_soft_placement=True,
log_device_placement=False))
self.x_tf = tf.placeholder(tf.float32, shape=[None, self.x.shape[1]])
self.y_tf = tf.placeholder(tf.float32, shape=[None, self.y.shape[1]])
self.v_tf = tf.placeholder(tf.float32, shape=[None, self.v.shape[1]])
self.v_predict = self.neural_net(tf.concat([self.x_tf,self.y_tf], 1), self.weights, self.biases)
self.loss = tf.reduce_sum(tf.square(self.v_tf - self.v_predict))
self.optimizer = tf.contrib.opt.ScipyOptimizerInterface(self.loss,
method = 'L-BFGS-B',
options = {'maxiter': 5000,
'maxfun': 5000,
'maxcor': 50,
'maxls': 50,
'ftol' : 1.0 * np.finfo(float).eps})
self.optimizer_Adam = tf.train.AdamOptimizer(learning_rate=0.001)
self.train_op_Adam = self.optimizer_Adam.minimize(self.loss)
init = tf.global_variables_initializer()
self.sess.run(init)
def initialize_NN(self, layers):
weights = []
biases = []
num_layers = len(layers)
for l in range(0,num_layers-1):
W = self.xavier_init(size=[layers[l], layers[l+1]])
b = tf.Variable(tf.zeros([1,layers[l+1]], dtype=tf.float32), dtype=tf.float32)
weights.append(W)
biases.append(b)
return weights, biases
def xavier_init(self, size):
in_dim = size[0]
out_dim = size[1]
xavier_stddev = np.sqrt(2/(in_dim + out_dim))
return tf.Variable(tf.random.truncated_normal([in_dim, out_dim], stddev=xavier_stddev), dtype=tf.float32)
def neural_net(self, X, weights, biases):
num_layers = len(weights) + 1
H = 2.0*(X - self.lb)/(self.ub - self.lb) - 1.0
for l in range(0,num_layers-2):
W = weights[l]
b = biases[l]
H = tf.tanh(tf.add(tf.matmul(H, W), b))
W = weights[-1]
b = biases[-1]
Y = tf.add(tf.matmul(H, W), b)
return Y
def train(self, nIter):
tf_dict = {self.x_tf: self.x, self.y_tf: self.y,
self.v_tf: self.v}
for it in range(nIter):
self.sess.run(self.train_op_Adam, tf_dict)
# Print
if it % 10 == 0:
loss_value = self.sess.run(self.loss, tf_dict)
print('It: %d, Loss: %.3e' % (it, loss_value))
self.optimizer.minimize(self.sess,
feed_dict = tf_dict,
fetches = [self.loss],
)
if __name__ == "__main__":
layers = [20000, 200, 200, 200, 200, 20, 20, 20, 20, 1]
# Load Data
data = scipy.io.loadmat('')
x_train = data['x']
y_train = data['y']
v_train = data['v']
x_train=[]
y_train=[]
u_train=[]
v_train=[]
model = predict_velocity(np.array(x_train), np.array(y_train), np.array(v_train), layers)
model.train(1000)
</code>
<code>import tensorflow as tf
import numpy as np
import json
class predict_velocity():
def __init__(self, x, y, v, layers):
X = np.concatenate([x, y], 1)
self.lb = X.min(0)
self.ub = X.max(0)
self.x = x
self.y = y
self.v = v
self.layers = layers
# Initialize NN
self.weights, self.biases = self.initialize_NN(layers)
# tf placeholders and graph
self.sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(allow_soft_placement=True,
log_device_placement=False))
self.x_tf = tf.placeholder(tf.float32, shape=[None, self.x.shape[1]])
self.y_tf = tf.placeholder(tf.float32, shape=[None, self.y.shape[1]])
self.v_tf = tf.placeholder(tf.float32, shape=[None, self.v.shape[1]])
self.v_predict = self.neural_net(tf.concat([self.x_tf,self.y_tf], 1), self.weights, self.biases)
self.loss = tf.reduce_sum(tf.square(self.v_tf - self.v_predict))
self.optimizer = tf.contrib.opt.ScipyOptimizerInterface(self.loss,
method = 'L-BFGS-B',
options = {'maxiter': 5000,
'maxfun': 5000,
'maxcor': 50,
'maxls': 50,
'ftol' : 1.0 * np.finfo(float).eps})
self.optimizer_Adam = tf.train.AdamOptimizer(learning_rate=0.001)
self.train_op_Adam = self.optimizer_Adam.minimize(self.loss)
init = tf.global_variables_initializer()
self.sess.run(init)
def initialize_NN(self, layers):
weights = []
biases = []
num_layers = len(layers)
for l in range(0,num_layers-1):
W = self.xavier_init(size=[layers[l], layers[l+1]])
b = tf.Variable(tf.zeros([1,layers[l+1]], dtype=tf.float32), dtype=tf.float32)
weights.append(W)
biases.append(b)
return weights, biases
def xavier_init(self, size):
in_dim = size[0]
out_dim = size[1]
xavier_stddev = np.sqrt(2/(in_dim + out_dim))
return tf.Variable(tf.random.truncated_normal([in_dim, out_dim], stddev=xavier_stddev), dtype=tf.float32)
def neural_net(self, X, weights, biases):
num_layers = len(weights) + 1
H = 2.0*(X - self.lb)/(self.ub - self.lb) - 1.0
for l in range(0,num_layers-2):
W = weights[l]
b = biases[l]
H = tf.tanh(tf.add(tf.matmul(H, W), b))
W = weights[-1]
b = biases[-1]
Y = tf.add(tf.matmul(H, W), b)
return Y
def train(self, nIter):
tf_dict = {self.x_tf: self.x, self.y_tf: self.y,
self.v_tf: self.v}
for it in range(nIter):
self.sess.run(self.train_op_Adam, tf_dict)
# Print
if it % 10 == 0:
loss_value = self.sess.run(self.loss, tf_dict)
print('It: %d, Loss: %.3e' % (it, loss_value))
self.optimizer.minimize(self.sess,
feed_dict = tf_dict,
fetches = [self.loss],
)
if __name__ == "__main__":
layers = [20000, 200, 200, 200, 200, 20, 20, 20, 20, 1]
# Load Data
data = scipy.io.loadmat('')
x_train = data['x']
y_train = data['y']
v_train = data['v']
x_train=[]
y_train=[]
u_train=[]
v_train=[]
model = predict_velocity(np.array(x_train), np.array(y_train), np.array(v_train), layers)
model.train(1000)
</code>
import tensorflow as tf
import numpy as np
import json
class predict_velocity():
def __init__(self, x, y, v, layers):
X = np.concatenate([x, y], 1)
self.lb = X.min(0)
self.ub = X.max(0)
self.x = x
self.y = y
self.v = v
self.layers = layers
# Initialize NN
self.weights, self.biases = self.initialize_NN(layers)
# tf placeholders and graph
self.sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(allow_soft_placement=True,
log_device_placement=False))
self.x_tf = tf.placeholder(tf.float32, shape=[None, self.x.shape[1]])
self.y_tf = tf.placeholder(tf.float32, shape=[None, self.y.shape[1]])
self.v_tf = tf.placeholder(tf.float32, shape=[None, self.v.shape[1]])
self.v_predict = self.neural_net(tf.concat([self.x_tf,self.y_tf], 1), self.weights, self.biases)
self.loss = tf.reduce_sum(tf.square(self.v_tf - self.v_predict))
self.optimizer = tf.contrib.opt.ScipyOptimizerInterface(self.loss,
method = 'L-BFGS-B',
options = {'maxiter': 5000,
'maxfun': 5000,
'maxcor': 50,
'maxls': 50,
'ftol' : 1.0 * np.finfo(float).eps})
self.optimizer_Adam = tf.train.AdamOptimizer(learning_rate=0.001)
self.train_op_Adam = self.optimizer_Adam.minimize(self.loss)
init = tf.global_variables_initializer()
self.sess.run(init)
def initialize_NN(self, layers):
weights = []
biases = []
num_layers = len(layers)
for l in range(0,num_layers-1):
W = self.xavier_init(size=[layers[l], layers[l+1]])
b = tf.Variable(tf.zeros([1,layers[l+1]], dtype=tf.float32), dtype=tf.float32)
weights.append(W)
biases.append(b)
return weights, biases
def xavier_init(self, size):
in_dim = size[0]
out_dim = size[1]
xavier_stddev = np.sqrt(2/(in_dim + out_dim))
return tf.Variable(tf.random.truncated_normal([in_dim, out_dim], stddev=xavier_stddev), dtype=tf.float32)
def neural_net(self, X, weights, biases):
num_layers = len(weights) + 1
H = 2.0*(X - self.lb)/(self.ub - self.lb) - 1.0
for l in range(0,num_layers-2):
W = weights[l]
b = biases[l]
H = tf.tanh(tf.add(tf.matmul(H, W), b))
W = weights[-1]
b = biases[-1]
Y = tf.add(tf.matmul(H, W), b)
return Y
def train(self, nIter):
tf_dict = {self.x_tf: self.x, self.y_tf: self.y,
self.v_tf: self.v}
for it in range(nIter):
self.sess.run(self.train_op_Adam, tf_dict)
# Print
if it % 10 == 0:
loss_value = self.sess.run(self.loss, tf_dict)
print('It: %d, Loss: %.3e' % (it, loss_value))
self.optimizer.minimize(self.sess,
feed_dict = tf_dict,
fetches = [self.loss],
)
if __name__ == "__main__":
layers = [20000, 200, 200, 200, 200, 20, 20, 20, 20, 1]
# Load Data
data = scipy.io.loadmat('')
x_train = data['x']
y_train = data['y']
v_train = data['v']
x_train=[]
y_train=[]
u_train=[]
v_train=[]
model = predict_velocity(np.array(x_train), np.array(y_train), np.array(v_train), layers)
model.train(1000)