我目前正在使用Tensorflow中的一个函数,该函数需要在scipy.optimize中使用lsq_linear()。但是,我需要运行它而不必急于执行,而是建立TF图。我尝试在函数之前加上@ tf.function,但是我不确定是否可以将tensorflow变量传递给scipy.optimize库,因为尝试时会不断遇到错误,例如
TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.
以下是一些示例代码:
@tf.function
def tf_lsq(A, b):
ones = np.squeeze(np.asarray(np.ones(A.shape[1], )))
neg_ones = -1 * ones
A_coo = csr_matrix(A)
result = lsq_linear(A_coo, b, bounds=(neg_ones, ones), lsq_solver='lsmr', lsmr_tol=1e-13, verbose=0).x
return result
我感谢能提供的任何帮助!