我已经使用python在Tensorflow中训练了一个自定义文本分类器,用于使用以下代码将句子分类为包含信息的问题/句子:
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)
但是,在训练时,它会打印出奇怪的准确性和损失值,如下所示:
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
train_new.txt文件包含以下形式的数据: text<t>class_num<n>
尝试使用model.predict()
功能进行预测时,它总是输出[[1.]]
我的代码有什么问题?
tf.keras.layers.Dense(1, activation='sigmoid')
如果要进行二进制分类,则应使用sigmoid作为激活。但是,
tf.keras.layers.Dense(2, activation='softmax')
就概率而言将是正确的。
Softmax输出的总和将始终等于1。这就是为什么每次输出1的原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句