from keras.preprocessing.text import Tokenizer from keras.preprocessing.sequence import pad_sequences from keras.utils import to_categorical import numpy as np MAX_SEQUENCE_LEN = 1000 # 文档限制长度 MAX_WORDS_NUM = 20000 # 词典的个数 VAL_SPLIT_RATIO = 0.2 # 验证集的比例 tokenizer = Tokenizer(num_words=MAX_WORDS_NUM) tokenizer.fit_on_texts(texts) sequences = tokenizer.texts_to_sequences(texts) word_index = tokenizer.word_index print(len(word_index)) # all token found # print(word_index.get('新闻')) # get word index dict_swaped = lambda _dict: {val:key for (key, val) in _dict.items()} word_dict = dict_swaped(word_index) # swap key-value data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LEN) labels_categorical = to_categorical(np.asarray(labels)) print('Shape of data tensor:', data.shape) print('Shape of label tensor:', labels_categorical.shape) indices = np.arange(data.shape[0]) np.random.shuffle(indices) data = data[indices] labels_categorical = labels_categorical[indices] # split data by ratio val_samples_num = int(VAL_SPLIT_RATIO * data.shape[0]) x_train = data[:-val_samples_num] y_train = labels_categorical[:-val_samples_num] x_val = data[-val_samples_num:] y_val = labels_categorical[-val_samples_num:]
时间: 2024-03-18 12:43:47 浏览: 282
这段代码使用了Keras库中的Tokenizer和pad_sequences方法对文本进行预处理,将文本转化为数字序列,并进行了填充,确保所有文本序列的长度相同。同时也使用了to_categorical方法对标签进行独热编码。最后,将数据集按照VAL_SPLIT_RATIO的比例划分为训练集和验证集。
相关问题
NMT sequence to sequence 英语和印地语 代码
以下是一个使用神经机器翻译(NMT)的英语到印地语的 seq2seq 模型代码示例:
```python
from keras.models import Model
from keras.layers import Input, LSTM, Dense, Embedding
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
import numpy as np
# 定义模型输入和输出序列的最大长度
max_encoder_seq_length = 50
max_decoder_seq_length = 50
# 定义输入序列的维度
num_encoder_tokens = ...
num_decoder_tokens = ...
# 定义LSTM层的维度
latent_dim = 256
# 定义编码器模型
encoder_inputs = Input(shape=(None,))
encoder_embedding = Embedding(num_encoder_tokens, latent_dim)
encoder_lstm = LSTM(latent_dim, return_state=True)
encoder_outputs, state_h, state_c = encoder_lstm(encoder_embedding(encoder_inputs))
encoder_states = [state_h, state_c]
# 定义解码器模型
decoder_inputs = Input(shape=(None,))
decoder_embedding = Embedding(num_decoder_tokens, latent_dim)
decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_embedding(decoder_inputs), initial_state=encoder_states)
decoder_dense = Dense(num_decoder_tokens, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
# 定义整个模型
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
# 编译模型
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
# 训练模型
model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
batch_size=batch_size,
epochs=epochs,
validation_split=0.2)
# 预测模型
encoder_model = Model(encoder_inputs, encoder_states)
decoder_state_input_h = Input(shape=(latent_dim,))
decoder_state_input_c = Input(shape=(latent_dim,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm(decoder_embedding(decoder_inputs), initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = Model([decoder_inputs] + decoder_states_inputs, [decoder_outputs] + decoder_states)
# 定义预测函数
def decode_sequence(input_seq):
states_value = encoder_model.predict(input_seq)
target_seq = np.zeros((1, 1))
target_seq[0, 0] = target_token_index['\t']
stop_condition = False
decoded_sentence = ''
while not stop_condition:
output_tokens, h, c = decoder_model.predict([target_seq] + states_value)
sampled_token_index = np.argmax(output_tokens[0, -1, :])
sampled_char = reverse_target_char_index[sampled_token_index]
decoded_sentence += sampled_char
if (sampled_char == '\n' or len(decoded_sentence) > max_decoder_seq_length):
stop_condition = True
target_seq = np.zeros((1, 1))
target_seq[0, 0] = sampled_token_index
states_value = [h, c]
return decoded_sentence
```
需要注意的是,NMT 的 seq2seq 模型相对于简单的 seq2seq 模型要复杂得多,需要更多的调整和优化才能在实际任务中获得好的性能。此外,还需要对数据进行预处理,比如分词、标记化等,这些内容在上述代码中并未包含。
给出一个使用keras神经网络模型的文本分类代码
以下是一个使用 Keras 模型进行文本分类的示例代码:
```python
from keras.models import Sequential
from keras.layers import Dense, Embedding, LSTM, Dropout
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import np_utils
import numpy as np
# 准备数据
texts = ["I like to play football", "He hates playing video games", "She loves dancing"]
labels = [0, 1, 2] # 0表示“体育”,1表示“游戏”,2表示“音乐”
# 将标签转换为 one-hot 编码
labels = np_utils.to_categorical(labels)
# 构建词汇表
tokenizer = Tokenizer(num_words=1000)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)
word_index = tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))
# 将序列填充到相同的长度
max_sequence_length = max([len(seq) for seq in sequences])
data = pad_sequences(sequences, maxlen=max_sequence_length)
# 构建模型
model = Sequential()
model.add(Embedding(input_dim=len(word_index) + 1, output_dim=256, input_length=max_sequence_length))
model.add(LSTM(256, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# 训练模型
model.fit(data, labels, epochs=10, batch_size=32)
# 使用模型进行预测
test_text = ["She likes to play basketball"]
test_sequence = tokenizer.texts_to_sequences(test_text)
test_data = pad_sequences(test_sequence, maxlen=max_sequence_length)
prediction = model.predict(test_data)
print(prediction)
```
在上面的代码中,我们使用了 Embedding 层将每个单词嵌入到一个 256 维的空间中。然后,我们使用 LSTM 层来学习输入序列的特征。最后,我们将 LSTM 的输出连接到一个大小为 3 的全连接层上,使用 softmax 激活函数进行多分类。在训练模型时,我们使用交叉熵损失函数和 Adam 优化器进行优化。在预测时,我们将测试文本转换为序列并进行填充,然后使用模型进行预测。
阅读全文