gpt4 book ai didi

tensorflow - tf.keras.utils.to_categorical 在图形模式下引发 TypeError

转载 作者:行者123 更新时间:2023-12-05 06:15:42 26 4
gpt4 key购买 nike

我有一个稀疏分类张量,我想将其转换为单热编码表示。我可以让它在急切模式下工作,但不能在图形模式下工作。我不明白这是怎么回事。

具体来说,假设我有一个稀疏分类

y=tf.constant(np.random.choice([0,1,2],2).reshape(-1,1))

我想将其转换为一种热编码表示形式。我定义了一个函数

def tmp(y):
return tf.keras.utils.to_categorical(y)

它按预期工作。 但是,如果我包装函数

@tf.function
def tmp(y):
return tf.keras.utils.to_categorical(y)

然后我得到异常

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-bb4411a7c6f7> in <module>
----> 1 tmp(y)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
578 xla_context.Exit()
579 else:
--> 580 result = self._call(*args, **kwds)
581
582 if tracing_count == self._get_tracing_count():

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
625 # This is the first call of __call__, so we have to initialize.
626 initializers = []
--> 627 self._initialize(args, kwds, add_initializers_to=initializers)
628 finally:
629 # At this point we know that the initialization is complete (or less

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
504 self._concrete_stateful_fn = (
505 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
--> 506 *args, **kwds))
507
508 def invalid_creator_scope(*unused_args, **unused_kwds):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
2444 args, kwargs = None, None
2445 with self._lock:
-> 2446 graph_function, _, _ = self._maybe_define_function(args, kwargs)
2447 return graph_function
2448

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
2775
2776 self._function_cache.missed.add(call_context_key)
-> 2777 graph_function = self._create_graph_function(args, kwargs)
2778 self._function_cache.primary[cache_key] = graph_function
2779 return graph_function, args, kwargs

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
2665 arg_names=arg_names,
2666 override_flat_arg_shapes=override_flat_arg_shapes,
-> 2667 capture_by_value=self._capture_by_value),
2668 self._function_attributes,
2669 # Tell the ConcreteFunction to clean up its graph once it goes out of

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
979 _, original_func = tf_decorator.unwrap(python_func)
980
--> 981 func_outputs = python_func(*func_args, **func_kwargs)
982
983 # invariant: `func_outputs` contains only Tensors, CompositeTensors,

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
439 # __wrapped__ allows AutoGraph to swap in a converted function. We give
440 # the function a weak reference to itself to avoid a reference cycle.
--> 441 return weak_wrapped_fn().__wrapped__(*args, **kwds)
442 weak_wrapped_fn = weakref.ref(wrapped_fn)
443

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
966 except Exception as e: # pylint:disable=broad-except
967 if hasattr(e, "ag_error_metadata"):
--> 968 raise e.ag_error_metadata.to_exception(e)
969 else:
970 raise

TypeError: in user code:

<ipython-input-11-4e328cd877a4>:3 tmp *
return tf.keras.utils.to_categorical(y)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/np_utils.py:49 to_categorical **
y = np.array(y, dtype='int')

TypeError: __array__() takes 1 positional argument but 2 were given

从表面上看,它看起来像一个 numpy 错误,但我验证了我的 numpy 安装有两个参数,所以出了什么问题?

更一般地说,在图形模式下将备用分类张量转换为热编码张量的方法是什么?

最佳答案

您可以使用 tf.one_hot 而不是 tf.keras.utils.to_categorical 作为解决方法。它在 eager 模式和图形模式下都按预期工作。

完整代码请引用下图

import tensorflow as tf
print(tf.__version__)
import numpy as np
y=tf.constant(np.random.choice([0,1,2],[2,2]))
print(y)

输出:

2.3.0
tf.Tensor(
[[1 0]
[2 1]], shape=(2, 2), dtype=int64)

使用 tf.keras.utils.to_categorical 的 Eager 模式

def tmp(y):
return tf.keras.utils.to_categorical(y)
tmp(y)

输出:

array([[[0., 1., 0.],
[1., 0., 0.]],

[[0., 0., 1.],
[0., 1., 0.]]], dtype=float32)

使用 tf.one_hot 的 Eager 模式

def tmp(y):
return tf.one_hot(y,3)
tmp(y)

输出:

<tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
array([[[0., 1., 0.],
[1., 0., 0.]],

[[0., 0., 1.],
[0., 1., 0.]]], dtype=float32)>

使用 tf.one_hot 的图表模式

@tf.function
def tmp(y):
return tf.one_hot(y,3)
tmp(y)

输出:

<tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
array([[[0., 1., 0.],
[1., 0., 0.]],

[[0., 0., 1.],
[0., 1., 0.]]], dtype=float32)>

关于tensorflow - tf.keras.utils.to_categorical 在图形模式下引发 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62378837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com