我有一个NN,通过Model.fit()训练需要相当长的时间。我总是得到以下信息: WARNING:tensorflow:7 out of the last 12 calls to <function Model.make_train_function.<locals>.train_function at 0x7f128aee0ae8> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creati
我试图使用构建在类似体系结构上的四种TensorFlow模型生成4种不同类型的预测。当我使用相同的特性四次调用model.predict()函数时,我会得到以下警告:
WARNING:tensorflow:5 out of the last 9 calls to <function Model.make_predict_function.<locals>.predict_function at 0x2b18970e88c0> triggered tf.function retracing. Tracing is expensive and the excessive n
有人知道这个错误的原因吗?
WARNING:tensorflow:No training configuration found in the save file, so the model was *not* compiled. Compile it manually.
WARNING:tensorflow:11 out of the last 11 calls to <function Model.make_predict_function.<locals>.predict_function at 0x000001F9D1C05EE0> triggered tf.f
这是造成问题的代码。
def cost_func(x=None, y=None):
if not x:
tf.compat.v1.disable_eager_execution()
x = tf.compat.v1.placeholder(tf.float32, shape=[None, 1])
if not y:
tf.compat.v1.disable_eager_execution()
y = tf.compat.v1.placeholder(tf.float32, shape=[None, 1])
我的训练循环给了我以下警告:
警告:tensorflow:当损失最小化时,变量‘噪音:0’不存在梯度。
经过一些修改后,我确定只有当噪声变量作为参数传递给我的损失函数(即tf.function )时,才会发生这种情况。下面的代码显示,当损失函数不是tf.function或函数中引用全局噪声变量时,没有问题。它还显示,当噪声变量在tf.function中用作参数时,尝试对其执行梯度会导致错误:
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
from tensorflow_p
我在下面的例子中观察到的行为让我感到困惑: import tensorflow as tf
@tf.function
def f(a):
c = a * 2
b = tf.reduce_sum(c ** 2 + 2 * c)
return b, c
def fplain(a):
c = a * 2
b = tf.reduce_sum(c ** 2 + 2 * c)
return b, c
a = tf.Variable([[0., 1.], [1., 0.]])
with tf.GradientTape() as tape:
我想在tf.function中的张量上使用for循环,如下所示:
@tf.function
def test(x):
for i in range(tf.shape(x)[0]):
print(i)
我的定义是:
S = tf.random.uniform([2,2],0,1)
然后
test(S)
给出
Tensor("while/Placeholder:0", shape=(), dtype=int32)
而
for i in range(tf.shape(S)[0]):
print(i)
返回
0
1
为什么我不能循环计算tf.functi
在阅读this和this answer时,我了解到在TensorFlow2.0上使用非动态模型时,Keras将使用AutoGraph。但是现在编写一个回调来获取训练期间变量的历史记录, class TrainHistory(tf.keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.vars = []
def on_batch_end(self, batch, logs={}):
self.vars.append([v.numpy() for v in self.
我想用梯度做一些线性代数(例如tf.matmul)。默认情况下,梯度作为张量列表返回,其中张量可能具有不同的形状。我的解决方案是将梯度重塑成一个单一的向量。这在急切的模式下工作,但是现在我想使用tf.function编译我的代码。在图形模式(tf.function)中,似乎没有办法编写一个可以“平缓”渐变的函数。
grad = [tf.ones((2,10)), tf.ones((3,))] # an example of what a gradient from tape.gradient can look like
# this works for flattening the gra
对于一些自定义代码,我需要运行一个For循环在Tensorflow 2中动态创建一个变量(启用了急切执行模式)。(在我的自定义代码中,我写入变量的值将需要渐变,因此我希望跟踪for循环中的计算,以便可以从autodiff获得渐变)。我的代码可以工作,但速度非常慢。事实上,它比在numpy中执行相同的操作慢几个数量级。 我已经隔离了问题,并提供了一个突出显示问题的玩具代码片段。修复它将允许我修复我的自定义代码。 import numpy as np
import tensorflow as tf
import timeit
N = int(1e5)
data = np.random.rand
我已经创建了keras函数API模型,现在我正在尝试查看它的层输出,为每一层创建子模型,每一层从原始模型的输入开始,以我选择的层结束。我不明白怎样做才是正确的,而不是
WARNING:tensorflow:11 out of the last 11 calls to
<function Model.make_predict_function.<locals>.predict_function at 0x7fb8ebc92700>
triggered tf.function retracing.
Tracing is expensive and the excessi
在Python2中,@tf.function装饰器允许TensorFlow函数变成TensorFlow图(或多或少),并且可以带来一些性能改进。但是,当以这种方式装饰时,Python no longer traces the functions each time they run。这使得使用Python调试器调试函数变得更加困难。有没有一种方法可以暂时禁用所有@tf.function装饰器以方便调试?