- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
triggered tf.function retracing. Tracing is expensive"-6ren"> triggered tf.function retracing. Tracing is expensive"-我认为这个错误来自形状问题,但我不知道在哪里。完整的错误消息建议执行以下操作: Also, tf.function has experimental_relax_shapes=True option -6ren">
我认为这个错误来自形状问题,但我不知道在哪里。完整的错误消息建议执行以下操作:
Also, tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing.
@tf.function(experimental_relax_shapes=True)
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
print(f'Tensorflow version {tf.__version__}')
from tensorflow import keras
from tensorflow.keras.layers import Dense, Conv1D, GlobalAveragePooling1D, Embedding
import tensorflow_datasets as tfds
from tensorflow.keras.models import Model
(train_data, test_data), info = tfds.load('imdb_reviews/subwords8k',
split=[tfds.Split.TRAIN, tfds.Split.TEST],
as_supervised=True, with_info=True)
padded_shapes = ([None], ())
train_dataset = train_data.shuffle(25000).\
padded_batch(padded_shapes=padded_shapes, batch_size=16)
test_dataset = test_data.shuffle(25000).\
padded_batch(padded_shapes=padded_shapes, batch_size=16)
n_words = info.features['text'].encoder.vocab_size
class ConvModel(Model):
def __init__(self):
super(ConvModel, self).__init__()
self.embe = Embedding(n_words, output_dim=16)
self.conv = Conv1D(32, kernel_size=6, activation='elu')
self.glob = GlobalAveragePooling1D()
self.dens = Dense(2)
def call(self, x, training=None, mask=None):
x = self.embe(x)
x = self.conv(x)
x = self.glob(x)
x = self.dens(x)
return x
conv = ConvModel()
conv(next(iter(train_dataset))[0])
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
train_loss = tf.keras.metrics.Mean()
test_loss = tf.keras.metrics.Mean()
train_acc = tf.keras.metrics.CategoricalAccuracy()
test_acc = tf.keras.metrics.CategoricalAccuracy()
optimizer = tf.keras.optimizers.Adam(learning_rate=1e-3)
@tf.function
def train_step(inputs, labels):
with tf.GradientTape() as tape:
logits = conv(inputs, training=True)
loss = loss_object(labels, logits)
train_loss(loss)
train_acc(logits, labels)
gradients = tape.gradient(loss, conv.trainable_variables)
optimizer.apply_gradients(zip(gradients, conv.trainable_variables))
@tf.function
def test_step(inputs, labels):
logits = conv(inputs, training=False)
loss = loss_object(labels, logits)
test_loss(loss)
test_acc(logits, labels)
def learn():
train_loss.reset_states()
test_loss.reset_states()
train_acc.reset_states()
test_acc.reset_states()
for text, target in train_dataset:
train_step(inputs=text, labels=target)
for text, target in test_dataset:
test_step(inputs=text, labels=target)
def main(epochs=2):
for epoch in tf.range(1, epochs + 1):
learn()
template = 'TRAIN LOSS {:>5.3f} TRAIN ACC {:.2f} TEST LOSS {:>5.3f} TEST ACC {:.2f}'
print(template.format(
train_loss.result(),
train_acc.result(),
test_loss.result(),
test_acc.result()
))
if __name__ == '__main__':
main(epochs=1)
最佳答案
TF/DR:此错误的根本原因是 train_data
的形状发生了变化这因批次而异。修复 train_data
的大小/形状解决此跟踪警告。我更改了以下行,然后一切都按预期进行。完整要点是 here
padded_shapes = ([9000], ())#None.
细节:
WARNING:tensorflow:10 out of the last 11 calls to <function train_stepat 0x7f4825f6d400> triggered tf.function retracing. Tracing isexpensive and the excessive number of tracings could be due to (1)creating @tf.function repeatedly in a loop, (2) passing tensors withdifferent shapes, (3) passing Python objects instead of tensors. For(1), please define your @tf.function outside of the loop. For (2),@tf.function has experimental_relax_shapes=True option that relaxesargument shapes that can avoid unnecessary retracing.
train_step
的两个参数和
test_step
是张量对象。所以根本原因是警告中提到的原因(2)。
train_data
的尺寸时,它打印了不同的尺寸。所以我试着垫
train_data
所以所有批次的形状都是相同的。
padded_shapes = ([9000], ())#None. # this line throws tracing error as the shape of text is varying for each step in an epoch.
# as the data size is varying, tf.function will start retracing it
# For the demonstration, I used 9000 as max length, but please change it accordingly
关于python - tensorflow 2 : Getting "WARNING:tensorflow:9 out of the last 9 calls to <function> triggered tf.function retracing. Tracing is expensive",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61647404/
我完全按照这里提到的做了:http://developer.android.com/tools/help/proguard.html .我签署了 apk。 这是从 LogCat 窗口保存的日志: 07
在讨论我的问题之前,这里是我的程序与我的问题相关的快速小总结,以便您理解。我的程序与机器人相关,下面代表我的程序对机器人执行的一些命令。用户输入的命令存储在commandList.size数组中。 L
我很难理解 retrace.sh 在我的 Android 应用程序上提供的调试信息。为什么显示2个方法? at java.lang.Thread.run(Thread.java:1027) Cause
我在命令提示符下使用 retrace.bat -verbose mapping.txt obfuscated_trace.txt。 但它在 obfuscated_trace.txt 中没有显示。从项目
我需要从我的 proguard.trace 文件中删除行,如 E/AndroidRuntime(10237): 以便回溯工作。基本上我正在查看日志文件并且需要删除此行或回溯不起作用。我是否遗漏了什么或
我认为这个错误来自形状问题,但我不知道在哪里。完整的错误消息建议执行以下操作: Also, tf.function has experimental_relax_shapes=True option
我是一名优秀的程序员,十分优秀!