Skip to content

Commit 1192f3d

Browse files
committed
update the constructor
1 parent 0ed5ade commit 1192f3d

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

python/paddle/__init__.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,31 @@
8686
else:
8787
Tensor = framework.core.eager.Tensor
8888
Tensor.__qualname__ = 'Tensor'
89+
super_init = Tensor.__init__
90+
91+
def new_init(self, *args, **kwargs):
92+
kwargs_cnt = len(kwargs.keys())
93+
if kwargs_cnt:
94+
super_init(self, *args, **kwargs)
95+
return
96+
if len(args) == 0:
97+
super_init(self, paddle.empty(shape=[0], dtype="float32"))
98+
return
99+
elif len(args) == 1 and isinstance(args[0], (list, tuple)):
100+
super_init(self, paddle.to_tensor(args[0], dtype="float32"))
101+
return
102+
create_random_tensor = True
103+
for arg in args:
104+
if not isinstance(arg, int):
105+
create_random_tensor = False
106+
break
107+
if create_random_tensor:
108+
super_init(self, paddle.randn(list(args), dtype="float32"))
109+
return
110+
else:
111+
super_init(self, *args, **kwargs)
112+
113+
Tensor.__init__ = new_init
89114

90115
import paddle.distributed.fleet
91116
import paddle.text
@@ -199,7 +224,6 @@
199224
)
200225
from .tensor.creation import (
201226
MmapStorage,
202-
Tensor,
203227
arange,
204228
assign,
205229
cauchy_,

0 commit comments

Comments
 (0)