GitOrigin-RevId: 09cf6518e8
release-1.1
@@ -208,25 +208,16 @@ def broadcast_to(inp: Tensor, shape: Union[int, Iterable[int]]) -> Tensor: | |||||
from megengine import tensor | from megengine import tensor | ||||
import megengine.functional as F | import megengine.functional as F | ||||
data = tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3)) | |||||
out = F.broadcast_to(data, (4, 2, 3)) | |||||
data = tensor(np.arange(0, 3, dtype=np.float32).reshape(3)) | |||||
out = F.broadcast_to(data, (2, 3)) | |||||
print(out.numpy()) | print(out.numpy()) | ||||
Outputs: | Outputs: | ||||
.. testoutput:: | .. testoutput:: | ||||
[[[0. 1. 2.] | |||||
[3. 4. 5.]] | |||||
[[0. 1. 2.] | |||||
[3. 4. 5.]] | |||||
[[0. 1. 2.] | |||||
[3. 4. 5.]] | |||||
[[0. 1. 2.] | |||||
[3. 4. 5.]]] | |||||
[[0. 1. 2.] | |||||
[0. 1. 2.]] | |||||
""" | """ | ||||
return _broadcast(inp, shape) | return _broadcast(inp, shape) | ||||
@@ -298,8 +289,8 @@ def stack(inps, axis=0, device=None): | |||||
from megengine import tensor | from megengine import tensor | ||||
import megengine.functional as F | import megengine.functional as F | ||||
x1 = tensor(np.arange(0, 6, dtype=np.float32).reshape((2, 3))) | |||||
x2 = tensor(np.arange(6, 12, dtype=np.float32).reshape((2, 3))) | |||||
x1 = tensor(np.arange(0, 3, dtype=np.float32).reshape((3))) | |||||
x2 = tensor(np.arange(6, 9, dtype=np.float32).reshape((3))) | |||||
out = F.stack([x1, x2], axis=0) | out = F.stack([x1, x2], axis=0) | ||||
print(out.numpy()) | print(out.numpy()) | ||||
@@ -307,11 +298,8 @@ def stack(inps, axis=0, device=None): | |||||
.. testoutput:: | .. testoutput:: | ||||
[[[ 0. 1. 2.] | |||||
[ 3. 4. 5.]] | |||||
[[ 6. 7. 8.] | |||||
[ 9. 10. 11.]]] | |||||
[[0. 1. 2.] | |||||
[6. 7. 8.]] | |||||
""" | """ | ||||
if len(inps) > 0 and not isinstance(inps[0].shape, inps[0].__class__): | if len(inps) > 0 and not isinstance(inps[0].shape, inps[0].__class__): | ||||
@@ -751,21 +739,16 @@ def reshape(inp: Tensor, target_shape: Iterable[int]) -> Tensor: | |||||
from megengine import tensor | from megengine import tensor | ||||
import megengine.functional as F | import megengine.functional as F | ||||
x = tensor(np.arange(12, dtype=np.int32)) | x = tensor(np.arange(12, dtype=np.int32)) | ||||
out = F.reshape(x, (3, 2, 2)) | |||||
out = F.reshape(x, (3, 4)) | |||||
print(out.numpy()) | print(out.numpy()) | ||||
Outputs: | Outputs: | ||||
.. testoutput:: | .. testoutput:: | ||||
[[[ 0 1] | |||||
[ 2 3]] | |||||
[[ 4 5] | |||||
[ 6 7]] | |||||
[[ 8 9] | |||||
[10 11]]] | |||||
[[ 0 1 2 3] | |||||
[ 4 5 6 7] | |||||
[ 8 9 10 11]] | |||||
""" | """ | ||||
return inp.reshape(target_shape) | return inp.reshape(target_shape) | ||||
@@ -38,10 +38,10 @@ class Embedding(Module): | |||||
import numpy as np | import numpy as np | ||||
import megengine as mge | import megengine as mge | ||||
import megengine.module as M | import megengine.module as M | ||||
weight = mge.tensor(np.array([(1.2,2.3,3.4,4.5,5.6),(0.1,1.1,2.1,3.1,4.1)], dtype=np.float32)) | |||||
data = mge.tensor(np.array([(0,1,1),(1,0,1),(0,0,1)], dtype=np.int32)) | |||||
weight = mge.tensor(np.array([(1.2,2.3,3.4,4.5,5.6)], dtype=np.float32)) | |||||
data = mge.tensor(np.array([(0,0)], dtype=np.int32)) | |||||
embedding = M.Embedding(2, 5, initial_weight=weight) | |||||
embedding = M.Embedding(1, 5, initial_weight=weight) | |||||
output = embedding(data) | output = embedding(data) | ||||
with np.printoptions(precision=6): | with np.printoptions(precision=6): | ||||
print(output.numpy()) | print(output.numpy()) | ||||
@@ -51,16 +51,7 @@ class Embedding(Module): | |||||
.. testoutput:: | .. testoutput:: | ||||
[[[1.2 2.3 3.4 4.5 5.6] | [[[1.2 2.3 3.4 4.5 5.6] | ||||
[0.1 1.1 2.1 3.1 4.1] | |||||
[0.1 1.1 2.1 3.1 4.1]] | |||||
[[0.1 1.1 2.1 3.1 4.1] | |||||
[1.2 2.3 3.4 4.5 5.6] | |||||
[0.1 1.1 2.1 3.1 4.1]] | |||||
[[1.2 2.3 3.4 4.5 5.6] | |||||
[1.2 2.3 3.4 4.5 5.6] | |||||
[0.1 1.1 2.1 3.1 4.1]]] | |||||
[1.2 2.3 3.4 4.5 5.6]]] | |||||
""" | """ | ||||
@@ -134,8 +125,8 @@ class Embedding(Module): | |||||
import numpy as np | import numpy as np | ||||
import megengine as mge | import megengine as mge | ||||
import megengine.module as M | import megengine.module as M | ||||
weight = mge.tensor(np.array([(1.2,2.3,3.4,4.5,5.6),(0.1,1.1,2.1,3.1,4.1)], dtype=np.float32)) | |||||
data = mge.tensor(np.array([(0,1,1),(1,0,1),(0,0,1)], dtype=np.int32)) | |||||
weight = mge.tensor(np.array([(1.2,2.3,3.4,4.5,5.6)], dtype=np.float32)) | |||||
data = mge.tensor(np.array([(0,0)], dtype=np.int32)) | |||||
embedding = M.Embedding.from_pretrained(weight, freeze=False) | embedding = M.Embedding.from_pretrained(weight, freeze=False) | ||||
output = embedding(data) | output = embedding(data) | ||||
@@ -146,17 +137,7 @@ class Embedding(Module): | |||||
.. testoutput:: | .. testoutput:: | ||||
[[[1.2 2.3 3.4 4.5 5.6] | [[[1.2 2.3 3.4 4.5 5.6] | ||||
[0.1 1.1 2.1 3.1 4.1] | |||||
[0.1 1.1 2.1 3.1 4.1]] | |||||
[[0.1 1.1 2.1 3.1 4.1] | |||||
[1.2 2.3 3.4 4.5 5.6] | |||||
[0.1 1.1 2.1 3.1 4.1]] | |||||
[[1.2 2.3 3.4 4.5 5.6] | |||||
[1.2 2.3 3.4 4.5 5.6] | |||||
[0.1 1.1 2.1 3.1 4.1]]] | |||||
[1.2 2.3 3.4 4.5 5.6]]] | |||||
""" | """ | ||||
embeddings_shape = embeddings.shape | embeddings_shape = embeddings.shape | ||||