I'm trying to solve the following problems :
So, I tried by using tf.GradientTape
4 times. The following is my code :
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import math
from scipy import io
tf.random.set_seed(1234)
np.random.seed(1234)
n_u=20
n_f=30
t_u=tf.cast(tf.linspace(0,1,n_u),tf.float32)
t_u=tf.reshape(t_u,shape=(n_u,1))
t_u=tf.Variable(t_u)
x_u=tf.cast(tf.linspace(0,1,n_u),tf.float32)
x_u=tf.reshape(x_u,shape=(n_u,1))
x_u=tf.Variable(x_u)
t_f=tf.cast(tf.linspace(0,1,n_f),tf.float32)
t_f=tf.reshape(t_f,shape=(n_f,1))
t_f=tf.Variable(t_f)
x_f=tf.cast(tf.linspace(0,1,n_f),tf.float32)
x_f=tf.reshape(x_f,shape=(n_f,1))
x_f=tf.Variable(x_f)
def cov(x,x_,t,t_,sigma=0.5, weight_x=-1, weight_t=-3,repeat = False):
sigma = tf.math.log(1+tf.math.exp(tf.constant(sigma,dtype=tf.float32)))
weight_x = tf.math.log(1+tf.math.exp(tf.constant(weight_x,dtype=tf.float32)))
weight_t = tf.math.log(1+tf.math.exp(tf.constant(weight_t,dtype=tf.float32)))
if repeat:
x=tf.repeat(x,x_.shape[0],axis=1)
x_=tf.transpose(tf.repeat(x_,x.shape[0],axis=1))
t=tf.repeat(t,t_.shape[0],axis=1)
t_=tf.transpose(tf.repeat(t_,t.shape[0],axis=1))
return sigma**2*tf.math.exp(-1/2* weight_x*(x-x_)**2 -1/2 * weight_t*(t-t_)**2)
#======================The above are the settings =================================
def K_12(x,x_,t,t_):
x=tf.repeat(x,x_.shape[0],axis=1)
x_=tf.transpose(tf.repeat(x_,x.shape[0],axis=1))
t=tf.repeat(t,t_.shape[0],axis=1)
t_=tf.transpose(tf.repeat(t_,t.shape[0],axis=1))
with tf.GradientTape() as g:
g.watch(x_)
with tf.GradientTape(persistent=True) as gg:
gg.watch(x_)
gg.watch(t_)
with tf.GradientTape() as ggg:
ggg.watch(x)
with tf.GradientTape(persistent=True) as gggg:
gggg.watch(x)
gggg.watch(t)
K_uu=cov(x,x_,t,t_)
K_x=gggg.gradient(K_uu,x)
K_t=gggg.gradient(K_uu,t)
K_xx=ggg.gradient(K_x,x)
K_f=K_t-1*K_xx
K_f_x=gg.gradient(K_f,x_)
K_f_t=gg.gradient(K_f,t_)
K_f_xx=g.gradient(K_f_x,x_)
return K_t - K_f_xx
print(K_12(x_u,x_f,t_u,t_f))
The results are nice, except for two points : the K_f_xx[0,0] and K_f_xx[-1,-1] For the above two points, nan values are obtained. For three GradientTape times, no nan value was obtained, but for four GradientTape times, a "nan" value came out. Therefore, I want to know are there any problem by using more than four GradientTape? If not, what would be the problem?