简体   繁体   English

为什么我的 Tensorflow Keras model output 在训练时会出现奇怪的损失和准确度值?

[英]Why does my Tensorflow Keras model output weird loss and accuracy values while training?

I have trained a custom text classifier in Tensorflow with python for classifying sentences into questions/sentences containing information using this code:我已经在 Tensorflow 和 python 中训练了一个自定义文本分类器,用于使用以下代码将句子分类为包含信息的问题/句子:

import tensorflow as tf
from tensorflow import keras


from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

text = ""
with open("/content/train_new.txt") as source:
  for line in source.readlines():
    text = text + line

print("text: " + text)

sentences = []
labels = []

for item in text.split("<n>"):
  parts = item.split("<t>")
  print(parts)
  sentences.append(parts[0])
  labels.append(parts[1])

print(sentences)
print(labels)

print("----")

train_test_split_percentage = 80

training_size = round((len(sentences)/100)*train_test_split_percentage)

print("training size: " + str(training_size) + " of " + str(len(labels)))

training_sentences = sentences[0:training_size]
testing_sentences = sentences[training_size:]

training_labels = labels[0:training_size]
testing_labels = labels[training_size:]

vocab_size = 100
max_length = 10

tokenizer = Tokenizer(num_words = vocab_size, oov_token="<OOV>")
tokenizer.fit_on_texts(sentences)

word_index = tokenizer.word_index

training_sequences = tokenizer.texts_to_sequences(training_sentences)
training_padded = pad_sequences(training_sequences, maxlen=max_length, padding="post", truncating="post")

testing_sequences = tokenizer.texts_to_sequences(testing_sentences)
testing_padded = pad_sequences(testing_sequences, maxlen=max_length, padding="post", truncating="post")

# convert training & testing data into numpy array
# Need this block to get it to work with TensorFlow 2.x
import numpy as np
training_padded = np.array(training_padded)
training_labels = np.asarray(training_labels).astype('float32').reshape((-1,1))
testing_padded = np.array(testing_padded)
testing_labels = np.asarray(testing_labels).astype('float32').reshape((-1,1))

# defining the model
model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, 24, input_length=max_length),
    tf.keras.layers.GlobalAveragePooling1D(),
    tf.keras.layers.Dense(24, activation='relu'),
    tf.keras.layers.Dense(1, activation='softmax')
])
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])

# training the model
num_epochs = 1000
history = model.fit(training_padded, training_labels, epochs=num_epochs, validation_data=(testing_padded, testing_labels), verbose=2)

However, while training, it prints weird accuracy and loss values like this:然而,在训练时,它会打印出奇怪的准确率和损失值,如下所示:

Epoch 972/1000
9/9 - 0s - loss: -8.2316e+03 - accuracy: 0.7345 - val_loss: -2.7299e+04 - val_accuracy: 0.0000e+00
Epoch 973/1000
9/9 - 0s - loss: -8.2452e+03 - accuracy: 0.7345 - val_loss: -2.7351e+04 - val_accuracy: 0.0000e+00
Epoch 974/1000
9/9 - 0s - loss: -8.2571e+03 - accuracy: 0.7345 - val_loss: -2.7363e+04 - val_accuracy: 0.0000e+00
Epoch 975/1000
9/9 - 0s - loss: -8.2703e+03 - accuracy: 0.7345 - val_loss: -2.7416e+04 - val_accuracy: 0.0000e+00

The train_new.txt file contains data in the form of text<t>class_num<n> train_new.txt 文件包含text<t>class_num<n>形式的数据

When trying to predict using the model.predict() function, it always outputs [[1.]]当尝试使用model.predict() function 进行预测时,它总是输出[[1.]]

What's the issue with my code?我的代码有什么问题?

tf.keras.layers.Dense(1, activation='sigmoid')

You should use sigmoid as activation if you are doing a binary classification.如果您正在执行二进制分类,则应该使用sigmoid作为激活。 However also,不过也,

tf.keras.layers.Dense(2, activation='softmax') 

will be correct in the terms of probability.在概率方面是正确的。

Softmax outputs' sum will always be equal to one. Softmax 输出的总和将始终等于 1。 That's why you get 1 as output everytime.这就是为什么你每次都得到 1 作为 output 的原因。

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

相关问题 Keras张量流怪异损失精度 - Keras tensorflow weird loss accuracy Tensorflow CNN模型不训练吗? 持续的损失和准确性 - Tensorflow CNN model not training? Constant loss and accuracy tensorflow.keras model 准确度、损失和验证指标在训练期间保持 ZA81259CEF8E959C2297DF1D456EZ5 - tensorflow.keras model accuracy, loss, and validation metrics remain static during 30 epochs of training 为什么我的 Tensorflow CNN 的准确度为零而损失不是? - why my Tensorflow CNN's accuracy is zero while loss is not? 训练Keras顺序模型时损失不会减少 - Loss is not decreasing while training Keras Sequential Model Keras 序列模型未训练(卡在相同的准确度和损失上) - Keras sequential model not training (Stuck on the same Accuracy and Loss) 如何在训练 Keras 功能 API model 时打印不同激活层的准确性? (张量流Python) - How to print accuracy of different activation layers while training a Keras functional API model? (Tensorflow Python) 为什么我在 keras 中的准确度和损失为 0.000 和 nan? - Why is my accuracy and loss, 0.000 and nan, in keras? 为什么张量流代码中的训练模型的准确性没有改变? - Why the accuracy of the training model is not changed in the tensorflow code? 那些喀拉拉邦损失和准确性怪异吗? - Are those keras loss and accuracy weird?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM