我想对SparseCategoricalCrossentropy函数做一个简单的测试,看看它到底对输出做了什么。为此,我使用了MobileNetV2的最后一层的输出。
import keras.backend as K
full_model = tf.keras.applications.MobileNetV2(
input_shape=(224,224,3),
alpha=1.0,
include_top=True,
weights="imagenet",
input_tensor=None,
pooling=None,
classes=1000,
classifier_activation="softmax",)
func = K.function(full_model.layers[1].input, full_model.layers[155].output)
conv_output = func([processed_image])
y_pred = np.single(conv_output)
y_true = np.zeros(1000).reshape(1,1000)
y_true[0][282] = 1
scce = tf.keras.losses.SparseCategoricalCrossentropy()
scce(y_true, y_pred).numpy()
processed_image
是先前创建的1x224x224x3数组。
我得到错误ValueError: Shape mismatch: The shape of labels (received (1000,)) should equal the shape of logits except for the last dimension (received (1, 1000)).
我尝试重塑数组以匹配错误提到的尺寸,但它似乎不起作用。它能接受什么形状?
由于您使用的是SparseCategoricalCrossentropy
损失函数,因此y_true
的形状应为[batch_size]
,y_pred
的形状应为[batch_size, num_classes]
。此外,y_true
应该由整数值组成。请参阅文档。在您的具体示例中,您可以尝试这样做:
import keras.backend as K
import tensorflow as tf
import numpy as np
full_model = tf.keras.applications.MobileNetV2(
input_shape=(224,224,3),
alpha=1.0,
include_top=True,
weights="imagenet",
input_tensor=None,
pooling=None,
classes=1000,
classifier_activation="softmax",)
batch_size = 1
processed_image = tf.random.uniform(shape=[batch_size,224,224,3])
func = K.function(full_model.layers[1].input,
full_model.layers[155].output)
conv_output = func([processed_image])
y_pred = np.single(conv_output)
# Generates an integer between 0 and 999 representing a class index.
y_true = np.random.randint(low = 0, high = 999, size = batch_size)
# [984]
scce = tf.keras.losses.SparseCategoricalCrossentropy()
scce(y_true, y_pred).numpy()
# y_pred encodes a probability distribution here and the calculated loss is 10.69202
您可以尝试batch_size
,看看一切是如何工作的。在上面的例子中,我只是使用了1的batch_size
。