|
16 | 16 | --> |
17 | 17 |
|
18 | 18 | <!doctype html> |
| 19 | + |
19 | 20 | <head> |
20 | | - <link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.cyan-teal.min.css" /> |
| 21 | + <meta charset="UTF-8"> |
| 22 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 23 | + <link rel="stylesheet" href="../shared/tfjs-examples.css" /> |
21 | 24 | </head> |
22 | 25 |
|
23 | 26 | <style> |
24 | | - body { |
25 | | - font-family: "Roboto", "Helvetica", "Arial", sans-serif; |
26 | | - margin-top: 50px; |
27 | | - margin-left: 50px; |
28 | | - width: 80%; |
29 | | - } |
30 | | - .canvases { |
31 | | - display: inline-block; |
32 | | - } |
33 | 27 | .monospace { |
34 | 28 | font-family: monospace; |
35 | 29 | } |
| 30 | + |
36 | 31 | .input-title { |
37 | 32 | display: inline-block; |
38 | | - width: 20em; |
39 | | - } |
40 | | - .vertical-sections { |
41 | | - border: 1px solid #AAA; |
42 | | - padding: 6px; |
43 | | - margin: 6px; |
44 | | - } |
45 | | - .horizontal-sections { |
46 | | - display: inline-block; |
47 | | - padding-right: 20px; |
48 | | - vertical-align: top; |
49 | | - } |
50 | | - #app-status { |
51 | | - color: blue; |
52 | | - font-size: 150%; |
53 | | - padding-bottom: 1em; |
| 33 | + width: 12em; |
54 | 34 | } |
| 35 | + |
55 | 36 | select { |
56 | 37 | font-size: 14pt; |
57 | 38 | } |
| 39 | + |
58 | 40 | button { |
59 | | - font-size: 14pt; |
60 | 41 | margin: 3px; |
61 | 42 | } |
| 43 | + |
62 | 44 | input:disabled { |
63 | 45 | background: #eeeeee; |
64 | 46 | } |
| 47 | + |
65 | 48 | textarea { |
66 | | - width:98%; |
| 49 | + width: 98%; |
67 | 50 | } |
68 | 51 | </style> |
69 | 52 |
|
70 | | -<body> |
71 | | - <h1>TensorFlow.js Example:<br/>Train LSTM to Generate Text</h1> |
72 | | - <div> |
73 | | - <div id="app-status" class="vertical-sections"> |
74 | | - <span>Please select a text data source or enter your custom text in the text box below and click "load text data" first</span> |
| 53 | +<body class='tfjs-example-container'> |
| 54 | + |
| 55 | + <section class='title-area'> |
| 56 | + <h1>TensorFlow.js Text Generation:</h1> |
| 57 | + <p class='subtitle'>Train a LSTM (Long Short Term Memory) model to generate text</p> |
| 58 | + </section> |
| 59 | + |
| 60 | + <section> |
| 61 | + <p class='section-head'>Description</p> |
| 62 | + <p> |
| 63 | + This example allows you to train a model to generate text in the style of some existing source text. |
| 64 | + The model is designed to predict the <em>next character</em> in a text given some preceding string of characters. |
| 65 | + Doing this repetedly builds up a text, charachter by character. |
| 66 | + </p> |
| 67 | + </section> |
| 68 | + |
| 69 | + <section> |
| 70 | + <p class='section-head'>Status</p> |
| 71 | + <div id="app-status"> |
| 72 | + Please select a text data source or enter your custom text in the text box below and click <em>"Load source data"</em>. |
| 73 | + </div> |
| 74 | + </section> |
| 75 | + |
| 76 | + <section> |
| 77 | + <p class='section-head'>Source Data</p> |
| 78 | + <select id="text-data-select"></select> |
| 79 | + <button id="load-text-data">Load source data</button> |
| 80 | + <div> |
| 81 | + <textarea class="monospace" id="test-text" value="" rows="10"></textarea> |
75 | 82 | </div> |
| 83 | + </section> |
| 84 | + |
| 85 | + <section> |
| 86 | + <p class='section-head'>Model Loading/Creation</p> |
| 87 | + <div> |
| 88 | + <p> |
| 89 | + <em> |
| 90 | + Model saved in IndexedDB: |
| 91 | + <span id="model-available">Load text data first.</span> |
| 92 | + </em> |
| 93 | + </p> |
76 | 94 |
|
77 | | - <div class="vertical-sections"> |
78 | | - <select id="text-data-select"></select> |
79 | | - <button id="load-text-data">Load text data</button> |
80 | 95 | <div> |
81 | | - <textarea class="monospace" id="test-text" value="" rows="10"></textarea> |
| 96 | + <span class="input-title">LSTM layer size(s) (e.g., 128 or 100,50):</span> |
| 97 | + <input id="lstm-layer-sizes" value="128"></input> |
| 98 | + |
| 99 | + <button id="create-or-load-model" disabled="true">Create or load model</button> |
| 100 | + <button id="delete-model" disabled="true">Delete existing model</button> |
82 | 101 | </div> |
83 | 102 | </div> |
| 103 | + </section> |
84 | 104 |
|
85 | | - <div class="vertical-sections"> |
86 | | - <div class="monospace"> |
87 | | - <span class="input-title">Model saved in IndexedDB:</span> |
88 | | - <input class="monospace" id="model-available" readonly="true" size="60" value="(Load text data first.)"></input> |
89 | | - </div> |
90 | | - <div class="horizontal-sections"> |
91 | | - <div class="monospace"> |
92 | | - <span class="input-title">LSTM layer size(s)<br/>(e.g., 128 or 100,50):</span> |
93 | | - <input class="monospace" id="lstm-layer-sizes" value="128"></input> |
94 | | - </div> |
| 105 | + <section> |
| 106 | + <p class='section-head'>Model Training</p> |
| 107 | + <p> |
| 108 | + It can take a while to generate an effective model. Try increasing the number of <em>epochs</em> to improve the |
| 109 | + results, we have found that about <strong>50-100 epochs</strong> are needed to start generating reasonable |
| 110 | + text. |
| 111 | + </p> |
| 112 | + <div class="with-cols"> |
| 113 | + <div> |
95 | 114 | <div> |
96 | | - <button id="create-or-load-model" disabled="true">Create or load model</button> |
97 | | - <button id="delete-model" disabled="true">Delete existing model</button> |
| 115 | + <span class="input-title">Number of Epochs:</span> |
| 116 | + <input id="epochs" value="5"></input> |
98 | 117 | </div> |
99 | | - </div> |
100 | | - |
101 | | - <div class="horizontal-sections"> |
102 | | - <div class="monospace"> |
| 118 | + <div> |
103 | 119 | <span class="input-title">Examples per epoch:</span> |
104 | | - <input class="monospace" id="examples-per-epoch" value="2048"></input> |
| 120 | + <input id="examples-per-epoch" value="2048"></input> |
105 | 121 | </div> |
106 | | - <div class="monospace"> |
| 122 | + <div> |
107 | 123 | <span class="input-title">Batch size:</span> |
108 | | - <input class="monospace" id="batch-size" value="128"></input> |
109 | | - </div> |
110 | | - <div class="monospace"> |
111 | | - <span class="input-title">Epochs:</span> |
112 | | - <input class="monospace" id="epochs" value="5"></input> |
| 124 | + <input id="batch-size" value="128"></input> |
113 | 125 | </div> |
114 | | - <div class="monospace"> |
| 126 | + <div> |
115 | 127 | <span class="input-title">Validation spilt:</span> |
116 | | - <input class="monospace" id="validation-split" value="0.0625"></input> |
| 128 | + <input id="validation-split" value="0.0625"></input> |
117 | 129 | </div> |
118 | | - <div class="monospace"> |
| 130 | + <div> |
119 | 131 | <span class="input-title">Learning rate:</span> |
120 | | - <input class="monospace" id="learning-rate" value="1e-2"></input> |
| 132 | + <input id="learning-rate" value="1e-2"></input> |
121 | 133 | </div> |
122 | 134 | <div> |
123 | 135 | <button id="train-model" disabled="true">Train model</button> |
124 | 136 | </div> |
125 | | - <div class="horizontal-sections"> |
126 | | - <div class="canvases" id="loss-canvas"></div> |
127 | | - </div> |
128 | 137 | </div> |
| 138 | + <div class="canvases" id="batch-loss-canvas"></div> |
| 139 | + <div class="canvases" id="epoch-loss-canvas"></div> |
129 | 140 | </div> |
| 141 | + </section> |
130 | 142 |
|
131 | | - <div class="vertical-sections"> |
132 | | - <div class="monospace"> |
133 | | - <span class="input-title">Generation length:</span> |
134 | | - <input class="monospace" id="generate-length" value="200"></input> |
135 | | - </div> |
136 | | - <div class="monospace"> |
137 | | - <span class="input-title">Generation temperature:</span> |
138 | | - <input class="monospace" id="temperature" value="0.75"></input> |
139 | | - </div> |
140 | | - <div> |
141 | | - <button id="generate-text" disabled="true">Generate text</button> |
142 | | - <span id="text-generation-status"></span> |
143 | | - </div> |
144 | | - <div> |
145 | | - <span class="input-title">Seed text:</span> |
146 | | - <textarea class="monospace" id="seed-text" value="" rows="4"></textarea> |
147 | | - </div> |
148 | | - <div> |
149 | | - <span class="input-title">Generated text:</span> |
150 | | - <textarea class="monospace" id="generated-text" readonly="true" value="" rows="10"></textarea> |
151 | | - </div> |
| 143 | + <section> |
| 144 | + <p class='section-head'>Text Generation Parameters</p> |
| 145 | + <p> |
| 146 | + To generate text the model needs to have some number of preceding characters from which it continues, |
| 147 | + we call these characters the <em>seed text</em>. You can type one in, or we will extract a random substring from |
| 148 | + the input text to be the seed text. Note that the seed text must be at least 40 charachters long. |
| 149 | + </p> |
| 150 | + <div> |
| 151 | + <span class="input-title">Length of generated text:</span> |
| 152 | + <input id="generate-length" value="200"></input> |
| 153 | + </div> |
| 154 | + |
| 155 | + <div> |
| 156 | + <span class="input-title">Generation temperature:</span> |
| 157 | + <input id="temperature" value="0.75"></input> |
| 158 | + </div> |
| 159 | + |
| 160 | + <div> |
| 161 | + <button id="generate-text" disabled="true">Generate text</button> |
| 162 | + <span id="text-generation-status"></span> |
152 | 163 | </div> |
153 | | - </div> |
| 164 | + |
| 165 | + <div> |
| 166 | + <span class="input-title">Seed text:</span> |
| 167 | + <textarea class="monospace" id="seed-text" value="" rows="4"></textarea> |
| 168 | + </div> |
| 169 | + |
| 170 | + <p class='section-head'>Model Output</p> |
| 171 | + |
| 172 | + <div> |
| 173 | + <span class="input-title">Generated text:</span> |
| 174 | + <textarea class="monospace" id="generated-text" readonly="true" value="" rows="10"></textarea> |
| 175 | + </div> |
| 176 | + |
| 177 | + </section> |
154 | 178 |
|
155 | 179 | </body> |
156 | 180 |
|
|
0 commit comments