PHP前端开发

创建 LLM 以在 Python 中使用张量流进行测试

百变鹏仔 4天前 #Python
文章标签 流进

嗨,

我想测试一个小型的llm程序,我决定用tensorflow来做。

我的源代码可以在 https://github.com/victordalet/first_llm


一、要求

您需要安装tensorflow和numpy

立即学习“Python免费学习笔记(深入)”;

pip install 'numpy<2'pip install tensorflow

ii - 创建数据集

您需要创建一个数据字符串数组来计算一个小数据集,例如我创建:

data = [    "salut comment ca va",    "je suis en train de coder",    "le machine learning est une branche de l'intelligence artificielle",    "le deep learning est une branche du machine learning",]

如果你没有灵感,可以在kaggle上找到一个数据集。


iii - 构建模型并训练它

为此,我使用各种方法创建了一个小型 llm 类。

class llm:    def __init__(self):        self.model = none        self.max_sequence_length = none        self.input_sequences = none        self.total_words = none        self.tokenizer = none        self.tokenize()        self.create_input_sequences()        self.create_model()        self.train()        test_sentence = "pour moi le machine learning est"        print(self.test(test_sentence, 10))    def tokenize(self):        self.tokenizer = tokenizer()        self.tokenizer.fit_on_texts(data)        self.total_words = len(self.tokenizer.word_index) + 1    def create_input_sequences(self):        self.input_sequences = []        for line in data:            token_list = self.tokenizer.texts_to_sequences([line])[0]            for i in range(1, len(token_list)):                n_gram_sequence = token_list[:i + 1]                self.input_sequences.append(n_gram_sequence)        self.max_sequence_length = max([len(x) for x in self.input_sequences])        self.input_sequences = pad_sequences(self.input_sequences, maxlen=self.max_sequence_length, padding='pre')    def create_model(self):        self.model = sequential()        self.model.add(embedding(self.total_words, 100, input_length=self.max_sequence_length - 1))        self.model.add(lstm(150, return_sequences=true))        self.model.add(dropout(0.2))        self.model.add(lstm(100))        self.model.add(dense(self.total_words, activation='softmax'))    def train(self):        self.model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])        x, y = self.input_sequences[:, :-1], self.input_sequences[:, -1]        y = tf.keras.utils.to_categorical(y, num_classes=self.total_words)        self.model.fit(x, y, epochs=200, verbose=1)

iv - 测试

最后,我使用类的构造函数中调用的测试方法来测试模型。

警告:如果生成的单词与前一个单词相同,我会在此测试函数中阻止生成。

    def test(self, sentence: str, nb_word_to_generate: int):        last_word = ""        for _ in range(nb_word_to_generate):            token_list = self.tokenizer.texts_to_sequences([sentence])[0]            token_list = pad_sequences([token_list], maxlen=self.max_sequence_length - 1, padding='pre')            predicted = np.argmax(self.model.predict(token_list), axis=-1)            output_word = ""            for word, index in self.tokenizer.word_index.items():                if index == predicted:                    output_word = word                    break            if last_word == output_word:                return sentence            sentence += " " + output_word            last_word = output_word        return sentence