|
| 1 | +### The First Project |
| 2 | + |
| 3 | +This example is the same as `../text_cls` but from scratch. |
| 4 | + |
| 5 | +#### Prepare the BERT model and .intc.json |
| 6 | + |
| 7 | +Download the BERT pretrained model and the `tokenizer.json` to `./pretrain` |
| 8 | + |
| 9 | +add a file `.intc.json` in current dir |
| 10 | +```json |
| 11 | +{ |
| 12 | + "entry": [ |
| 13 | + "config" // the config will put in this dir, the intc-lsp will server on files in this dir |
| 14 | + ], |
| 15 | + "src": [ // the source code for this project |
| 16 | + "dlk", // the dlk package |
| 17 | + "src" // and the src code will write |
| 18 | + ] |
| 19 | +} |
| 20 | + |
| 21 | +``` |
| 22 | + |
| 23 | +#### Prepare the Dataset |
| 24 | + |
| 25 | +load and convert the `sst2` dataset, see `./process.py` |
| 26 | + |
| 27 | +```python |
| 28 | +from datasets import load_dataset |
| 29 | + |
| 30 | +def flat(data): |
| 31 | + """flat the data like zip""" |
| 32 | + sentences = data["sentence"] |
| 33 | + uuids = data["uuid"] |
| 34 | + labelses = data["labels"] |
| 35 | + return [ |
| 36 | + {"sentence": sentece, "labels": [labels], "uuid": uuid} |
| 37 | + for sentece, labels, uuid in zip(sentences, labelses, uuids) |
| 38 | + ] |
| 39 | + |
| 40 | + |
| 41 | +label_map = {0: "neg", 1: "pos"} |
| 42 | + |
| 43 | +data = load_dataset("sst2") # load dataset from huggingface dataset or what you want |
| 44 | +data = data.map( |
| 45 | + lambda one: { |
| 46 | + "sentence": one["sentence"], |
| 47 | + "labels": label_map[one["label"]], |
| 48 | + "uuid": str(uuid.uuid1()), |
| 49 | + }, |
| 50 | + remove_columns=["sentence", "label"], |
| 51 | +) |
| 52 | +input = { |
| 53 | + "train": pd.DataFrame(flat(data["train"].to_dict())), # convert the train data and valid part to pd.DataFrame |
| 54 | + "valid": pd.DataFrame(flat(data["validation"].to_dict())), |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +#### Prepare the Process code |
| 59 | + |
| 60 | +dlk provide a general `@processor@default` processor to preprocess the data, but for this example we create a simple one, please check the code on `./src/my_process.py` and registed as `@processor@my` |
| 61 | + |
| 62 | +#### Prepare the config of preprocessor |
| 63 | +config `@processor@my` see `./config/processor.jsonc`, |
| 64 | + |
| 65 | +#### Run the preprocessor |
| 66 | + |
| 67 | +then run preprocess |
| 68 | + |
| 69 | +```bash |
| 70 | +python process.py |
| 71 | +``` |
| 72 | + |
| 73 | +#### Prepare the model code |
| 74 | + |
| 75 | +dlk provide many modules, but we want just test ours. |
| 76 | + |
| 77 | +we define a simple classification model at `./src/my_model.py` and registed as `@model@my_model` |
| 78 | + |
| 79 | +#### Prepare the DataModule |
| 80 | +dlk also provide a general datamodule, for this case, we implement ours. |
| 81 | + |
| 82 | +we define a simple datamodule at `./src/my_datamodule.py` and registed as `@datamodule@my_datamodule` |
| 83 | + |
| 84 | +#### Prepare the `fit.json` |
| 85 | + |
| 86 | +besides `datamodule` `model`, there are many other module like `loss`, `optimizer`, `schedule` which is easy to understand, we reuse them buitin dlk. |
| 87 | + |
| 88 | +see `./config/fit.jsonc` |
| 89 | + |
| 90 | + |
| 91 | +#### Train your model |
| 92 | + |
| 93 | +just prepare the train.py and |
| 94 | +```bash |
| 95 | +python train.py |
| 96 | + |
| 97 | +``` |
0 commit comments