是梯度的用途数量'在Gradienttape Limited中?

发布于 2025-02-10 03:29:10 字数 2419 浏览 2 评论 0原文

我正在尝试解决以下问题:

因此,我尝试使用tf.gradienttape 4次尝试。以下是我的代码:

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))

结果很好,除了两个点:k_f_xx [0,0]和k_f_xx [-1,-1] 对于上述两个点,获得NAN值。对于三个梯度时间,没有获得NAN值,但是对于四个渐变时间,“ NAN”值出现了。因此,我想知道使用超过四个渐变tape是否有任何问题?如果没有,那是什么问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文