我有一个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
在阅读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.
我是坦索弗洛的新手。我想建立一个三层神经网络,
i) i在tf.function中声明了权重W,并为此抛出了以下错误:
ValueError: tf.function-decorated function tried to create variables on non-first call.
ii),我还创建了sample_batch的字典数据,其中包含了x:Featureandy:标签,我想传递给tf.function并使用它,later.But,我不知道该如何做呢?
下面是我的代码片段:
@tf.function
def forward_pass1(batch):
p
我从.I学习了tensorflow2.0 2.0,可以从下面的代码中了解结果。
def square_if_positive(x):
return [i ** 2 if i > 0 else i for i in x]
square_if_positive(range(-5, 5))
# result
[-5, -4, -3, -2, -1, 0, 1, 4, 9, 16]
但是如果我用张量而不是python代码来改变输入,就像这样
def square_if_positive(x):
return [i ** 2 if i > 0 else i for i in x]
在TensorFlow 2.0的tf.function中,有没有tensor.numpy()的替代品?问题是,当我试图在装饰的函数中使用它时,我得到了错误消息'Tensor' object has no attribute 'numpy',而它在外部运行时没有任何问题。 通常,我会选择像tensor.eval()这样的东西,但它只能在TF会话中使用,并且在TF2.0中不再有会话。
我有一个@tf.function装饰的函数。在函数中,我想从一个分布中提取一些值并计算几次(让我们调用这个过程f(x))。
如何在Tensorflow 2.0中做到这一点?我不能使用numpy数组,因为我想使用@tf.function装饰器。
numpy实现如下所示:
reps = 4
store = np.zeros((n, reps))
for i in range(reps):
store[:, i] = f(x) #f(x) is shape (n,)
然后,目标将是计算store的行均值。
这应该很容易,但我还没有想出怎么做!
我是坦索弗洛的新手。已经尝试过这个简单的例子:
import tensorflow as tf
sess = tf.Session()
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
z = x + y
print(sess.run(z, feed_dict={x: 3.0, y: 4.5}))
并得到一些警告The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.和正确的答案- 7.5
在阅读了之后,我了解到这些警告是由于从
我正试图在tf.function中打印最后一批的结果
import tensorflow as tf
def small_data():
for i in range(10):
yield 3, 2
data = tf.data.Dataset.from_generator(
small_data, (tf.int32, tf.int32), )
def result(data):
"""
Psuedo code for a model which outputs multiple layer outputs
我第一次尝试了Tensorflow 2.0。这是习语吗?
@tf.function
def add(a,b):
return a+b
if __name__=="__main__":
result=add(1.0,2.0)
print(result)
print(tf.keras.backend.get_value(result))
但是,我得到了与add函数相关的警告:
WARNING:tensorflow:Entity <function add at 0x7ff34781a2f0> could not be transfor
我试图使用构建在类似体系结构上的四种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
我在下面的例子中观察到的行为让我感到困惑: 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.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