""" Prepare the Shakespeare dataset for character-level language modeling. So instead of…
- User
""" Prepare the Shakespeare dataset for character-level language modeling. So instead of encoding with GPT-2 BPE tokens, we just map characters to ints. Will save train.bin, val.bin containing the ids, and meta.pkl containing the encoder and decoder and some other related info. """ import os import pickle import requests import numpy as np
download the tiny shakespeare dataset
input_file_path = os.path.join(os.path.dirname(file), 'input.txt') if not os.path.exists(input_file_path): data_url = 'https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt' with open(input_file_path, 'w') as f: f.write(requests.get(data_url).text)
with open(input_file_path, 'r') as f: data = f.read() print(f"length of dataset in characters: {len(data):,}")
get all the unique characters that occur in this text
chars = sorted(list(set(data))) vocab_size = len(chars) print("all the unique characters:", ''.join(chars)) print(f"vocab size: {vocab_size:,}")
create a mapping from characters to integers
stoi = { ch:i for i,ch in enumerate(chars) } itos = { i:ch for i,ch in enumerate(chars) } def encode(s): return [stoi[c] for c in s] # encoder: take a string, output a list of integers def decode(l): return ''.join([itos[i] for i in l]) # decoder: take a list of integers, output a string
create the train and test splits
n = len(data) train_data = data[:int(n0.9)] val_data = data[int(n0.9):]
encode both to integers
train_ids = encode(train_data) val_ids = encode(val_data) print(f"train has {len(train_ids):,} tokens") print(f"val has {len(val_ids):,} tokens")
export to bin files
train_ids = np.array(train_ids, dtype=np.uint16) val_ids = np.array(val_ids, dtype=np.uint16) train_ids.tofile(os.path.join(os.path.dirname(file), 'train.bin')) val_ids.tofile(os.path.join(os.path.dirname(file), 'val.bin'))
save the meta information as well, to help us encode/decode later
meta = { 'vocab_size': vocab_size, 'itos': itos, 'stoi': stoi, } with open(os.path.join(os.path.dirname(file), 'meta.pkl'), 'wb') as f: pickle.dump(meta, f)
length of dataset in characters: 1115394
all the unique characters:
!$&',-.3:;?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
vocab size: 65
train has 1003854 tokens
val has 111540 tokens 请问我可以用这段程序来进行中文网络文学文字的处理么
- Assistant
是的,您可以使用这段程序来处理中文的字符级语言建模。唯一的区别是,您需要使用中文文本代替莎士比亚的数据集。
- User
这段程序可以用于中文网络小说txt格式么,请你以gpt大师的身份为我解答
- Assistant
作为语言模型,这段程序可以用于处理中文网络小说的txt格式。它会将文本中的字符映射为整数,并创建训练和验证数据集。然后,您可以使用这些数据集来训练和评估基于字符级的语言模型。请注意,这段程序仅适用于字符级建模,不考虑中文词语的语义和上下文。
- User
可是我需要中文词语的语义和上下文,请你以gpt大师的身份为我修改程序
- Assistant
作为GPT大师,我来为您修改这段程序,以便处理中文文本的单词级语言建模。请注意,这需要使用适当的中文分词工具来将文本分割成单词的序列。在这里,我将使用jieba作为中文分词工具。
- User
请你以gpt大宗师的身份,将这段程序修改为可以自动生成有逻辑的,有上下文语义的中文网文形式
- Assistant
作为GPT大宗师,我来为您修改这段程序,以便生成有逻辑和上下文语义的中文网文。
以下是修改后的程序:
- User
非常感谢你
- Assistant
不客气,我很高兴能够帮助您!如果您有任何其他问题,请随时提问。祝您使用愉快!
- User
好的
- Assistant
谢谢!如果您有任何其他问题,请随时提问。我随时准备帮助您。祝您一切顺利!