Python Tensorflow 2.0签名如何处理numpy手术?
显然,TF2.0 Autograph适用于Numpy操作 我在下面写了一个简单的例子,我使用Python Tensorflow 2.0签名如何处理numpy手术?,python,numpy,tensorflow,Python,Numpy,Tensorflow,显然,TF2.0 Autograph适用于Numpy操作 我在下面写了一个简单的例子,我使用autograph.to_code查看autograph对Numpy操作的作用。ag\uu.converted\u调用似乎完成了运行Numpy函数的工作。但我不知道在这个转换后的通话中会发生什么 我很好奇TF是如何用Numpy操作生成图形的。TF2.0是否将所有Numpy操作转换为TF ops?我们是否需要在TF2.0代码中过度使用Numpy操作来担心性能问题 @tf.function def run()
autograph.to_code
查看autograph对Numpy操作的作用。ag\uu.converted\u调用
似乎完成了运行Numpy函数的工作。但我不知道在这个转换后的通话中会发生什么
我很好奇TF是如何用Numpy操作生成图形的。TF2.0是否将所有Numpy操作转换为TF ops?我们是否需要在TF2.0代码中过度使用Numpy操作来担心性能问题
@tf.function
def run():
a = np.array([1, 2])
if a[0] > 1:
a[0] = 10
a = a.reshape(-1)
return tf.convert_to_tensor(a)
print(tf.autograph.to_code(run.python_function))
这将提供以下输出:
def tf__run():
do_return = False
retval_ = ag__.UndefinedReturnValue()
with ag__.FunctionScope('run', 'run_scope', ag__.ConversionOptions(recursive=True, user_requested=True, optional_features=(), internal_convert_user_code=True)) as run_scope:
a = ag__.converted_call(np.array, run_scope.callopts, ([1, 2],), None, run_scope)
def get_state():
return a[0],
def set_state(vals):
a[0], = vals
def if_true():
a_1, = a,
a_1[0] = 10
a_1 = ag__.converted_call(a_1.reshape, run_scope.callopts, (-1,), None, run_scope)
return a_1
def if_false():
return a
cond = a[0] > 1
a = ag__.if_stmt(cond, if_true, if_false, get_state, set_state, ('a',), ('a[0]',))
do_return = True
retval_ = run_scope.mark_return_value(ag__.converted_call(tf.convert_to_tensor, run_scope.callopts, (a,), None, run_scope))
do_return,
return ag__.retval(retval_)
如报告中所述
tf.function与TensorFlow ops配合使用效果最佳;NumPy和Python调用被转换为常量
建议使用TensorFlow ops。此外,虽然混合使用NumPy和TensorFlow操作可以在渴望模式下工作,但启用tf.function时可能会出错
如果你想使用NumPy ops,你可以试试