Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多类别情况下最后一个密集输出层的单位_Python_Tensorflow_Keras_Neural Network_Cross Entropy - Fatal编程技术网

Python 多类别情况下最后一个密集输出层的单位

Python 多类别情况下最后一个密集输出层的单位,python,tensorflow,keras,neural-network,cross-entropy,Python,Tensorflow,Keras,Neural Network,Cross Entropy,我目前正在做这个。任务是把句子分成一个特定的类别。因此,我们有一个多类别的问题,而不是二元问题,比如根据某些复习句子预测复习的情绪(积极/消极)。对于多个类别,我认为最后一层的单位/神经元数量必须与我想要预测的类别数量相匹配。所以当我有一个二进制问题时,我使用一个神经元,表示0或1。当我有5节课时,我需要5个单元。我就是这么想的 但是,在colab的代码中有以下内容: model = tf.keras.Sequential([ tf.keras.layers.Embedding(voca

我目前正在做这个。任务是把句子分成一个特定的类别。因此,我们有一个多类别的问题,而不是二元问题,比如根据某些复习句子预测复习的情绪(积极/消极)。对于多个类别,我认为最后一层的单位/神经元数量必须与我想要预测的类别数量相匹配。所以当我有一个二进制问题时,我使用一个神经元,表示0或1。当我有5节课时,我需要5个单元。我就是这么想的

但是,在colab的代码中有以下内容:

model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),
    tf.keras.layers.GlobalAveragePooling1D(),
    tf.keras.layers.Dense(24, activation='relu'),
    tf.keras.layers.Dense(6, activation='softmax')
])
model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model.summary()
当我在这个colab中运行model.fit代码部分时,它确实起作用了。但我不明白。当我检查

print(label_tokenizer.word_index)
print(label_tokenizer.word_docs)
print(label_tokenizer.word_counts)
这给

{'sport': 1, 'business': 2, 'politics': 3, 'tech': 4, 'entertainment': 5}
defaultdict(<class 'int'>, {'tech': 401, 'business': 510, 'sport': 511, 'entertainment': 386, 'politics': 417})
OrderedDict([('tech', 401), ('business', 510), ('sport', 511), ('entertainment', 386), ('politics', 417)])
{'sport':1,'business':2,'politics':3,'tech':4,'entertainment':5}
defaultdict(,{'tech':401,'business':510,'sport':511,'entertainment':386,'politics':417})
订单信息技术([('tech',401),('business',510),('sport',511),('entertainment',386),('politics',417)])
显然是5节课。但是,当我将模型调整为
tf.keras.layers.Dense(5,activation='softmax')
并运行model.fit命令时,它不起作用。精度始终为0


为什么这里的6是正确的而不是5?

是6,因为编码目标在[1,5]中,但keras sparse_cat从0创建一个热标签,因此它创建另一个不可用的标签(0)

要使用
Dense(5,activation='softmax')
只需执行y-1操作,即可在[0,4]中获取标签,并从0开始获取标签

通过colab链接,您可以更改:

model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),
    tf.keras.layers.GlobalAveragePooling1D(),
    tf.keras.layers.Dense(24, activation='relu'),
    tf.keras.layers.Dense(5, activation='softmax')
])

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

history = model.fit(train_padded, training_label_seq-1, epochs=num_epochs, validation_data=(validation_padded, validation_label_seq-1), verbose=2)

是6,因为编码目标在[1,5]中,但keras sparse_cat从0创建一个热标签,因此它创建另一个不可用的标签(0)

要使用
Dense(5,activation='softmax')
只需执行y-1操作,即可在[0,4]中获取标签,并从0开始获取标签

通过colab链接,您可以更改:

model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),
    tf.keras.layers.GlobalAveragePooling1D(),
    tf.keras.layers.Dense(24, activation='relu'),
    tf.keras.layers.Dense(5, activation='softmax')
])

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

history = model.fit(train_padded, training_label_seq-1, epochs=num_epochs, validation_data=(validation_padded, validation_label_seq-1), verbose=2)