梯度张量流返回无

时间:2020-10-10 13:09:41

标签: python tensorflow

我必须两次计算梯度才能实现优化程序,但是第二个梯度(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)) 

0 个答案:

没有答案