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))))
我希望這有幫助。
添加回答
舉報