Serializes the model variables.
(self, path)
| 380 | shutil.copy(path, destination) |
| 381 | |
| 382 | def _serialize(self, path): |
| 383 | """Serializes the model variables.""" |
| 384 | variables = [] |
| 385 | aliases = [] |
| 386 | for variable in self.variables(ordered=True): |
| 387 | if isinstance(variable[1], str): |
| 388 | aliases.append(variable) |
| 389 | else: |
| 390 | variables.append(variable) |
| 391 | |
| 392 | with open(path, "wb") as model: |
| 393 | |
| 394 | def _write_string(string): |
| 395 | model.write(struct.pack("H", len(string) + 1)) |
| 396 | model.write(string.encode("utf-8")) |
| 397 | model.write(struct.pack("B", 0)) |
| 398 | |
| 399 | model.write(struct.pack("I", CURRENT_BINARY_VERSION)) |
| 400 | _write_string(self.name) |
| 401 | model.write(struct.pack("I", self.revision)) |
| 402 | model.write(struct.pack("I", len(variables))) |
| 403 | for name, value in variables: |
| 404 | _write_string(name) |
| 405 | model.write(struct.pack("B", len(value.shape))) |
| 406 | for dim in value.shape: |
| 407 | model.write(struct.pack("I", dim)) |
| 408 | model.write(struct.pack("B", _dtype_to_type_id(value.dtype))) |
| 409 | model.write(struct.pack("I", value.num_bytes())) |
| 410 | model.write(value.to_bytes()) |
| 411 | model.write(struct.pack("I", len(aliases))) |
| 412 | for alias, variable_name in aliases: |
| 413 | _write_string(alias) |
| 414 | _write_string(variable_name) |
| 415 | |
| 416 | |
| 417 | def _flatten_vocabularies(vocabularies): |
no test coverage detected