我正在使用tensorflow while_loop。问题是我在while_loop主体中创建的变量将被添加到可训练变量中。例如:
def body(b, c):
a = b + c
...
(others calculations that create variables like a)
...
return b, c
A被添加到trainable_variables
我的训练循环给了我以下警告:
警告:tensorflow:当损失最小化时,变量‘噪音:0’不存在梯度。
经过一些修改后,我确定只有当噪声变量作为参数传递给我的损失函数(即tf.function )时,才会发生这种情况。下面的代码显示,当损失函数不是tf.function或函数中引用全局噪声变量时,没有问题。它还显示,当噪声变量在tf.function中用作参数时,尝试对其执行梯度会导致错误:
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
from tensorflow_p
我希望在自定义NN架构上实现GradientTape(),但我没有看到任何关于如何使用它来计算偏差的解释。here也回答了类似的问题,但没有得到完全回答。 举个简单的例子,我的神经网络的训练步骤如下: self.W = ## Initialized earlier on
self.b = ## Initialized earlier on
@tf.function
def train(self):
with tf.GradientTape() as tape:
pred = self.feedforward()
los
我现在正在一个定制的训练循环中训练一个模型。因此,我需要访问model.trainable_variables。但是,我的模型有keras.engine.sequential.Sequential object at 0x7fa284a24d30类型,而不是tensorflow.python.keras.engine.sequential.Sequential类型(参见)。
因此,当我试图获取可训练变量时,就会得到错误:
AttributeError: 'function' object has no attribute 'trainable_variables'
我试图在TensorFlow 2.0中尽可能地执行最基本的函数最小化,与问题Tensorflow 2.0: minimize a simple function中的完全一样,但是我无法让那里描述的解决方案工作。这是我的尝试,大部分是复制粘贴的,但添加了一些似乎缺少的部分。 import tensorflow as tf
x = tf.Variable(2, name='x', trainable=True, dtype=tf.float32)
with tf.GradientTape() as t:
y = tf.math.square(x)
# Is the t
我正在从外部源读取梯度值(即计算在其他地方完成,但我想在“主”网络中累积不同的源),我只想在tensorflow中使用apply_gradients() op。问题是,梯度以浮动的形式被发送进来。有什么方法可以使用浮点数组来应用带有内置Optimizer函数的梯度吗?
在一个非常小的例子/测试用例中,这基本上是我想要做的。
W = tf.Variable(1.0)
b = tf.Variable(2.0)
trainable_variables = [W, b]
gradients = [0.05, 0.01] # Example gradients for W, b
# ... Som