python - Tensorflow 2.0 - SparseCategoricalCrossentropy 的数据形状不正确
问题描述
使用SparseCategoricalCrossentropy
损失时,我不了解有关数据集形状不匹配的错误。根据SparseCategoricalCrossentropy
,输出似乎是正确的形式:
输入:[7 1]
输出:[7 3]
其中批量大小为 7。
我的环境当前运行:
python 3.7.4
tensorflow 2.0.0-rc0
numpy 1.17.2
pandas 0.24.2
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-5-fe1cc3e1dca1> in <module>
47 print(model.summary())
48
---> 49 model.fit(train.batch(BATCH_SIZE), epochs=EPOCHS, verbose=2)
50 model.evaluate(train, steps=None, verbose=1)
/usr/local/lib/python3.7/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)
732 max_queue_size=max_queue_size,
733 workers=workers,
--> 734 use_multiprocessing=use_multiprocessing)
735
736 def evaluate(self,
/usr/local/lib/python3.7/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, **kwargs)
322 mode=ModeKeys.TRAIN,
323 training_context=training_context,
--> 324 total_epochs=epochs)
325 cbks.make_logs(model, epoch_logs, training_result, ModeKeys.TRAIN)
326
/usr/local/lib/python3.7/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)
121 step=step, mode=mode, size=current_batch_size) as batch_logs:
122 try:
--> 123 batch_outs = execution_function(iterator)
124 except (StopIteration, errors.OutOfRangeError):
125 # TODO(kaftan): File bug about tf function and errors.OutOfRangeError?
/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py in execution_function(input_fn)
84 # `numpy` translates Tensors to values in Eager mode.
85 return nest.map_structure(_non_none_constant_value,
---> 86 distributed_function(input_fn))
87
88 return execution_function
/usr/local/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in __call__(self, *args, **kwds)
437 # Lifting succeeded, so variables are initialized and we can run the
438 # stateless function.
--> 439 return self._stateless_fn(*args, **kwds)
440 else:
441 canon_args, canon_kwds = \
/usr/local/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in __call__(self, *args, **kwargs)
1820 """Calls a graph function specialized to the inputs."""
1821 graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
-> 1822 return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
1823
1824 @property
/usr/local/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _filtered_call(self, args, kwargs)
1139 if isinstance(t, (ops.Tensor,
1140 resource_variable_ops.BaseResourceVariable))),
-> 1141 self.captured_inputs)
1142
1143 def _call_flat(self, args, captured_inputs, cancellation_manager=None):
/usr/local/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
1222 if executing_eagerly:
1223 flat_outputs = forward_function.call(
-> 1224 ctx, args, cancellation_manager=cancellation_manager)
1225 else:
1226 gradient_name = self._delayed_rewrite_functions.register()
/usr/local/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in call(self, ctx, args, cancellation_manager)
509 inputs=args,
510 attrs=("executor_type", executor_type, "config_proto", config),
--> 511 ctx=ctx)
512 else:
513 outputs = execute.execute_with_cancellation(
/usr/local/lib/python3.7/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 = [
~/Library/Python/3.7/lib/python/site-packages/six.py in raise_from(value, from_value)
InvalidArgumentError: assertion failed: [] [Condition x == y did not hold element-wise:] [x (loss/dense_3_loss/SparseSoftmaxCrossEntropyWithLogits/Shape_1:0) = ] [7 1] [y (loss/dense_3_loss/SparseSoftmaxCrossEntropyWithLogits/strided_slice:0) = ] [7 3]
[[node loss/dense_3_loss/SparseSoftmaxCrossEntropyWithLogits/assert_equal/Assert/Assert (defined at /usr/local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:1751) ]] [Op:__inference_distributed_function_2031]
Function call stack:
distributed_function
使用 tensorflow=2.0.0-beta0:
InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [34,3] and labels shape [2]
[[node loss/dense_2_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at <ipython-input-3-4fa88a5fad5e>:70) ]] [Op:__inference_keras_scratch_graph_5038]
有人可以解释或指出我正确的方向吗?
谢谢
以下代码重现了该错误:
import numpy as np
import pandas as pd
import random
import tensorflow as tf
INPUT_SHAPE=[3, 5]
NUM_POINTS=20
BATCH_SIZE=7
EPOCHS=4
def data_gen(num=10, in_shape=[5, 3]):
for i in range(num):
x = np.random.rand(in_shape[0], in_shape[1])
y = random.randint(0,2)
yield x, y
train = tf.data.Dataset.from_generator(
generator=data_gen,
output_types=(tf.float32, tf.int32),
args=([NUM_POINTS, INPUT_SHAPE])
)
def create_model(input_shape):
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(100, activation="tanh",input_shape=input_shape),
tf.keras.layers.Dense(3, activation="softmax", kernel_regularizer= tf.keras.regularizers.l2(0.001))
])
return model
model = create_model(input_shape=INPUT_SHAPE)
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4, clipvalue=1.0),
loss= tf.keras.losses.SparseCategoricalCrossentropy())
print(model.summary())
model.fit(train.batch(BATCH_SIZE), epochs=EPOCHS, verbose=2)
model.evaluate(train, steps=None, verbose=1)
型号总结:
模型:“sequential_1” _________________________________________________________________ 层(类型)输出形状参数#
==================================== ============================= dense_2(密集)(无,3、100)600
_________________________________________________________________密集_3(密集)(无,3 , 3) 303
============================================== =================== 总参数:903 可训练参数:903 不可训练参数:0
解决方案
推荐阅读
- powershell - Powershell 将对象数组转换为 PSCustomObject
- python - 从字典中写入一个 CSV,其中值作为标题键对齐在下
- cassandra - 在重新启动一个节点时,其他节点显示在 Cassandra 集群中
- android - 未解决的参考:viewModels (hilt)
- bash - 将 aws cli v2 重定向到文件会添加额外的控制字符
- reactjs - 搜索不适用于材料表中的链接文本
- cassandra - Cassandra 4.0 虚拟表如何读取数据?
- coq - 模式匹配两个列表,其类型依赖于 Coq
- node.js - 我们如何在 AWS EC2 上为 laravel (https//:example.com) 和 nodejs (https//:example.com:8443) 使用相同的域?
- powershell - PowerShell - 展开组对象