tensorflow:可以使用一种热编码来构建具有分类目标的神经网络吗?

问题描述 投票:0回答:1

我有一个数据集,其中的目标是客户购买的产品类别。每个客户可以购买一个或多个类别。

像这样:

enter image description here

以前,我建立此模型是为了与一个目标(每个客户购买或不购买)一起工作:

model = tf.keras.Sequential([
                            tf.keras.layers.Dense(hidden_layer_size, activation='tanh'), 
                            tf.keras.layers.Dense(hidden_layer_size, activation='tanh'), 
                            tf.keras.layers.Dense(output_size, activation='softmax') 
                            ])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

batch_size = 20000
max_epochs = 200
early_stopping = tf.keras.callbacks.EarlyStopping(patience=2) 
model.fit(train_inputs,
          train_targets,
          batch_size = batch_size,
          epochs = max_epochs,
          callbacks = [early_stopping],
          validation_data = (validation_inputs, validation_targets),
          verbose=2)

在创建目标之前,必须先使用一种热编码生成数据集,然后将其用作[n x 5]张量?使用一种热编码,例如,客户端1具有目标:[0,0,1,0,1]:

enter image description here

我如何建立模型以与这样的目标一起使用?

或者目标可以在模型内部用张量流编码?

对不起,如果我的问题有点基本,但是我刚开始使用tensorflow

python tensorflow target one-hot-encoding
1个回答
0
投票

您正在寻找多标签分类。

在这种情况下,您的输出仍将是一个热编码,但会有多个目标。例如,[0, 0, 0, 0, 1, 0, 1]

您的网络结构将是相同的,除了最终激活必须是sigmoid而不是softmax,并且将以binary_crossentropy损失训练模型。

© www.soinside.com 2019 - 2024. All rights reserved.