Update README.md
Browse files
README.md
CHANGED
@@ -58,19 +58,27 @@ Use `datasets.shuffle()` to properly shuffle the dataset. Use `datasets.map()` t
|
|
58 |
```py
|
59 |
# Shuffle and apply your own preprocessing.
|
60 |
dataset = dataset.shuffle(seed=42)
|
61 |
-
dataset = dataset.map(preprocess, fn_kwargs={'tokenizer': tokenizer
|
|
|
62 |
```
|
63 |
|
64 |
-
|
65 |
```py
|
|
|
66 |
class Tokenizer:
|
|
|
|
|
67 |
def __call__(self, example):
|
68 |
return example
|
69 |
|
70 |
-
|
|
|
|
|
|
|
|
|
71 |
# Get number of moves made in the game.
|
72 |
max_ply = len(example['moves'])
|
73 |
-
pick_random_move = random.randint(0, max_ply)
|
74 |
|
75 |
# Get the FEN, move and score for our random choice.
|
76 |
fen = example['fens'][pick_random_move]
|
@@ -78,16 +86,19 @@ def preprocess(example, useful_fn):
|
|
78 |
score = example['scores'][pick_random_move]
|
79 |
|
80 |
# Transform data into the format of your choice.
|
81 |
-
example['fens'] =
|
82 |
-
example['moves'] =
|
83 |
-
example['scores'] =
|
84 |
return example
|
|
|
|
|
85 |
```
|
86 |
<br>
|
87 |
<br>
|
88 |
<br>
|
89 |
|
90 |
# Complete Example
|
|
|
91 |
|
92 |
```py
|
93 |
import random
|
@@ -107,19 +118,19 @@ def score_fn(score):
|
|
107 |
|
108 |
def preprocess(example, tokenizer, score_fn):
|
109 |
# Get number of moves made in the game.
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
|
124 |
|
125 |
tokenizer = Tokenizer()
|
|
|
58 |
```py
|
59 |
# Shuffle and apply your own preprocessing.
|
60 |
dataset = dataset.shuffle(seed=42)
|
61 |
+
dataset = dataset.map(preprocess, fn_kwargs={'tokenizer': tokenizer,
|
62 |
+
'score_fn': score_fn})
|
63 |
```
|
64 |
|
65 |
+
In this example, we're passing two additional arguments to the preprocess function in dataset.map(). You can use the following mock examples for inspiration:
|
66 |
```py
|
67 |
+
# A mock tokenizer and functions for demonstration.
|
68 |
class Tokenizer:
|
69 |
+
def __init__(self):
|
70 |
+
pass
|
71 |
def __call__(self, example):
|
72 |
return example
|
73 |
|
74 |
+
# Transform Stockfish score and terminal outcomes.
|
75 |
+
def score_fn(score):
|
76 |
+
return score
|
77 |
+
|
78 |
+
def preprocess(example, tokenizer, score_fn):
|
79 |
# Get number of moves made in the game.
|
80 |
max_ply = len(example['moves'])
|
81 |
+
pick_random_move = random.randint(0, max_ply-1)
|
82 |
|
83 |
# Get the FEN, move and score for our random choice.
|
84 |
fen = example['fens'][pick_random_move]
|
|
|
86 |
score = example['scores'][pick_random_move]
|
87 |
|
88 |
# Transform data into the format of your choice.
|
89 |
+
example['fens'] = tokenizer(fen)
|
90 |
+
example['moves'] = tokenizer(move)
|
91 |
+
example['scores'] = score_fn(score)
|
92 |
return example
|
93 |
+
|
94 |
+
tokenizer = Tokenizer()
|
95 |
```
|
96 |
<br>
|
97 |
<br>
|
98 |
<br>
|
99 |
|
100 |
# Complete Example
|
101 |
+
You can try pasting this into Colab and it should work fine. Have fun!
|
102 |
|
103 |
```py
|
104 |
import random
|
|
|
118 |
|
119 |
def preprocess(example, tokenizer, score_fn):
|
120 |
# Get number of moves made in the game.
|
121 |
+
max_ply = len(example['moves'])
|
122 |
+
pick_random_move = random.randint(0, max_ply-1)
|
123 |
+
|
124 |
+
# Get the FEN, move and score for our random choice.
|
125 |
+
fen = example['fens'][pick_random_move]
|
126 |
+
move = example['moves'][pick_random_move]
|
127 |
+
score = example['scores'][pick_random_move]
|
128 |
+
|
129 |
+
# Transform data into the format of your choice.
|
130 |
+
example['fens'] = tokenizer(fen)
|
131 |
+
example['moves'] = tokenizer(move)
|
132 |
+
example['scores'] = score_fn(score)
|
133 |
+
return example
|
134 |
|
135 |
|
136 |
tokenizer = Tokenizer()
|