import numpy as np
import tensorflow as tf
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import LSTM, Dense, Embedding
from tensorflow.keras.preprocessing.sequence import pad_sequences
from sklearn.model_selection import train_test_split
import jieba
from gensim.models import KeyedVectors
加载数据集
with open("path_to_your_big_text_file.txt", "r", encoding="utf-8") as file:
text_data = file.read()
使用 jieba 进行分词
text_data = jieba.lcut(text_data)
创建词汇字典(word_index)
word_index = {}
for word in text_data:
if word not in word_index:
word_index[word] = len(word_index) + 1 # 词汇索引从1开始编号
将文本转换为索引序列
sequences = []
for word in text_data:
sequences.append(word_index[word])
encoded_text = np.array(sequences)
生成训练序列
seq_length = 100 # 一个例子的序列长度
sequences = []
for i in range(seq_length, len(encoded_text)):
seq = encoded_text[i-seq_length:i+1]
sequences.append(seq)
sequences = np.array(sequences)
X, y = sequences[:, :-1], sequences[:, -1]
y = to_categorical(y, num_classes=len(word_index) + 1)
划分数据集
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.05)
embedding_matrix = np.zeros((len(word_index) + 1, word2vec_model.vector_size))
for word, i in word_index.items():
if word in word2vec_model:
embedding_vector = word2vec_model[word]
embedding_matrix[i] = embedding_vector
2023-12-14 17:12:38.772668: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0.
WARNING:tensorflow:From C:\Users\Tony-H\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\losses.py:2976:
The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\Tony-H\AppData\Local\Temp\jieba.cache
Loading model cost 0.258 seconds.
Prefix dict has been built successfully.
Traceback (most recent call last):
File "d:\Tony-H\LanguageMachine\LM2-create_model.py", line 45, in
word2vec_model = KeyedVectors.load_word2vec_format(r'D:\Tony-H\LanguageMachine\Pre-trainedModel\zh.bin', binary=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Tony-H\AppData\Local\Programs\Python\Python311\Lib\site-packages\gensim\models\keyedvectors.py", line 1719, in load_word2vec_format
return _load_word2vec_format(
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Tony-H\AppData\Local\Programs\Python\Python311\Lib\site-packages\gensim\models\keyedvectors.py", line 2058, in _load_word2vec_format
header = utils.to_unicode(fin.readline(), encoding=encoding)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Tony-H\AppData\Local\Programs\Python\Python311\Lib\site-packages\gensim\utils.py", line 365, in any2unicode return str(text, encoding, errors=errors)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
这是错误信息
Assistant
错误信息 UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte 指出在尝试使用UTF-8编码方式解码一个二进制文件时出现了问题。这通常发生在尝试错误地以文本方式读取一个二进制文件。