我正在尝试构建一个Keras模型,将不同的文章分类为主题。每篇文章只有一个主题。我有一个具有以下结构的自定义csv文件:
"topic1","article1"
"topic2","article2"
我正在尝试为此数据集训练我的模型,但遗憾的是我收到错误,因为来自csv的数据尚未处理为矢量。
这是我的代码:
from __future__ import print_function
import csv
import numpy as np
import keras
import os
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.preprocessing.text import Tokenizer
max_words = 1000
batch_size = 32
epochs = 5
model_file_name = 'model.h5'
def load_data(word_max, test_split):
xs = []
labels = []
counter = 0
with open('data.csv', 'r') as f:
reader = csv.reader(f)
for line in reader:
if counter > word_max:
break
xs.append(line[1])
labels.append(line[0])
counter += 1
idx = int(len(xs) * (1 - test_split))
train_x, train_y = np.array(xs[:idx]), np.array(labels[:idx])
test_x, test_y = np.array(xs[idx:]), np.array(labels[idx:])
return (train_x, train_y), (test_x, test_y)
print('Loading data...')
(x_train, y_train), (x_test, y_test) = load_data(max_words, 0.3)
print(len(x_train), 'train sequences')
print(len(x_test), 'test sequences')
num_classes = np.max(y_train) + 1
print(num_classes, 'classes')
print('Vectorizing sequence data...')
tokenizer = Tokenizer(num_words=max_words)
x_train = tokenizer.sequences_to_matrix(x_train, mode='binary')
x_test = tokenizer.sequences_to_matrix(x_test, mode='binary')
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)
print('Convert class vector to binary class matrix '
'(for use with categorical_crossentropy)')
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
print('y_train shape:', y_train.shape)
print('y_test shape:', y_test.shape)
if os.path.isfile(model_file_name):
model = keras.models.load_model(model_file_name)
else:
print('Building model...')
model = Sequential()
model.add(Dense(512, input_shape=(max_words,)))
model.add(Activation('relu'))
model.add(Dropout(0.35))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_split=0.1)
score = model.evaluate(x_test, y_test,
batch_size=batch_size, verbose=1)
print('Test score:', score[0])
print('Test accuracy:', score[1])
model.save(model_file_name)
如何正确加载数据以训练我的模型?我如何使用model.predict
预测给定文本的主题?
编辑: 我通过改变加载训练数据的过程来获得模型的训练,如:
print('Loading data...')
(x_train, y_train), (x_test, y_test) = load_data(max_words, 0.3)
tokenizer = Tokenizer(num_words=max_words)
tokenizer.fit_on_texts(x_train)
x_train = tokenizer.texts_to_sequences(x_train)
tokenizer.fit_on_texts(y_train)
y_train = tokenizer.texts_to_sequences(y_train)
tokenizer.fit_on_texts(x_test)
x_test = tokenizer.texts_to_sequences(x_test)
tokenizer.fit_on_texts(y_test)
y_test = tokenizer.texts_to_sequences(y_test)
print(len(x_train), 'train sequences')
print(len(x_test), 'test sequences')
那么,我如何使用它并预测给定文章中的标签?:
model = keras.models.load_model(model_file_name)
to_predict = np.array(['The sun is shining.'])
# Predict label from example article
label = model.predict(...)
print(label)
答案 0 :(得分:1)
您无法直接将文章转换为numpy数组,您需要使用相同的标记生成器将文章转换为numpy数组。
array = tokenizer.texts_to_sequences([title]) # the tokenizer must be same with the training tokenizer
array = np.asanyarray(array)
array = sequence.pad_sequences(array, maxlen=max_words, padding='post', truncating='post')
array = np.asarray(array)
result = model.predict(array)
然后,结果将是概率向量(因为您使用softmax函数),如[0.3,0.7],每个元素代表相应主题的概率。具有最高概率的主题将是预测。