TensorFlow UMAP.fit_transform() TypeError: __array__() 错误解决方法
这个问题可能是因为您正在使用 TensorFlow 的 Eager Execution 模式,而不是 Graph Execution 模式。在 Eager Execution 模式下,您不能使用'eval()'方法来评估 TensorFlow 张量。
如果您想使用'umap'库进行降维,可以使用以下方法:
- 将 TensorFlow 张量转换为 NumPy 数组,然后传递给'umap.fit_transform()'方法。例如:
import tensorflow as tf
import umap
# 创建一个 TensorFlow 张量
inputs = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
# 将 TensorFlow 张量转换为 NumPy 数组
inputs_np = inputs.numpy()
# 使用 umap 进行降维
umap_result = umap.fit_transform(inputs_np)
- 使用'tf.Session()'来运行图并获取张量的值,然后将其转换为 NumPy 数组。例如:
import tensorflow as tf
import umap
# 创建一个 TensorFlow 张量
inputs = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
# 创建一个 Session 对象
with tf.Session() as sess:
# 运行图并获取张量的值
inputs_val = sess.run(inputs)
# 将 TensorFlow 张量转换为 NumPy 数组
inputs_np = inputs_val.numpy()
# 使用 umap 进行降维
umap_result = umap.fit_transform(inputs_np)
请注意,第二种方法需要在'with tf.Session() as sess:'块中运行图,并且需要在每个张量上调用'sess.run()'来获取它的值。
原文地址: https://www.cveoy.top/t/topic/jr6m 著作权归作者所有。请勿转载和采集!