Browse Source

Fix Tensor.from_numpy() returns wrong type

Tensor.from_numpy() should return mindspore.Tensor not _c_expression.Tensor.
tags/v1.0.0
He Wei 5 years ago
parent
commit
27721ff4ea
2 changed files with 9 additions and 0 deletions
  1. +5
    -0
      mindspore/common/tensor.py
  2. +4
    -0
      tests/ut/python/ir/test_tensor.py

+ 5
- 0
mindspore/common/tensor.py View File

@@ -232,6 +232,11 @@ class Tensor(Tensor_):
raise TypeError("virtual_flag must be bool.")
self._virtual_flag = value

@staticmethod
def from_numpy(array):
"""Convert numpy array to Tensor without copy data."""
return Tensor(Tensor_.from_numpy(array))

def asnumpy(self):
"""Convert tensor to numpy array."""
return Tensor_.asnumpy(self)


+ 4
- 0
tests/ut/python/ir/test_tensor.py View File

@@ -480,6 +480,7 @@ def test_tensor_operation():
def test_tensor_from_numpy():
a = np.ones((2, 3))
t = ms.Tensor.from_numpy(a)
assert isinstance(t, ms.Tensor)
assert np.all(t.asnumpy() == 1)
# 't' and 'a' share same data.
a[1] = 2
@@ -489,3 +490,6 @@ def test_tensor_from_numpy():
del a
assert np.all(t.asnumpy()[0] == 1)
assert np.all(t.asnumpy()[1] == 2)
with pytest.raises(TypeError):
# incorrect input.
t = ms.Tensor.from_numpy([1, 2, 3])

Loading…
Cancel
Save