2 回答
TA贡献1805条经验 获得超9个赞
您当然应该利用 TF 2.x 的优势,包括 Eager Execution。它不仅非常方便,而且效率更高。
import tensorflow as tf
def get_values():
A = tf.random.normal([10_000,10_000])
B = tf.random.normal([10_000,10_000])
return A,B
@tf.function
def compute():
A,B = get_values()
return tf.reduce_sum(tf.matmul(A,B))
print(compute())
您(大部分)在 TF 2.x 中不再需要任何会话,Auto Graph 会自动为您完成。
只需使用注释“主要”功能@tf.function(无需注释其他功能,例如get_values,这也会自动发生)。
TA贡献2039条经验 获得超8个赞
关于您的第一个问题,我能够让它在 Colab 中运行,并添加了“disable_eager_execution()”调用 - 似乎 TF 2.0 中默认启用了“eager execution”模式:
# Install TensorFlow
try:
# %tensorflow_version only exists in Colab.
%tensorflow_version 2.x
except Exception:
pass
import numpy as np
import tensorflow as tf
from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()
print(tf.executing_eagerly())
print(tf.__version__)
matdim = 1000
try:
Session = tf.Session
except AttributeError:
Session = tf.compat.v1.Session
A = tf.random.normal([matdim,matdim])
B = tf.random.normal([matdim,matdim])
with Session() as sess:
print(sess.run(tf.reduce_sum(tf.matmul(A,B))))
我希望这有帮助。
添加回答
举报
