gpt4 book ai didi

python - 不支持将字符串转换为 int64

转载 作者:行者123 更新时间:2023-12-05 01:12:29 28 4
gpt4 key购买 nike

大家好,当我在下面运行以下代码时,我遇到了“UnimplementedError:不支持将字符串转换为 int64”的问题。你们能帮我解决这个问题吗?谢谢。

import tensorflow as tf

model = tf.keras.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(15, activation=tf.nn.softmax)
])
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
X_train = tf.zeros([3000, 45, 45], tf.float32, '3d')
y_list = list()
for i in range(0,3000):
y_list.append(str(i))
y_train = tf.convert_to_tensor(y_list, tf.string)
model.fit(X_train, y_train, epochs=3)

这是错误消息(我在 Jupyter Notebook 上运行,2.1.0 版本的 tensorflow,我实际程序中的实际 X_train 应该是用于图像的,但是我觉得这个创建的张量很好):

Train on 3000 samples
Epoch 1/3
32/3000 [..............................] - ETA: 4:22
---------------------------------------------------------------------------
UnimplementedError Traceback (most recent call last)
<ipython-input-111-1a89f1d94518> in <module>
14 y_list.append(str(i))
15 y_train = tf.convert_to_tensor(y_list, tf.string)
---> 16 model.fit(X_train, y_train, epochs=3)

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
817 max_queue_size=max_queue_size,
818 workers=workers,
--> 819 use_multiprocessing=use_multiprocessing)
820
821 def evaluate(self,

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
340 mode=ModeKeys.TRAIN,
341 training_context=training_context,
--> 342 total_epochs=epochs)
343 cbks.make_logs(model, epoch_logs, training_result, ModeKeys.TRAIN)
344

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py in run_one_epoch(model, iterator, execution_function, dataset_size, batch_size, strategy, steps_per_epoch, num_samples, mode, training_context, total_epochs)
126 step=step, mode=mode, size=current_batch_size) as batch_logs:
127 try:
--> 128 batch_outs = execution_function(iterator)
129 except (StopIteration, errors.OutOfRangeError):
130 # TODO(kaftan): File bug about tf function and errors.OutOfRangeError?

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py in execution_function(input_fn)
96 # `numpy` translates Tensors to values in Eager mode.
97 return nest.map_structure(_non_none_constant_value,
---> 98 distributed_function(input_fn))
99
100 return execution_function

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\eager\def_function.py in __call__(self, *args, **kwds)
566 xla_context.Exit()
567 else:
--> 568 result = self._call(*args, **kwds)
569
570 if tracing_count == self._get_tracing_count():

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\eager\def_function.py in _call(self, *args, **kwds)
630 # Lifting succeeded, so variables are initialized and we can run the
631 # stateless function.
--> 632 return self._stateless_fn(*args, **kwds)
633 else:
634 canon_args, canon_kwds = \

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\eager\function.py in __call__(self, *args, **kwargs)
2361 with self._lock:
2362 graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
-> 2363 return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
2364
2365 @property

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\eager\function.py in _filtered_call(self, args, kwargs)
1609 if isinstance(t, (ops.Tensor,
1610 resource_variable_ops.BaseResourceVariable))),
-> 1611 self.captured_inputs)
1612
1613 def _call_flat(self, args, captured_inputs, cancellation_manager=None):

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
1690 # No tape is watching; skip to running the function.
1691 return self._build_call_outputs(self._inference_function.call(
-> 1692 ctx, args, cancellation_manager=cancellation_manager))
1693 forward_backward = self._select_forward_and_backward_functions(
1694 args,

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\eager\function.py in call(self, ctx, args, cancellation_manager)
543 inputs=args,
544 attrs=("executor_type", executor_type, "config_proto", config),
--> 545 ctx=ctx)
546 else:
547 outputs = execute.execute_with_cancellation(

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
65 else:
66 message = e.message
---> 67 six.raise_from(core._status_to_exception(e.code, message), None)
68 except TypeError as e:
69 keras_symbolic_tensors = [

~\Anaconda3\envs\tf2\lib\site-packages\six.py in raise_from(value, from_value)

UnimplementedError: Cast string to int64 is not supported
[[node loss/output_1_loss/Cast (defined at <ipython-input-111-1a89f1d94518>:16) ]] [Op:__inference_distributed_function_544280]

Function call stack:
distributed_function

最佳答案

原来我应该将标签设为整数,然后让这些整数对应于字符串标签。有了这个,我的整数有 0 到 13,然后是一个大小为 13 的数组,其中包含字符串值。

关于python - 不支持将字符串转换为 int64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61882720/

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