如何训练模型将输入分类为一个或多个 类

How to train a model to classify input to one or more classes

我使用此示例代码来训练模型,以 class将随机数验证为 10 classes

中的一个
import numpy as np
import tensorflow as tf
from tensorflow import keras

samples_number = 1000
features_number = 5
output_classes_number = 10
x_train = np.random.random((samples_number, features_number))
y_train = keras.utils.to_categorical(np.random.randint(output_classes_number, size=(samples_number, 1)), num_classes=output_classes_number)

model = Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
model.add(Dense(64, activation='relu', input_dim=features_number))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])
model.fit(x_train, y_train,
          epochs=1000,
          batch_size=128) 

在这个样本中,x_train[0]的一个样本值是

[0.54071786 0.31048455 0.87843899 0.88947151 0.89052953]

y_train[0] 的示例输出是

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

这意味着 x_train[0] 值映射到 7th 特征。

在这个在线示例示例代码中,每个输入只有一个匹配class。


如何更改我的代码以同时将 5 个特征训练为一个或多个 class?

例如,可能的 y_train[z] 值可能是 [0. 1. 0. 0. 1. 1. 0. 0. 1. 0.]?

只需将最终激活函数更改为sigmoid,您将获得每个class的概率,因此它允许多标签class化。

当然,您将需要反映这项新任务的标签,而您目前似乎没有。

完整示例:

import numpy as np
from tensorflow import keras

x_train = np.random.random((1000, 4))
y_train = np.random.randint(0, 2, (1000, 4))

多标签目标:

array([[0, 0, 0, 0],
       [0, 1, 0, 1],
       [1, 0, 0, 0],
       ...,
       [1, 1, 0, 0],
       [0, 1, 0, 1],
       [0, 1, 0, 1]])
model = keras.models.Sequential()
model.add(keras.layers.Dense(64, activation='relu', input_dim=4))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(4, activation='sigmoid'))

model.compile(loss='categorical_crossentropy',
              optimizer='sgd')

model.fit(x_train, y_train,
          epochs=10,
          batch_size=16)
model.predict(x_train)

每个class的概率:

array([[0.48028257, 0.48918256, 0.4759362 , 0.51707023],
       [0.460468  , 0.50321233, 0.5157731 , 0.51490146],
       [0.5088656 , 0.50617874, 0.47503173, 0.5145618 ],
       ...,
       [0.452385  , 0.48947614, 0.47086555, 0.51236445],
       [0.48170012, 0.475545  , 0.48153797, 0.49793705],
       [0.47959277, 0.5056894 , 0.45207116, 0.50883204]], dtype=float32)

如果用 softmax 完成,这些概率总和为 1,因为它只预测一个类别:

array([[0.2694298 , 0.21779475, 0.23155291, 0.28122255],
       [0.28732255, 0.24838863, 0.23328216, 0.23100664],
       [0.28733823, 0.24516277, 0.23259555, 0.23490342],
       ...,
       [0.28732476, 0.21751696, 0.24203528, 0.253123  ],
       [0.27158916, 0.26262963, 0.22158018, 0.244201  ],
       [0.27889836, 0.25647762, 0.20330393, 0.2613201 ]], dtype=float32)