|
4 | 4 |
|
5 | 5 | from typing import Any, Optional, Sequence |
6 | 6 |
|
7 | | -import numpy |
| 7 | +import numpy as np |
8 | 8 | import onnx |
9 | 9 | from onnx import FunctionProto, GraphProto, ModelProto, TensorProto, ValueInfoProto |
10 | 10 |
|
@@ -384,17 +384,17 @@ def _translate_attributes(self, node): |
384 | 384 | if isinstance(value, str): |
385 | 385 | attributes.append((at.name, f"{value!r}")) |
386 | 386 | continue |
387 | | - if isinstance(value, numpy.ndarray): |
| 387 | + if isinstance(value, np.ndarray): |
388 | 388 | onnx_dtype = at.t.data_type |
389 | 389 | if len(value.shape) == 0: |
390 | 390 | text = ( |
391 | 391 | f'make_tensor("value", {onnx_dtype}, dims=[], ' |
392 | | - f"vals=[{value.tolist()!r}])" |
| 392 | + f"vals=[{repr(value.tolist()).replace('nan', 'np.nan').replace('inf', 'np.inf')}])" |
393 | 393 | ) |
394 | 394 | else: |
395 | 395 | text = ( |
396 | 396 | f'make_tensor("value", {onnx_dtype}, dims={list(value.shape)!r}, ' |
397 | | - f"vals={value.ravel().tolist()!r})" |
| 397 | + f"vals={repr(value.ravel().tolist()).replace('nan', 'np.nan').replace('inf', 'np.inf')})" |
398 | 398 | ) |
399 | 399 | attributes.append((at.name, text)) |
400 | 400 | continue |
@@ -738,7 +738,7 @@ def generate_rand(name: str, value: TensorProto) -> str: |
738 | 738 | raise NotImplementedError( |
739 | 739 | f"Unable to generate random initializer for data type {value.data_type}." |
740 | 740 | ) |
741 | | - return f"{__}{name} = numpy.random.rand({shape}).astype(numpy.float32)" |
| 741 | + return f"{__}{name} = np.random.rand({shape}).astype(np.float32)" |
742 | 742 |
|
743 | 743 | random_initializer_values = "\n".join( |
744 | 744 | generate_rand(key, value) for key, value in self.skipped_initializers.items() |
@@ -793,7 +793,7 @@ def add(line: str) -> None: |
793 | 793 | result.append(line) |
794 | 794 |
|
795 | 795 | # Generic imports. |
796 | | - add("import numpy") |
| 796 | + add("import numpy as np") |
797 | 797 | add("from onnx import TensorProto") |
798 | 798 | add("from onnx.helper import make_tensor") |
799 | 799 | add("from onnxscript import script, external_tensor") |
@@ -873,11 +873,11 @@ def export2python( |
873 | 873 | .. runpython:: |
874 | 874 | :showcode: |
875 | 875 | :process: |
876 | | - import numpy |
| 876 | + import numpy as np |
877 | 877 | from sklearn.cluster import KMeans |
878 | 878 | from mlprodict.onnx_conv import to_onnx |
879 | 879 | from mlprodict.onnx_tools.onnx_export import export2python |
880 | | - X = numpy.arange(20).reshape(10, 2).astype(numpy.float32) |
| 880 | + X = np.arange(20).reshape(10, 2).astype(np.float32) |
881 | 881 | tr = KMeans(n_clusters=2) |
882 | 882 | tr.fit(X) |
883 | 883 | onx = to_onnx(tr, X, target_opset=14) |
|
0 commit comments