You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_array_ops.py 19 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """unit tests for numpy array operations"""
  16. import functools
  17. import pytest
  18. import numpy as onp
  19. import mindspore.context as context
  20. import mindspore.numpy as mnp
  21. from mindspore.nn import Cell
  22. from ..ut_filter import non_graph_engine
  23. from ....mindspore_test_framework.mindspore_test import mindspore_test
  24. from ....mindspore_test_framework.pipeline.forward.compile_forward \
  25. import pipeline_for_compile_forward_ge_graph_for_case_by_case_config
  26. class Cases():
  27. def __init__(self):
  28. self.all_shapes = [
  29. 0, 1, 2, (), (1,), (2,), (1, 2, 3), [], [1], [2], [1, 2, 3]
  30. ]
  31. self.onp_dtypes = [onp.int32, 'int32', int,
  32. onp.float32, 'float32', float,
  33. onp.uint32, 'uint32',
  34. onp.bool_, 'bool', bool]
  35. self.mnp_dtypes = [mnp.int32, 'int32', int,
  36. mnp.float32, 'float32', float,
  37. mnp.uint32, 'uint32',
  38. mnp.bool_, 'bool', bool]
  39. self.array_sets = [1, 1.1, True, [1, 0, True], [1, 1.0, 2], (1,),
  40. [(1, 2, 3), (4, 5, 6)], onp.random.random(
  41. (100, 100)).astype(onp.float32),
  42. onp.random.random((100, 100)).astype(onp.bool)]
  43. def match_array(actual, expected, error=0):
  44. if error > 0:
  45. onp.testing.assert_almost_equal(actual.tolist(), expected.tolist(),
  46. decimal=error)
  47. else:
  48. onp.testing.assert_equal(actual.tolist(), expected.tolist())
  49. def check_all_results(onp_results, mnp_results):
  50. """Check all results from numpy and mindspore.numpy"""
  51. for i, _ in enumerate(onp_results):
  52. match_array(onp_results[i], mnp_results[i].asnumpy())
  53. def test_asarray():
  54. test_case = Cases()
  55. for array in test_case.array_sets:
  56. # Check for dtype matching
  57. actual = onp.asarray(array)
  58. expected = mnp.asarray(array).asnumpy()
  59. # Since we set float32/int32 as the default dtype in mindspore, we need
  60. # to make a conversion between numpy.asarray and mindspore.numpy.asarray
  61. if actual.dtype is onp.dtype('float64'):
  62. assert expected.dtype == onp.dtype('float32')
  63. elif actual.dtype is onp.dtype('int64'):
  64. assert expected.dtype == onp.dtype('int32')
  65. else:
  66. assert actual.dtype == expected.dtype
  67. match_array(actual, expected, error=7)
  68. for i in range(len(test_case.onp_dtypes)):
  69. actual = onp.asarray(array, test_case.onp_dtypes[i])
  70. expected = mnp.asarray(array, test_case.mnp_dtypes[i]).asnumpy()
  71. match_array(actual, expected, error=7)
  72. # Additional tests for nested tensor/numpy_array mixture
  73. mnp_input = [(onp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  74. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  75. actual = onp.asarray(onp_input)
  76. expected = mnp.asarray(mnp_input).asnumpy()
  77. match_array(actual, expected, error=7)
  78. def test_array():
  79. # array's function is very similar to asarray, so we mainly test the
  80. # `copy` argument.
  81. test_case = Cases()
  82. for array in test_case.array_sets:
  83. arr1 = mnp.asarray(array)
  84. arr2 = mnp.array(arr1, copy=False)
  85. arr3 = mnp.array(arr1)
  86. arr4 = mnp.asarray(array, dtype='int32')
  87. arr5 = mnp.asarray(arr4, dtype=mnp.int32)
  88. assert arr1 is arr2
  89. assert arr1 is not arr3
  90. assert arr4 is arr5
  91. # Additional tests for nested tensor/numpy_array mixture
  92. mnp_input = [(onp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  93. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  94. actual = onp.asarray(onp_input)
  95. expected = mnp.asarray(mnp_input).asnumpy()
  96. match_array(actual, expected, error=7)
  97. def test_asfarray():
  98. test_case = Cases()
  99. for array in test_case.array_sets:
  100. # Check for dtype matching
  101. actual = onp.asfarray(array)
  102. expected = mnp.asfarray(array).asnumpy()
  103. # Since we set float32/int32 as the default dtype in mindspore, we need
  104. # to make a conversion between numpy.asarray and mindspore.numpy.asarray
  105. if actual.dtype is onp.dtype('float64'):
  106. assert expected.dtype == onp.dtype('float32')
  107. else:
  108. assert actual.dtype == expected.dtype
  109. match_array(actual, expected, error=7)
  110. for i in range(len(test_case.onp_dtypes)):
  111. actual = onp.asfarray(array, test_case.onp_dtypes[i])
  112. expected = mnp.asfarray(array, test_case.mnp_dtypes[i]).asnumpy()
  113. match_array(actual, expected, error=7)
  114. # Additional tests for nested tensor/numpy_array mixture
  115. mnp_input = [(onp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  116. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  117. actual = onp.asarray(onp_input)
  118. expected = mnp.asarray(mnp_input).asnumpy()
  119. match_array(actual, expected, error=7)
  120. def test_zeros():
  121. test_case = Cases()
  122. for shape in test_case.all_shapes:
  123. for i in range(len(test_case.onp_dtypes)):
  124. actual = onp.zeros(shape, test_case.onp_dtypes[i])
  125. expected = mnp.zeros(shape, test_case.mnp_dtypes[i]).asnumpy()
  126. match_array(actual, expected)
  127. actual = onp.zeros(shape)
  128. expected = mnp.zeros(shape).asnumpy()
  129. match_array(actual, expected)
  130. def test_ones():
  131. test_case = Cases()
  132. for shape in test_case.all_shapes:
  133. for i in range(len(test_case.onp_dtypes)):
  134. actual = onp.ones(shape, test_case.onp_dtypes[i])
  135. expected = mnp.ones(shape, test_case.mnp_dtypes[i]).asnumpy()
  136. match_array(actual, expected)
  137. actual = onp.ones(shape)
  138. expected = mnp.ones(shape).asnumpy()
  139. match_array(actual, expected)
  140. def test_full():
  141. actual = onp.full((2, 2), [1, 2])
  142. expected = mnp.full((2, 2), [1, 2]).asnumpy()
  143. match_array(actual, expected)
  144. actual = onp.full((2, 0), onp.inf)
  145. expected = mnp.full((2, 0), mnp.inf).asnumpy()
  146. match_array(actual, expected)
  147. actual = onp.full((2, 3), True)
  148. expected = mnp.full((2, 3), True).asnumpy()
  149. match_array(actual, expected)
  150. actual = onp.full((3, 4, 5), 7.5)
  151. expected = mnp.full((3, 4, 5), 7.5).asnumpy()
  152. match_array(actual, expected)
  153. def test_eye():
  154. test_case = Cases()
  155. for i in range(len(test_case.onp_dtypes)):
  156. for m in range(1, 5):
  157. actual = onp.eye(m, dtype=test_case.onp_dtypes[i])
  158. expected = mnp.eye(m, dtype=test_case.mnp_dtypes[i]).asnumpy()
  159. match_array(actual, expected)
  160. for n in range(1, 5):
  161. for k in range(0, 5):
  162. actual = onp.eye(m, n, k, dtype=test_case.onp_dtypes[i])
  163. expected = mnp.eye(
  164. m, n, k, dtype=test_case.mnp_dtypes[i]).asnumpy()
  165. match_array(actual, expected)
  166. def test_identity():
  167. test_case = Cases()
  168. for i in range(len(test_case.onp_dtypes)):
  169. for m in range(1, 5):
  170. actual = onp.identity(m, dtype=test_case.onp_dtypes[i])
  171. expected = mnp.identity(m, dtype=test_case.mnp_dtypes[i]).asnumpy()
  172. match_array(actual, expected)
  173. def test_arange():
  174. actual = onp.arange(10)
  175. expected = mnp.arange(10).asnumpy()
  176. match_array(actual, expected)
  177. actual = onp.arange(0, 10)
  178. expected = mnp.arange(0, 10).asnumpy()
  179. match_array(actual, expected)
  180. actual = onp.arange(start=10)
  181. expected = mnp.arange(start=10).asnumpy()
  182. match_array(actual, expected)
  183. actual = onp.arange(start=10, step=0.1)
  184. expected = mnp.arange(start=10, step=0.1).asnumpy()
  185. match_array(actual, expected, error=6)
  186. actual = onp.arange(10, step=0.1)
  187. expected = mnp.arange(10, step=0.1).asnumpy()
  188. match_array(actual, expected, error=6)
  189. actual = onp.arange(0.1, 9.9)
  190. expected = mnp.arange(0.1, 9.9).asnumpy()
  191. match_array(actual, expected, error=6)
  192. def test_linspace():
  193. actual = onp.linspace(2.0, 3.0, dtype=onp.float32)
  194. expected = mnp.linspace(2.0, 3.0).asnumpy()
  195. match_array(actual, expected, error=7)
  196. actual = onp.linspace(2.0, 3.0, num=5, dtype=onp.float32)
  197. expected = mnp.linspace(2.0, 3.0, num=5).asnumpy()
  198. match_array(actual, expected, error=7)
  199. actual = onp.linspace(
  200. 2.0, 3.0, num=5, endpoint=False, dtype=onp.float32)
  201. expected = mnp.linspace(2.0, 3.0, num=5, endpoint=False).asnumpy()
  202. match_array(actual, expected, error=7)
  203. actual = onp.linspace(2.0, 3.0, num=5, retstep=True, dtype=onp.float32)
  204. expected = mnp.linspace(2.0, 3.0, num=5, retstep=True)
  205. match_array(actual[0], expected[0].asnumpy())
  206. assert actual[1] == expected[1]
  207. actual = onp.linspace(2.0, [3, 4, 5], num=5,
  208. endpoint=False, dtype=onp.float32)
  209. expected = mnp.linspace(
  210. 2.0, [3, 4, 5], num=5, endpoint=False).asnumpy()
  211. match_array(actual, expected)
  212. def test_logspace():
  213. actual = onp.logspace(2.0, 3.0, dtype=onp.float32)
  214. expected = mnp.logspace(2.0, 3.0).asnumpy()
  215. match_array(actual, expected)
  216. actual = onp.logspace(2.0, 3.0, num=5, dtype=onp.float32)
  217. expected = mnp.logspace(2.0, 3.0, num=5).asnumpy()
  218. match_array(actual, expected)
  219. actual = onp.logspace(
  220. 2.0, 3.0, num=5, endpoint=False, dtype=onp.float32)
  221. expected = mnp.logspace(2.0, 3.0, num=5, endpoint=False).asnumpy()
  222. match_array(actual, expected)
  223. actual = onp.logspace(2.0, [3, 4, 5], num=5,
  224. endpoint=False, dtype=onp.float32)
  225. expected = mnp.logspace(
  226. 2.0, [3, 4, 5], num=5, endpoint=False).asnumpy()
  227. match_array(actual, expected)
  228. # Test np.transpose and np.ndarray.transpose
  229. def mnp_transpose(input_tensor):
  230. a = mnp.transpose(input_tensor, (0, 2, 1))
  231. b = mnp.transpose(input_tensor, [2, 1, 0])
  232. c = mnp.transpose(input_tensor, (1, 0, 2))
  233. d = mnp.transpose(input_tensor)
  234. return a, b, c, d
  235. def onp_transpose(input_array):
  236. a = onp.transpose(input_array, (0, 2, 1))
  237. b = onp.transpose(input_array, [2, 1, 0])
  238. c = onp.transpose(input_array, (1, 0, 2))
  239. d = onp.transpose(input_array)
  240. return a, b, c, d
  241. # Test np.expand_dims
  242. def mnp_expand_dims(input_tensor):
  243. a = mnp.expand_dims(input_tensor, 0)
  244. b = mnp.expand_dims(input_tensor, -1)
  245. c = mnp.expand_dims(input_tensor, axis=2)
  246. d = mnp.expand_dims(input_tensor, axis=-2)
  247. return a, b, c, d
  248. def onp_expand_dims(input_array):
  249. a = onp.expand_dims(input_array, 0)
  250. b = onp.expand_dims(input_array, -1)
  251. c = onp.expand_dims(input_array, axis=2)
  252. d = onp.expand_dims(input_array, axis=-2)
  253. return a, b, c, d
  254. # Test np.squeeze
  255. def mnp_squeeze(input_tensor):
  256. a = mnp.squeeze(input_tensor)
  257. b = mnp.squeeze(input_tensor, 0)
  258. c = mnp.squeeze(input_tensor, axis=None)
  259. d = mnp.squeeze(input_tensor, axis=-3)
  260. e = mnp.squeeze(input_tensor, (2,))
  261. f = mnp.squeeze(input_tensor, (0, 2))
  262. return a, b, c, d, e, f
  263. def onp_squeeze(input_array):
  264. a = onp.squeeze(input_array)
  265. b = onp.squeeze(input_array, 0)
  266. c = onp.squeeze(input_array, axis=None)
  267. d = onp.squeeze(input_array, axis=-3)
  268. e = onp.squeeze(input_array, (2,))
  269. f = onp.squeeze(input_array, (0, 2))
  270. return a, b, c, d, e, f
  271. # Test np.rollaxis
  272. def mnp_rollaxis(input_tensor):
  273. a = mnp.rollaxis(input_tensor, 0, 1)
  274. b = mnp.rollaxis(input_tensor, 0, 2)
  275. c = mnp.rollaxis(input_tensor, 2, 1)
  276. d = mnp.rollaxis(input_tensor, 2, 2)
  277. e = mnp.rollaxis(input_tensor, 0)
  278. f = mnp.rollaxis(input_tensor, 1)
  279. return a, b, c, d, e, f
  280. def onp_rollaxis(input_array):
  281. a = onp.rollaxis(input_array, 0, 1)
  282. b = onp.rollaxis(input_array, 0, 2)
  283. c = onp.rollaxis(input_array, 2, 1)
  284. d = onp.rollaxis(input_array, 2, 2)
  285. e = onp.rollaxis(input_array, 0)
  286. f = onp.rollaxis(input_array, 1)
  287. return a, b, c, d, e, f
  288. # Test np.swapaxes
  289. def mnp_swapaxes(input_tensor):
  290. a = mnp.swapaxes(input_tensor, 0, 1)
  291. b = mnp.swapaxes(input_tensor, 1, 0)
  292. c = mnp.swapaxes(input_tensor, 1, 1)
  293. d = mnp.swapaxes(input_tensor, 2, 1)
  294. e = mnp.swapaxes(input_tensor, 1, 2)
  295. f = mnp.swapaxes(input_tensor, 2, 2)
  296. return a, b, c, d, e, f
  297. def onp_swapaxes(input_array):
  298. a = onp.swapaxes(input_array, 0, 1)
  299. b = onp.swapaxes(input_array, 1, 0)
  300. c = onp.swapaxes(input_array, 1, 1)
  301. d = onp.swapaxes(input_array, 2, 1)
  302. e = onp.swapaxes(input_array, 1, 2)
  303. f = onp.swapaxes(input_array, 2, 2)
  304. return a, b, c, d, e, f
  305. # Test np.reshape
  306. def mnp_reshape(input_tensor):
  307. a = mnp.reshape(input_tensor, (3, 8))
  308. b = mnp.reshape(input_tensor, [3, -1])
  309. c = mnp.reshape(input_tensor, (-1, 12))
  310. d = mnp.reshape(input_tensor, (-1,))
  311. e = mnp.reshape(input_tensor, 24)
  312. f = mnp.reshape(input_tensor, [2, 4, -1])
  313. return a, b, c, d, e, f
  314. def onp_reshape(input_array):
  315. a = onp.reshape(input_array, (3, 8))
  316. b = onp.reshape(input_array, [3, -1])
  317. c = onp.reshape(input_array, (-1, 12))
  318. d = onp.reshape(input_array, (-1,))
  319. e = onp.reshape(input_array, 24)
  320. f = onp.reshape(input_array, [2, 4, -1])
  321. return a, b, c, d, e, f
  322. # Test np.ravel
  323. def mnp_ravel(input_tensor):
  324. a = mnp.ravel(input_tensor)
  325. return a
  326. def onp_ravel(input_array):
  327. a = onp.ravel(input_array)
  328. return a
  329. # Test np.concatenate
  330. def mnp_concatenate(input_tensor):
  331. a = mnp.concatenate(input_tensor, None)
  332. b = mnp.concatenate(input_tensor, 0)
  333. c = mnp.concatenate(input_tensor, 1)
  334. d = mnp.concatenate(input_tensor, 2)
  335. return a, b, c, d
  336. def onp_concatenate(input_array):
  337. a = onp.concatenate(input_array, None)
  338. b = onp.concatenate(input_array, 0)
  339. c = onp.concatenate(input_array, 1)
  340. d = onp.concatenate(input_array, 2)
  341. return a, b, c, d
  342. def test_transpose():
  343. onp_array = onp.random.random((3, 4, 5)).astype('float32')
  344. mnp_array = mnp.asarray(onp_array)
  345. o_transposed = onp_transpose(onp_array)
  346. m_transposed = mnp_transpose(mnp_array)
  347. check_all_results(o_transposed, m_transposed)
  348. def test_expand_dims():
  349. onp_array = onp.random.random((3, 4, 5)).astype('float32')
  350. mnp_array = mnp.asarray(onp_array)
  351. o_expanded = onp_expand_dims(onp_array)
  352. m_expanded = mnp_expand_dims(mnp_array)
  353. check_all_results(o_expanded, m_expanded)
  354. def test_squeeze():
  355. onp_array = onp.random.random((1, 3, 1, 4, 2)).astype('float32')
  356. mnp_array = mnp.asarray(onp_array)
  357. o_squeezed = onp_squeeze(onp_array)
  358. m_squeezed = mnp_squeeze(mnp_array)
  359. check_all_results(o_squeezed, m_squeezed)
  360. onp_array = onp.random.random((1, 1, 1, 1, 1)).astype('float32')
  361. mnp_array = mnp.asarray(onp_array)
  362. o_squeezed = onp_squeeze(onp_array)
  363. m_squeezed = mnp_squeeze(mnp_array)
  364. check_all_results(o_squeezed, m_squeezed)
  365. def test_rollaxis():
  366. onp_array = onp.random.random((3, 4, 5)).astype('float32')
  367. mnp_array = mnp.asarray(onp_array)
  368. o_rolled = onp_rollaxis(onp_array)
  369. m_rolled = mnp_rollaxis(mnp_array)
  370. check_all_results(o_rolled, m_rolled)
  371. def test_swapaxes():
  372. onp_array = onp.random.random((3, 4, 5)).astype('float32')
  373. mnp_array = mnp.asarray(onp_array)
  374. o_swaped = onp_swapaxes(onp_array)
  375. m_swaped = mnp_swapaxes(mnp_array)
  376. check_all_results(o_swaped, m_swaped)
  377. def test_reshape():
  378. onp_array = onp.random.random((2, 3, 4)).astype('float32')
  379. mnp_array = mnp.asarray(onp_array)
  380. o_reshaped = onp_reshape(onp_array)
  381. m_reshaped = mnp_reshape(mnp_array)
  382. check_all_results(o_reshaped, m_reshaped)
  383. def test_ravel():
  384. onp_array = onp.random.random((2, 3, 4)).astype('float32')
  385. mnp_array = mnp.asarray(onp_array)
  386. o_ravel = onp_ravel(onp_array)
  387. m_ravel = mnp_ravel(mnp_array).asnumpy()
  388. match_array(o_ravel, m_ravel)
  389. def test_concatenate():
  390. onp_array = onp.random.random((5, 4, 3, 2)).astype('float32')
  391. mnp_array = mnp.asarray(onp_array)
  392. o_concatenate = onp_concatenate(onp_array)
  393. m_concatenate = mnp_concatenate(mnp_array)
  394. check_all_results(o_concatenate, m_concatenate)
  395. class ReshapeExpandSqueeze(Cell):
  396. def __init__(self):
  397. super(ReshapeExpandSqueeze, self).__init__()
  398. def construct(self, x):
  399. x = mnp.expand_dims(x, 2)
  400. x = mnp.reshape(x, (1, 2, 3, 4, 1, 1))
  401. x = mnp.squeeze(x)
  402. return x
  403. class TransposeConcatRavel(Cell):
  404. def __init__(self):
  405. super(TransposeConcatRavel, self).__init__()
  406. def construct(self, x1, x2, x3):
  407. x1 = mnp.transpose(x1, [0, 2, 1])
  408. x2 = x2.transpose(0, 2, 1)
  409. x = mnp.concatenate((x1, x2, x3), -1)
  410. x = mnp.ravel(x)
  411. return x
  412. class RollSwap(Cell):
  413. def __init__(self):
  414. super(RollSwap, self).__init__()
  415. def construct(self, x):
  416. x = mnp.rollaxis(x, 2)
  417. x = mnp.swapaxes(x, 0, 1)
  418. return x
  419. test_case_array_ops = [
  420. ('ReshapeExpandSqueeze', {
  421. 'block': ReshapeExpandSqueeze(),
  422. 'desc_inputs': [mnp.ones((2, 3, 4))]}),
  423. ('TransposeConcatRavel', {
  424. 'block': TransposeConcatRavel(),
  425. 'desc_inputs': [mnp.ones((2, 3, 4)),
  426. mnp.ones((2, 3, 4)),
  427. mnp.ones((2, 4, 1))]}),
  428. ('RollSwap', {
  429. 'block': RollSwap(),
  430. 'desc_inputs': [mnp.ones((2, 3, 4))]})
  431. ]
  432. test_case_lists = [test_case_array_ops]
  433. test_exec_case = functools.reduce(lambda x, y: x + y, test_case_lists)
  434. # use -k to select certain testcast
  435. # pytest tests/python/ops/test_ops.py::test_backward -k LayerNorm
  436. @non_graph_engine
  437. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  438. def test_exec():
  439. context.set_context(mode=context.GRAPH_MODE)
  440. return test_exec_case
  441. def test_expand_dims_exception():
  442. with pytest.raises(TypeError):
  443. mnp.expand_dims(mnp.ones((3, 3)), 1.2)
  444. def test_asarray_exception():
  445. with pytest.raises(TypeError):
  446. mnp.asarray({1, 2, 3})
  447. def test_swapaxes_exception():
  448. with pytest.raises(ValueError):
  449. mnp.swapaxes(mnp.ones((3, 3)), 1, 10)
  450. def test_linspace_exception():
  451. with pytest.raises(TypeError):
  452. mnp.linspace(0, 1, num=2.5)