MCPcopy Create free account
hub / github.com/XL2248/MSCTD / create_sampling_graph

Function create_sampling_graph

src_code/thumt1_code/thumt/utils/sampling.py:151–214  ·  view source on GitHub ↗
(models, features, params)

Source from the content-addressed store, hash-verified

149
150
151def create_sampling_graph(models, features, params):
152 if not isinstance(models, (list, tuple)):
153 raise ValueError("'models' must be a list or tuple")
154
155 features = copy.copy(features)
156 model_fns = [model.get_inference_func() for model in models]
157
158 num_samples = params.num_samples
159
160 # Compute initial state if necessary
161 states = []
162 funcs = []
163
164 for model_fn in model_fns:
165 if callable(model_fn):
166 # For non-incremental decoding
167 states.append({})
168 funcs.append(model_fn)
169 else:
170 # For incremental decoding where model_fn is a tuple:
171 # (encoding_fn, decoding_fn)
172 states.append(model_fn[0](features))
173 funcs.append(model_fn[1])
174
175 batch_size = tf.shape(features["source"])[0]
176 pad_id = params.mapping["target"][params.pad]
177 bos_id = params.mapping["target"][params.bos]
178 eos_id = params.mapping["target"][params.eos]
179
180 # Expand the inputs
181 features["source"] = utils.tile_batch(features["source"], num_samples)
182 features["source_length"] = utils.tile_batch(features["source_length"],
183 num_samples)
184
185 min_length = tf.to_float(features["source_length"])
186 max_length = tf.to_float(features["source_length"])
187
188 if params.min_length_ratio:
189 min_length = min_length * params.min_length_ratio
190
191 if params.max_length_ratio:
192 max_length = max_length * params.max_length_ratio
193
194 if params.min_sample_length:
195 min_length = min_length - params.min_sample_length
196
197 if params.max_sample_length:
198 max_length = max_length + params.max_sample_length
199
200 min_length = tf.to_int32(min_length)
201 max_length = tf.to_int32(max_length)
202
203 decoding_fn = _get_inference_fn(funcs, features)
204 states = nest.map_structure(lambda x: utils.tile_batch(x, num_samples),
205 states)
206
207 seqs, scores = random_sample(decoding_fn, states, batch_size * num_samples,
208 min_length, max_length, pad_id, bos_id,

Callers

nothing calls this directly

Calls 3

_get_inference_fnFunction · 0.70
random_sampleFunction · 0.70
get_inference_funcMethod · 0.45

Tested by

no test coverage detected