我必须两次计算梯度才能实现优化程序,但是第二个梯度(g_n)返回None
for i in range(1):
with tf.GradientTape(persistent= True) as tape:
ypredicted = linear_model(Xtrain)
l = squared_loss(ytrain, ypredicted)
g = tape.gradient(l, w)
radius = 1
sigma = np.float64(np.random.randn())
RS = tf.fill((number_feature,1), radius*sigma)
W_N = tf.math.add(w, RS)
S = tf.math.subtract (w, W_N)
g_n = tape.gradient(l, tf.Variable(W_N, trainable = True))