Как сопоставить текст с загруженным в keras модулем h5, чтобы дать в качестве входных данных и классифицировать класс (Здесь я использовал CNN для классификации текста) - PullRequest
0 голосов
/ 09 октября 2018

В приведенном ниже коде я пытаюсь загрузить файл h5 и классифицировать текст в категорию

from keras.models import load_model
import numpy as np

model = load_model('my_model.h5')
model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['acc'])
texts = u'How is the weather today?'
classes = model.predict(texts)
print classes

Но проблема в том, как сопоставить эти слова с пакетом слов и получить позициючисла, которые являются входными данными для предиката

. Для генерации файла h5 я написал код, как показано ниже, текст маркируется, и меткам присваиваются 0 или 1

texts = []
labels = []

tokenizer = Tokenizer(num_words=MAX_NB_WORDS)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)

word_index = tokenizer.word_index
print('Number of Unique Tokens',len(word_index))

Генерируются токенизированные словасверху примерно так.

[[219, 1, 4, 3, 1821, 41, 288, 36], [33, 18, 1, 4, 14, 12, 563],[9, 28, 6, 214, 3, 1822, 388], [8, 11, 74, 30, 23], [13, 8, 1, 14, 12, 1823, 12, 187, 166, 7, 249, 1, 564, 565], [22, 29, 16, 566, 3, 861, 31, 862, 31, 67, 17, 122], [13, 8, 1, 14, 3, 348, 131, 31, 484, 148, 20, 63, 35, 153], [22, 86, 17, 567, 20, 17, 389, 3, 425], [13, 8, 1, 4, 14, 12, 56, 49, 3, 220, 1824, 680], [13, 9, 1, 4, 6, 63, 426, 3, 1825, 390], [13, 8, 1, 4, 221, 132, 5, 1158, 863, 41, 100, 154], [4, 3, 391, 41, 138, 1826, 1827], [13, 9, 1, 4, 6, 3, 1828, 143], [9, 11, 6, 101, 3, 1, 35, 123], [13, 8, 1, 4, 12, 260, 139, 3, 485, 179, 8, 144, 233, 215], [33, 18, 92, 11, 9, 11, 6, 140, 3, 1159, 1160, 100, 273], [9, 11, 6, 140, 30, 7, 261, 864], [13,8, 1, 4, 14, 12, 1829], [9, 11, 6, 75, 7, 274, 200, 349, 3, 1830, 350], [13, 9, 1, 4, 6, 3,1831, 1832, 201, 7, 1833, 1834, 145], [8, 1, ......

data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH)

labels = to_categorical(np.asarray(labels))
print('Shape of Data Tensor:', data.shape)
print('Shape of Label Tensor:', labels.shape)

indices = np.arange(data.shape[0])
np.random.shuffle(indices)
data = data[indices]
labels = labels[indices]
nb_validation_samples = int(VALIDATION_SPLIT * data.shape[0])

x_train = data[:-nb_validation_samples]
y_train = labels[:-nb_validation_samples]
x_val = data[-nb_validation_samples:]
y_val = labels[-nb_validation_samples:]



embeddings_index = {}
# f = open('glove.6B.100d.txt',encoding='utf8')
import io
f = io.open('glove.6B.100d.txt', encoding='utf-8')
for line in f:
    values = line.split()
    word = values[0]
    coefs = np.asarray(values[1:], dtype='float32')
    embeddings_index[word] = coefs
f.close()

print('Total %s word vectors in Glove 6B 100d.' % len(embeddings_index))



embedding_matrix = np.random.random((len(word_index) + 1, EMBEDDING_DIM))
for word, i in word_index.items():
    embedding_vector = embeddings_index.get(word)
    if embedding_vector is not None:
        # words not found in embedding index will be all-zeros.
        embedding_matrix[i] = embedding_vector

embedding_layer = Embedding(len(word_index) + 1,
                            EMBEDDING_DIM,weights=[embedding_matrix],
                            input_length=MAX_SEQUENCE_LENGTH,trainable=True)



sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)
l_cov1= Conv1D(128, 5, activation='relu')(embedded_sequences)
l_pool1 = MaxPooling1D(5)(l_cov1)
l_cov2 = Conv1D(128, 5, activation='relu')(l_pool1)
l_pool2 = MaxPooling1D(5)(l_cov2)
l_cov3 = Conv1D(128, 5, activation='relu')(l_pool2)
l_pool3 = MaxPooling1D(35)(l_cov3)  # global max pooling
l_flat = Flatten()(l_pool3)
l_dense = Dense(128, activation='relu')(l_flat)
preds = Dense(len(macronum), activation='softmax')(l_dense)

model = Model(sequence_input, preds)
model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['acc'])

print("Simplified convolutional neural network")
model.summary()
cp=ModelCheckpoint('model_cnn.hdf5',monitor='val_acc',verbose=1,save_best_only=True)



history=model.fit(x_train, y_train, validation_data=(x_val, y_val),epochs=15, batch_size=2,callbacks=[cp])
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...