简体   繁体   English

我最后一个密集的 keras 层有什么问题?

[英]What is the issue with my last dense keras layer?

I am working on a small NN in keras for multi-class classification problem.我正在研究 keras 中的一个小型 NN,用于解决多类分类问题。 I have 9 different labels and my features are also 9.我有 9 个不同的标签,我的特征也是 9 个。

My train/test shapes are the following:我的火车/测试形状如下:

Sets shape:
x_train shape: (7079, 9)
y_train shape: (7079,)
x_test shape: (7079, 9)
y_test shape: (7079,)

But when I try to make them categorical:但是当我试图让它们分类时:

y_train = tf.keras.utils.to_categorical(y_train, num_classes=9)
y_test = tf.keras.utils.to_categorical(y_test, num_classes=9)

I get the following error:我收到以下错误:

IndexError: index 9 is out of bounds for axis 1 with size 9

Here is more info about the y_train这是有关y_train的更多信息

print(np.unique(y_train)) # [1. 2. 3. 4. 5. 6. 7. 8. 9.]
print(len(np.unique(y_train))) # 9

Anyone would know what the problem is?任何人都会知道问题是什么?

The shape of the y_train is 1D . y_train的形状是1D You have to make it one-hot encoded.您必须对其进行一次热编码。 Something like就像是

y_train = tf.keras.utils.to_categorical(y_train , num_classes=9)

And same goes for y_test too. y_test也是如此。

Update更新

According to the doc ,根据文档

tf.keras.utils.to_categorical(y, num_classes=None, dtype="float32")

Here, y : class vector to be converted into a matrix (integers from 0 to num_classes ).这里, y : class 向量被转换成矩阵(从0num_classes的整数)。 As in your case, y_train is something like [1,2,..] .与您的情况一样, y_train类似于[1,2,..] You need to do as follows:您需要执行以下操作:

y_train = tf.keras.utils.to_categorical(y_train - 1, num_classes=9)

Here is an example for reference.这是一个供参考的例子。 If we do如果我们这样做

class_vector = np.array([1, 1, 2, 3, 5, 1, 4, 2])
print(class_vector)

output_matrix = tf.keras.utils.to_categorical(class_vector, 
                                      num_classes = 5, dtype ="float32")
print(output_matrix)
[1 1 2 3 5 1 4 2]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-15-69c8be7a0f1a> in <module>()
      6 print(class_vector)
      7 
----> 8 output_matrix = tf.keras.utils.to_categorical(class_vector, num_classes = 5, dtype ="float32")
      9 print(output_matrix)

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/np_utils.py in to_categorical(y, num_classes, dtype)
     76   n = y.shape[0]
     77   categorical = np.zeros((n, num_classes), dtype=dtype)
---> 78   categorical[np.arange(n), y] = 1
     79   output_shape = input_shape + (num_classes,)
     80   categorical = np.reshape(categorical, output_shape)

IndexError: index 5 is out of bounds for axis 1 with size 5

To solve this, we convert the data to a zero-based format.为了解决这个问题,我们将数据转换为从零开始的格式。

output_matrix = tf.keras.utils.to_categorical(class_vector - 1, 
                                     num_classes = 5, dtype ="float32")
print(output_matrix)

[[1. 0. 0. 0. 0.]
 [1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 1. 0. 0. 0.]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM