Python 来自csv的Keras文本分类自定义数据集

Python 来自csv的Keras文本分类自定义数据集,python,csv,numpy,keras,text-classification,Python,Csv,Numpy,Keras,Text Classification,我试图建立一个Keras模型,将不同的文章分类为不同的主题。每篇文章只有一个主题。我有一个自定义csv文件,其结构如下: "topic1","article1" "topic2","article2" 我试图为这个数据集训练我的模型,但不幸的是,我得到了一个错误,因为来自csv的数据还没有被处理成向量 这是我的代码: from __future__ import print_function import csv import numpy as np import keras import o

我试图建立一个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)
如何正确加载数据以训练模型?我如何使用
模型预测给定文本的主题。预测

编辑: 通过改变加载训练数据的过程,我对模型进行了训练,如下所示:

 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)

您无法直接将文章转换为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],每个元素表示对应主题的概率。概率最高的主题是预测