Views
No views yet
Embedding(vocab_size, 500) maps tokens to 500-dimensional vectors.SimpleRNN(50) processes sequences with 50 recurrent units.Dense(vocab_size, activation='softmax') produces a probability distribution over the vocabulary.loss='categorical_crossentropy', optimizer='adam', and tracked accuracy metric.11)| Metric | Value |
|---|---|
| Train Accuracy | 78.6% |
| Validation Accuracy | not used |
| Loss (final) | 0.44 |
1import random
2
3def generate_random_name(min_length=3, max_length=10, temperature=1.0, seed_text=""):
4 # Use a random seed
5 random.seed()
6
7 if seed_text:
8 # If seed text is provided
9 generated_name = seed_text
10 else:
11 # Randomly select a token from our vocab as our starting token if no seed text is present
12 random_index = random.randint(1, vocab_size-1)
13 random_token = sp.id_to_piece(random_index)
14 generated_name = random_token
15
16 # Generate subsequent subword tokens
17 for _ in range(max_length - 1):
18 # Encode our starting text
19 token_list = sp.encode_as_ids(generated_name)
20 token_list = pad_sequences([token_list], maxlen=max_seq_len-1, padding='pre')
21
22 # Run prediction
23 predicted = model.predict(token_list, verbose=0)[0]
24
25 # Apply temperature to predictions, helps to varied results
26 predicted = np.log(predicted + 1e-8) / temperature
27 predicted = np.exp(predicted) / np.sum(np.exp(predicted))
28
29 # Sample from the distribution
30 next_index = np.random.choice(range(vocab_size), p=predicted)
31 next_index = int(next_index)
32 next_token = sp.id_to_piece(next_index)
33
34 # Add the predicted token to our output
35 generated_name += next_token
36
37 # Decode the generated subword tokens into a string
38 decoded_name = sp.decode_pieces(generated_name.split())
39
40 # Stop if end token is predicted (optional, based on your dataset), or stop if max_length is reached
41 if next_token == '' or len(decoded_name) > max_length:
42 break
43
44 # Replace underscores with spaces
45 decoded_name = decoded_name.replace("▁", " ")
46
47 # Remove stop tokens from the output
48 decoded_name = decoded_name.replace("</s>", "")
49
50 # Capatilize the first letter of each word
51 generated_name = decoded_name.rsplit(' ', 1)[0]
52 generated_name = generated_name[0].upper() + generated_name[1:]
53
54 # Split the name and check the last part, make sure that it is not cut off
55 parts = generated_name.split()
56 if parts and len(parts[-1]) < min_length:
57 generated_name = " ".join(parts[:-1])
58
59 # Strip the output to ensure no extra whitespace
60 return generated_name.strip()