diff --git a/docs/mindspore/source_en/model_train/program_form/static_graph.rst b/docs/mindspore/source_en/model_train/program_form/static_graph.rst index ce490801335d48a9c5451618bf5c740febedfd5b..b1a3c36f3917757c4a5270bf5214584e526c6930 100644 --- a/docs/mindspore/source_en/model_train/program_form/static_graph.rst +++ b/docs/mindspore/source_en/model_train/program_form/static_graph.rst @@ -106,16 +106,20 @@ Constants Generate Scenes (without using the mutable interface). For example: .. code:: python + import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor - from mindspore import Tensor, jit + context.set_context(mode=ms.GRAPH_MODE) a = 1 b = [Tensor([1]), Tensor([2])] c = ["a", "b", "c"] - @jit - def foo(a, b, c): - return a, b, c + class Net(nn.Cell): + def construct(self, a, b, c): + return a, b, c In the above code, enter ``a``, ``b``, ``c`` are constants. @@ -123,14 +127,19 @@ Constants Generate Scenes .. code:: python - from mindspore import jit, Tensor + import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @jit - def foo(): - a = 1 - b = "2" - c = Tensor([1, 2, 3]) - return a, b, c + class Net(nn.Cell): + def construct(self): + a = 1 + b = "2" + c = Tensor([1, 2, 3]) + return a, b, c In the above code, enter ``a``, ``b``, ``c`` are constants. @@ -138,14 +147,19 @@ Constants Generate Scenes .. code:: python - from mindspore import jit, Tensor + import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @jit - def foo(): - a = Tensor([1, 2, 3]) - b = Tensor([1, 1, 1]) - c = a + b - return c + class Net(nn.Cell): + def construct(self): + a = Tensor([1, 2, 3]) + b = Tensor([1, 1, 1]) + c = a + b + return c In the above code, ``a`` and ``b`` are constants of Tensor generated in the graph mode, so the result of their calculation is also @@ -160,16 +174,22 @@ Variables Generate Scenes .. code:: python - from mindspore import Tensor, jit from mindspore import mutable + import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) a = mutable([Tensor([1]), Tensor([2])]) - @jit - def foo(a): - b = mutable(Tensor([3])) - c = mutable((Tensor([1]), Tensor([2]))) - return a, b, c + + class Net(nn.Cell): + def construct(self, a): + b = mutable(Tensor([3])) + c = mutable((Tensor([1]), Tensor([2]))) + return a, b, c In the above code, ``a`` is generated by calling the mutable interface outside the graph, ``b`` and ``c`` are generated by calling @@ -180,14 +200,20 @@ Variables Generate Scenes .. code:: python - from mindspore import Tensor, jit + import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) a = Tensor([1]) b = (Tensor([1]), Tensor([2])) - @jit - def foo(a, b): - return a, b + + class Net(nn.Cell): + def construct(self, a, b): + return a, b In the above code, ``a`` is the Tensor input as the graph pattern, so it is a variable. But ``b`` is a tuple that is input to the graph @@ -201,15 +227,20 @@ Variables Generate Scenes .. code:: python - from mindspore import Tensor, jit, ops + import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) a = Tensor([1]) b = Tensor([2]) - @jit - def foo(a, b): - c = a + b - return c + class Net(nn.Cell): + def construct(self, a, b): + c = a + b + return c In this case , ``c`` is the result of calculations of ``a`` and ``b`` , and the inputs ``a`` and ``b`` used for the calculation are @@ -243,16 +274,23 @@ bool(). For example: .. code:: python - from mindspore import Tensor, jit + import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @jit - def foo(x): - out1 = int(11.1) - out2 = int(Tensor([10])) - out3 = int(x.asnumpy()) - return out1, out2, out3 - res = foo(Tensor(2)) + class Net(nn.Cell): + def construct(self, x): + out1 = int(11.1) + out2 = int(Tensor([10])) + out3 = int(x.asnumpy()) + return out1, out2, out3 + + net = Net() + res = net(Tensor(2)) print("res[0]:", res[0]) print("res[1]:", res[1]) print("res[2]:", res[2]) @@ -270,12 +308,19 @@ Supporting returning Number. For example: .. code:: python import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor - @ms.jit - def test_return_scalar(x, y): - return x + y + context.set_context(mode=ms.GRAPH_MODE) - res = test_return_scalar(ms.mutable(1), ms.mutable(2)) + + class Net(nn.Cell): + def construct(self, x, y): + return x + y + + net = Net() + res = net(ms.mutable(1), ms.mutable(2)) print(res) The results are as follows: @@ -301,17 +346,23 @@ For example: .. code:: python - from mindspore import jit + import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @jit - def foo(): - var1 = 'Hello!' - var2 = "MindSpore" - var3 = str(123) - var4 = "{} is {}".format("string", var3) - return var1[0], var2[4:9], var1 + var2, var2 * 2, "H" in var1, "My name is %s!" % var2, var4 + class Net(nn.Cell): + def construct(self): + var1 = 'Hello!' + var2 = "MindSpore" + var3 = str(123) + var4 = "{} is {}".format("string", var3) + return var1[0], var2[4:9], var1 + var2, var2 * 2, "H" in var1, "My name is %s!" % var2, var4 - res = foo() + net = Net() + res = net() print("res:", res) The results are as follows: @@ -340,14 +391,19 @@ The basic usage scenarios of 'List' are as follows: import numpy as np import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @ms.jit - def generate_list(): - a = [1, 2, 3, 4] - b = ["1", "2", "a"] - c = [ms.Tensor([1]), ms.Tensor([2])] - d = [a, b, c, (4, 5)] - return d + class Net(nn.Cell): + def construct(self): + a = [1, 2, 3, 4] + b = ["1", "2", "a"] + c = [ms.Tensor([1]), ms.Tensor([2])] + d = [a, b, c, (4, 5)] + return d The above sample code, all ``List`` objects can be created normally. @@ -360,13 +416,20 @@ The basic usage scenarios of 'List' are as follows: .. code:: python import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) + - @ms.jit - def list_func(): - a = [1, 2, 3, 4] - return a + class Net(nn.Cell): + def construct(self): + a = [1, 2, 3, 4] + return a - output = list_func() # output: [1, 2, 3, 4] + net = Net() + output = net() # output: [1, 2, 3, 4] In the same way that a ``List`` is created within a graph mode, the graph mode returns a ``List`` object that can include any of the @@ -379,15 +442,22 @@ The basic usage scenarios of 'List' are as follows: .. code:: python import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) global_list = [1, 2, 3, 4] - @ms.jit - def list_func(): - global_list.reverse() - return global_list - output = list_func() # output: [4, 3, 2, 1] + class Net(nn.Cell): + def construct(self): + global_list.reverse() + return global_list + + net = Net() + output = net() # output: [4, 3, 2, 1] It should be noted that the list returned in the following pattern in the basic scenario is not the same object as the list of global @@ -404,14 +474,21 @@ The basic usage scenarios of 'List' are as follows: .. code:: python import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) list_input = [1, 2, 3, 4] - @ms.jit - def list_func(x): - return x - output = list_func(list_input) # output: [1, 2, 3, 4] + class Net(nn.Cell): + def construct(self, x): + return x + + net = Net() + output = net(list_input) # output: [1, 2, 3, 4] It should be noted that when 'List' is input as a static graph, it is always treated as a constant, regardless of the type of element @@ -439,15 +516,22 @@ The basic usage scenarios of 'List' are as follows: .. code:: python import mindspore as ms - @ms.jit() - def list_getitem_func(): - x = [[1, 2], 3, 4] - a = x[0] - b = x[0][ms.Tensor([1])] - c = x[1:3:1] - return a, b, c - - a, b, c = list_getitem_func() + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) + + class Net(nn.Cell): + def construct(self): + x = [[1, 2], 3, 4] + a = x[0] + b = x[0][ms.Tensor([1])] + c = x[1:3:1] + return a, b, c + + net = Net() + a, b, c = net() print('a:{}'.format(a)) print('b:{}'.format(b)) print('c:{}'.format(c)) @@ -486,17 +570,24 @@ The basic usage scenarios of 'List' are as follows: .. code:: python import mindspore as ms - - @ms.jit() - def test_setitem_func(): - x = [[0, 1], 2, 3, 4] - x[1] = 10 - x[2] = "ok" - x[3] = (1, 2, 3) - x[0][1] = 88 - return x - - output = test_setitem_func() + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) + + + class Net(nn.Cell): + def construct(self): + x = [[0, 1], 2, 3, 4] + x[1] = 10 + x[2] = "ok" + x[3] = (1, 2, 3) + x[0][1] = 88 + return x + + net = Net() + output = net() print('output:{}'.format(output)) The results are as follows: @@ -521,14 +612,21 @@ The basic usage scenarios of 'List' are as follows: .. code:: python import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @ms.jit() - def test_list(): - x = [1, 2, 3] - x.append(4) - return x + + class Net(nn.Cell): + def construct(self): + x = [1, 2, 3] + x.append(4) + return x - x = test_list() + net = Net() + x = net() print('x:{}'.format(x)) The results are as follows: @@ -553,14 +651,21 @@ The basic usage scenarios of 'List' are as follows: .. code:: python import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor - @ms.jit() - def test_list_clear(): - x = [1, 3, 4] - x.clear() - return x + context.set_context(mode=ms.GRAPH_MODE) - x = test_list_clear() + + class Net(nn.Cell): + def construct(self): + x = [1, 3, 4] + x.clear() + return x + + net = Net() + x = net() print('x:{}'.format(x)) The results are as follows: @@ -585,16 +690,22 @@ The basic usage scenarios of 'List' are as follows: .. code:: python import mindspore as ms - - @ms.jit() - def test_list_extend(): - x1 = [1, 2, 3] - x1.extend((4, "a")) - x2 = [1, 2, 3] - x2.extend(ms.Tensor([4, 5])) - return x1, x2 - - output1, output2 = test_list_extend() + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) + + class Net(nn.Cell): + def construct(self): + x1 = [1, 2, 3] + x1.extend((4, "a")) + x2 = [1, 2, 3] + x2.extend(ms.Tensor([4, 5])) + return x1, x2 + + net = Net() + output1, output2 = net() print('output1:{}'.format(output1)) print('output2:{}'.format(output2)) @@ -622,14 +733,21 @@ The basic usage scenarios of 'List' are as follows: .. code:: python import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @ms.jit() - def test_list_pop(): - x = [1, 2, 3] - b = x.pop() - return b, x + + class Net(nn.Cell): + def construct(self): + x = [1, 2, 3] + b = x.pop() + return b, x - pop_element, res_list = test_list_pop() + net = Net() + pop_element, res_list = net() print('pop_element:{}'.format(pop_element)) print('res_list:{}'.format(res_list)) @@ -652,14 +770,21 @@ The basic usage scenarios of 'List' are as follows: .. code:: python import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @ms.jit() - def test_list_reverse(): - x = [1, 2, 3] - x.reverse() - return x + + class Net(nn.Cell): + def construct(self): + x = [1, 2, 3] + x.reverse() + return x - output = test_list_reverse() + net = Net() + output = net() print('output:{}'.format(output)) The results are as follows: @@ -686,14 +811,21 @@ The basic usage scenarios of 'List' are as follows: .. code:: python import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor - @ms.jit() - def test_list_insert(): - x = [1, 2, 3] - x.insert(3, 4) - return x + context.set_context(mode=ms.GRAPH_MODE) - output = test_list_insert() + + class Net(nn.Cell): + def construct(self): + x = [1, 2, 3] + x.insert(3, 4) + return x + + net = Net() + output = net() print('output:{}'.format(output)) The results are as follows: @@ -738,19 +870,25 @@ supported. import numpy as np import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor - t = ms.Tensor(np.array([1, 2, 3])) + context.set_context(mode=ms.GRAPH_MODE) - @ms.jit() - def test_index(): - x = (1, (2, 3, 4), 3, 4, t) - y = x[1][1] - z = x[4] - m = x[1:4] - n = x[-4] - return y, z, m, n + t = ms.Tensor(np.array([1, 2, 3])) + + class Net(nn.Cell): + def construct(self): + x = (1, (2, 3, 4), 3, 4, t) + y = x[1][1] + z = x[4] + m = x[1:4] + n = x[-4] + return y, z, m, n - y, z, m, n = test_index() + net = Net() + y, z, m, n = net() print('y:{}'.format(y)) print('z:{}'.format(z)) print('m:{}'.format(m)) @@ -805,14 +943,21 @@ supported. .. code:: python import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @ms.jit() - def test_index(): - x = (1, 2, 3) - y = (4, 5, 6) - return x + y, x * 2 - out1, out2 = test_index() + class Net(nn.Cell): + def construct(self): + x = (1, 2, 3) + y = (4, 5, 6) + return x + y, x * 2 + + net = Net() + out1, out2 = net() print('out1:{}'.format(out1)) print('out2:{}'.format(out2)) @@ -878,24 +1023,31 @@ The ``value`` can be ``Number``, ``Tuple``, ``Tensor``, ``List`` or .. code:: python - import mindspore as ms import numpy as np + import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) x = {"a": ms.Tensor(np.array([1, 2, 3])), "b": ms.Tensor(np.array([4, 5, 6])), "c": ms.Tensor(np.array([7, 8, 9]))} - @ms.jit() - def test_dict(): - x_keys = x.keys() - x_values = x.values() - x_items = x.items() - value_a = x.get("a") - check_key = x.has_key("a") - y = {"a": ms.Tensor(np.array([0, 0, 0]))} - x.update(y) - new_dict = x.fromkeys("abcd", 123) - return x_keys, x_values, x_items, value_a, check_key, x, new_dict - - x_keys, x_values, x_items, value_a, check_key, new_x, new_dict = test_dict() + + class Net(nn.Cell): + def construct(self): + x_keys = x.keys() + x_values = x.values() + x_items = x.items() + value_a = x.get("a") + check_key = x.has_key("a") + y = {"a": ms.Tensor(np.array([0, 0, 0]))} + x.update(y) + new_dict = x.fromkeys("abcd", 123) + return x_keys, x_values, x_items, value_a, check_key, x, new_dict + + net = Net() + x_keys, x_values, x_items, value_a, check_key, new_x, new_dict = net() print('x_keys:{}'.format(x_keys)) print('x_values:{}'.format(x_values)) print('x_items:{}'.format(x_items)) @@ -943,7 +1095,6 @@ follows. def __init__(self): super(Net, self).__init__() - @ms.jit def construct(self, x): return ms.tensor(x.asnumpy(), dtype=ms.float32) @@ -1293,17 +1444,24 @@ compilation tensor_type[float32] diff --git a/docs/mindspore/source_zh_cn/model_train/program_form/static_graph.rst b/docs/mindspore/source_zh_cn/model_train/program_form/static_graph.rst index ada604d2e7885e4f7b07c59a8eee1aaf79e4480f..3d9e5a6746d7424f264cc7d8e0ff9ece19973f6e 100644 --- a/docs/mindspore/source_zh_cn/model_train/program_form/static_graph.rst +++ b/docs/mindspore/source_zh_cn/model_train/program_form/static_graph.rst @@ -65,15 +65,20 @@ API文档 `_\ 进行数据类型的转换。例如: .. code:: python + import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor - from mindspore import Tensor, jit + context.set_context(mode=ms.GRAPH_MODE) - @jit - def foo(x): - out1 = int(11.1) - out2 = int(Tensor([10])) - out3 = int(x.asnumpy()) - return out1, out2, out3 + class Net(nn.Cell): + def construct(self, x): + out1 = int(11.1) + out2 = int(Tensor([10])) + out3 = int(x.asnumpy()) + return out1, out2, out3 - res = foo(Tensor(2)) + net = Net() + res = net(Tensor(2)) print("res[0]:", res[0]) print("res[1]:", res[1]) print("res[2]:", res[2]) @@ -207,12 +244,19 @@ Number .. code:: python import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor - @ms.jit - def test_return_scalar(x, y): - return x + y + context.set_context(mode=ms.GRAPH_MODE) + + + class Net(nn.Cell): + def construct(self, x, y): + return x + y - res = test_return_scalar(ms.mutable(1), ms.mutable(2)) + net = Net() + res = net(ms.mutable(1), ms.mutable(2)) print(res) 运行结果如下: @@ -230,17 +274,23 @@ String .. code:: python - from mindspore import jit + import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @jit - def foo(): - var1 = 'Hello!' - var2 = "MindSpore" - var3 = str(123) - var4 = "{} is {}".format("string", var3) - return var1[0], var2[4:9], var1 + var2, var2 * 2, "H" in var1, "My name is %s!" % var2, var4 + class Net(nn.Cell): + def construct(self): + var1 = 'Hello!' + var2 = "MindSpore" + var3 = str(123) + var4 = "{} is {}".format("string", var3) + return var1[0], var2[4:9], var1 + var2, var2 * 2, "H" in var1, "My name is %s!" % var2, var4 - res = foo() + net = Net() + res = net() print("res:", res) 运行结果如下: @@ -264,14 +314,19 @@ List import numpy as np import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @ms.jit - def generate_list(): - a = [1, 2, 3, 4] - b = ["1", "2", "a"] - c = [ms.Tensor([1]), ms.Tensor([2])] - d = [a, b, c, (4, 5)] - return d + class Net(nn.Cell): + def construct(self): + a = [1, 2, 3, 4] + b = ["1", "2", "a"] + c = [ms.Tensor([1]), ms.Tensor([2])] + d = [a, b, c, (4, 5)] + return d 上述示例代码中,所有的\ ``List``\ 对象都可以被正常的创建。 @@ -281,15 +336,21 @@ List 对象时,\ ``List``\ 会被转换为\ ``Tuple``\ 。MindSpore2.0版本已经可以支持返回\ ``List``\ 对象。例如: .. code:: python - import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) + - @ms.jit - def list_func(): - a = [1, 2, 3, 4] - return a + class Net(nn.Cell): + def construct(self): + a = [1, 2, 3, 4] + return a - output = list_func() # output: [1, 2, 3, 4] + net = Net() + output = net() # output: [1, 2, 3, 4] 与图模式内创建\ ``List`` 相同,图模式返回\ ``List``\ 对象可以包括任意图模式支持的类型,也支持多层嵌套。 @@ -297,17 +358,23 @@ List - 图模式支持从全局变量中获取\ ``List``\ 对象。 .. code:: python - import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) global_list = [1, 2, 3, 4] - @ms.jit - def list_func(): - global_list.reverse() - return global_list - output = list_func() # output: [4, 3, 2, 1] + class Net(nn.Cell): + def construct(self): + global_list.reverse() + return global_list + + net = Net() + output = net() # output: [4, 3, 2, 1] 需要注意的是,在基础场景下图模式返回的列表与全局变量的列表不是同一个对象,当\ ``JIT_SYNTAX_LEVEL``\ 设置为\ ``LAX``\ 时,返回的对象与全局对象为统一对象。 @@ -316,16 +383,22 @@ List 图模式支持\ ``List``\ 作为静态图的输入,作为输入的\ ``List``\ 对象的元素必须为图模式支持的输入类型,也支持多层嵌套。 .. code:: python - import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) list_input = [1, 2, 3, 4] - @ms.jit - def list_func(x): - return x - output = list_func(list_input) # output: [1, 2, 3, 4] + class Net(nn.Cell): + def construct(self, x): + return x + + net = Net() + output = net(list_input) # output: [1, 2, 3, 4] 需要注意的是,\ ``List``\ 作为静态图输入时,无论其内部的元素是什么类型,一律被视为常量。 @@ -344,17 +417,23 @@ List 示例如下: .. code:: python - import mindspore as ms - @ms.jit() - def list_getitem_func(): - x = [[1, 2], 3, 4] - a = x[0] - b = x[0][ms.Tensor([1])] - c = x[1:3:1] - return a, b, c - - a, b, c = list_getitem_func() + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) + + class Net(nn.Cell): + def construct(self): + x = [[1, 2], 3, 4] + a = x[0] + b = x[0][ms.Tensor([1])] + c = x[1:3:1] + return a, b, c + + net = Net() + a, b, c = net() print('a:{}'.format(a)) print('b:{}'.format(b)) print('c:{}'.format(c)) @@ -387,17 +466,24 @@ List .. code:: python import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor - @ms.jit() - def test_setitem_func(): - x = [[0, 1], 2, 3, 4] - x[1] = 10 - x[2] = "ok" - x[3] = (1, 2, 3) - x[0][1] = 88 - return x + context.set_context(mode=ms.GRAPH_MODE) - output = test_setitem_func() + + class Net(nn.Cell): + def construct(self): + x = [[0, 1], 2, 3, 4] + x[1] = 10 + x[2] = "ok" + x[3] = (1, 2, 3) + x[0][1] = 88 + return x + + net = Net() + output = net() print('output:{}'.format(output)) 运行结果如下: @@ -418,16 +504,22 @@ List 示例如下: .. code:: python - import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @ms.jit() - def test_list(): - x = [1, 2, 3] - x.append(4) - return x + + class Net(nn.Cell): + def construct(self): + x = [1, 2, 3] + x.append(4) + return x - x = test_list() + net = Net() + x = net() print('x:{}'.format(x)) 运行结果如下: @@ -448,16 +540,22 @@ List 示例如下: .. code:: python - import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @ms.jit() - def test_list_clear(): - x = [1, 3, 4] - x.clear() - return x + + class Net(nn.Cell): + def construct(self): + x = [1, 3, 4] + x.clear() + return x - x = test_list_clear() + net = Net() + x = net() print('x:{}'.format(x)) 运行结果如下: @@ -477,18 +575,23 @@ List 示例如下: .. code:: python - import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor - @ms.jit() - def test_list_extend(): - x1 = [1, 2, 3] - x1.extend((4, "a")) - x2 = [1, 2, 3] - x2.extend(ms.Tensor([4, 5])) - return x1, x2 + context.set_context(mode=ms.GRAPH_MODE) - output1, output2 = test_list_extend() + class Net(nn.Cell): + def construct(self): + x1 = [1, 2, 3] + x1.extend((4, "a")) + x2 = [1, 2, 3] + x2.extend(ms.Tensor([4, 5])) + return x1, x2 + + net = Net() + output1, output2 = net() print('output1:{}'.format(output1)) print('output2:{}'.format(output2)) @@ -512,14 +615,20 @@ List .. code:: python import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor - @ms.jit() - def test_list_pop(): - x = [1, 2, 3] - b = x.pop() - return b, x + context.set_context(mode=ms.GRAPH_MODE) + + class Net(nn.Cell): + def construct(self): + x = [1, 2, 3] + b = x.pop() + return b, x - pop_element, res_list = test_list_pop() + net = Net() + pop_element, res_list = net() print('pop_element:{}'.format(pop_element)) print('res_list:{}'.format(res_list)) @@ -539,16 +648,22 @@ List 示例如下: .. code:: python - import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @ms.jit() - def test_list_reverse(): - x = [1, 2, 3] - x.reverse() - return x + + class Net(nn.Cell): + def construct(self): + x = [1, 2, 3] + x.reverse() + return x - output = test_list_reverse() + net = Net() + output = net() print('output:{}'.format(output)) 运行结果如下: @@ -570,14 +685,21 @@ List .. code:: python import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor - @ms.jit() - def test_list_insert(): - x = [1, 2, 3] - x.insert(3, 4) - return x + context.set_context(mode=ms.GRAPH_MODE) - output = test_list_insert() + + class Net(nn.Cell): + def construct(self): + x = [1, 2, 3] + x.insert(3, 4) + return x + + net = Net() + output = net() print('output:{}'.format(output)) 运行结果如下: @@ -609,11 +731,17 @@ Tuple import numpy as np import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) t = ms.Tensor(np.array([1, 2, 3])) - @ms.jit() - def test_index(): + + class Net(nn.Cell): + def construct(self): x = (1, (2, 3, 4), 3, 4, t) y = x[1][1] z = x[4] @@ -621,7 +749,8 @@ Tuple n = x[-4] return y, z, m, n - y, z, m, n = test_index() + net = Net() + y, z, m, n = net() print('y:{}'.format(y)) print('z:{}'.format(z)) print('m:{}'.format(m)) @@ -641,9 +770,11 @@ Tuple .. code:: python import mindspore as ms - from mindspore import nn, set_context + from mindspore import nn + from mindspore import context + from mindspore import Tensor - set_context(mode=ms.GRAPH_MODE) + context.set_context(mode=ms.GRAPH_MODE) class Net(nn.Cell): def __init__(self): @@ -673,16 +804,22 @@ Tuple 与字符串\ ``String``\ 类似,元组支持使用\ ``+``\ 和\ ``*``\ 进行组合,得到一个新的元组\ ``Tuple``\ ,例如: .. code:: python - import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) - @ms.jit() - def test_index(): - x = (1, 2, 3) - y = (4, 5, 6) - return x + y, x * 2 - out1, out2 = test_index() + class Net(nn.Cell): + def construct(self): + x = (1, 2, 3) + y = (4, 5, 6) + return x + y, x * 2 + + net = Net() + out1, out2 = net() print('out1:{}'.format(out1)) print('out2:{}'.format(out2)) @@ -722,24 +859,31 @@ Dictionary .. code:: python - import mindspore as ms import numpy as np + import mindspore as ms + from mindspore import nn + from mindspore import context + from mindspore import Tensor + + context.set_context(mode=ms.GRAPH_MODE) x = {"a": ms.Tensor(np.array([1, 2, 3])), "b": ms.Tensor(np.array([4, 5, 6])), "c": ms.Tensor(np.array([7, 8, 9]))} - @ms.jit() - def test_dict(): - x_keys = x.keys() - x_values = x.values() - x_items = x.items() - value_a = x.get("a") - check_key = x.has_key("a") - y = {"a": ms.Tensor(np.array([0, 0, 0]))} - x.update(y) - new_dict = x.fromkeys("abcd", 123) - return x_keys, x_values, x_items, value_a, check_key, x, new_dict - - x_keys, x_values, x_items, value_a, check_key, new_x, new_dict = test_dict() + + class Net(nn.Cell): + def construct(self): + x_keys = x.keys() + x_values = x.values() + x_items = x.items() + value_a = x.get("a") + check_key = x.has_key("a") + y = {"a": ms.Tensor(np.array([0, 0, 0]))} + x.update(y) + new_dict = x.fromkeys("abcd", 123) + return x_keys, x_values, x_items, value_a, check_key, x, new_dict + + net = Net() + x_keys, x_values, x_items, value_a, check_key, new_x, new_dict = net() print('x_keys:{}'.format(x_keys)) print('x_values:{}'.format(x_values)) print('x_items:{}'.format(x_items)) @@ -782,7 +926,6 @@ API文档 tensor_type[float32] diff --git a/docs/mindspore/source_zh_cn/model_train/program_form/static_graph_syntax/static_graph_syntax_support.ipynb b/docs/mindspore/source_zh_cn/model_train/program_form/static_graph_syntax/static_graph_syntax_support.ipynb index debc6658fd39b80b515f133499300e3498348001..9e7b076fd0c1a45a5e20d0b6bc17ba2c4ddf8eb4 100644 --- a/docs/mindspore/source_zh_cn/model_train/program_form/static_graph_syntax/static_graph_syntax_support.ipynb +++ b/docs/mindspore/source_zh_cn/model_train/program_form/static_graph_syntax/static_graph_syntax_support.ipynb @@ -54,7 +54,7 @@ "import mindspore as ms\n", "from mindspore import nn\n", "from mindspore import context\n", - "from mindspore import Tensor, jit\n", + "from mindspore import Tensor\n", "\n", "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", @@ -84,14 +84,19 @@ "metadata": {}, "outputs": [], "source": [ - "from mindspore import jit, Tensor\n", + "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", "\n", - "@jit\n", - "def foo():\n", - " a = 1\n", - " b = \"2\"\n", - " c = Tensor([1, 2, 3])\n", - " return a, b, c" + "context.set_context(mode=ms.GRAPH_MODE)\n", + "\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " a = 1\n", + " b = \"2\"\n", + " c = Tensor([1, 2, 3])\n", + " return a, b, c" ] }, { @@ -111,14 +116,19 @@ "metadata": {}, "outputs": [], "source": [ - "from mindspore import jit, Tensor\n", + "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "@jit\n", - "def foo():\n", - " a = Tensor([1, 2, 3])\n", - " b = Tensor([1, 1, 1])\n", - " c = a + b\n", - " return c" + "class Net(nn.Cell):\n", + " def construct(self):\n", + " a = Tensor([1, 2, 3])\n", + " b = Tensor([1, 1, 1])\n", + " c = a + b\n", + " return c" ] }, { @@ -140,16 +150,22 @@ "metadata": {}, "outputs": [], "source": [ - "from mindspore import Tensor, jit\n", "from mindspore import mutable\n", + "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", "a = mutable([Tensor([1]), Tensor([2])])\n", "\n", - "@jit\n", - "def foo(a):\n", - " b = mutable(Tensor([3]))\n", - " c = mutable((Tensor([1]), Tensor([2])))\n", - " return a, b, c" + "\n", + "class Net(nn.Cell):\n", + " def construct(self, a):\n", + " b = mutable(Tensor([3]))\n", + " c = mutable((Tensor([1]), Tensor([2])))\n", + " return a, b, c" ] }, { @@ -169,14 +185,20 @@ "metadata": {}, "outputs": [], "source": [ - "from mindspore import Tensor, jit\n", + "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", "a = Tensor([1])\n", "b = (Tensor([1]), Tensor([2]))\n", "\n", - "@jit\n", - "def foo(a, b):\n", - " return a, b" + "\n", + "class Net(nn.Cell):\n", + " def construct(self, a, b):\n", + " return a, b" ] }, { @@ -198,15 +220,20 @@ "metadata": {}, "outputs": [], "source": [ - "from mindspore import Tensor, jit, ops\n", + "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", "a = Tensor([1])\n", "b = Tensor([2])\n", "\n", - "@jit\n", - "def foo(a, b):\n", - " c = a + b\n", - " return c" + "class Net(nn.Cell):\n", + " def construct(self, a, b):\n", + " c = a + b\n", + " return c" ] }, { @@ -249,16 +276,22 @@ } ], "source": [ - "from mindspore import Tensor, jit\n", + "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", "\n", - "@jit\n", - "def foo(x):\n", - " out1 = int(11.1)\n", - " out2 = int(Tensor([10]))\n", - " out3 = int(x.asnumpy())\n", - " return out1, out2, out3\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", + "\n", + "class Net(nn.Cell):\n", + " def construct(self, x):\n", + " out1 = int(11.1)\n", + " out2 = int(Tensor([10]))\n", + " out3 = int(x.asnumpy())\n", + " return out1, out2, out3\n", "\n", - "res = foo(Tensor(2))\n", + "net = Net()\n", + "res = net(Tensor(2))\n", "print(\"res[0]:\", res[0])\n", "print(\"res[1]:\", res[1])\n", "print(\"res[2]:\", res[2])" @@ -288,12 +321,19 @@ ], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", "\n", - "@ms.jit\n", - "def test_return_scalar(x, y):\n", - " return x + y\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", + "\n", + "\n", + "class Net(nn.Cell):\n", + " def construct(self, x, y):\n", + " return x + y\n", "\n", - "res = test_return_scalar(ms.mutable(1), ms.mutable(2))\n", + "net = Net()\n", + "res = net(ms.mutable(1), ms.mutable(2))\n", "print(res)" ] }, @@ -324,17 +364,22 @@ } ], "source": [ - "from mindspore import jit\n", + "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "@jit\n", - "def foo():\n", - " var1 = 'Hello!'\n", - " var2 = \"MindSpore\"\n", - " var3 = str(123)\n", - " var4 = \"{} is {}\".format(\"string\", var3)\n", - " return var1[0], var2[4:9], var1 + var2, var2 * 2, \"H\" in var1, \"My name is %s!\" % var2, var4\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " var1 = 'Hello!'\n", + " var2 = \"MindSpore\"\n", + " var3 = str(123)\n", + " var4 = \"{} is {}\".format(\"string\", var3)\n", + " return var1[0], var2[4:9], var1 + var2, var2 * 2, \"H\" in var1, \"My name is %s!\" % var2, var4\n", "\n", - "res = foo()\n", + "net = Net()\n", + "res = net()\n", "print(\"res:\", res)" ] }, @@ -363,14 +408,19 @@ "source": [ "import numpy as np\n", "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", "\n", - "@ms.jit\n", - "def generate_list():\n", - " a = [1, 2, 3, 4]\n", - " b = [\"1\", \"2\", \"a\"]\n", - " c = [ms.Tensor([1]), ms.Tensor([2])]\n", - " d = [a, b, c, (4, 5)]\n", - " return d" + "context.set_context(mode=ms.GRAPH_MODE)\n", + "\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " a = [1, 2, 3, 4]\n", + " b = [\"1\", \"2\", \"a\"]\n", + " c = [ms.Tensor([1]), ms.Tensor([2])]\n", + " d = [a, b, c, (4, 5)]\n", + " return d" ] }, { @@ -393,13 +443,20 @@ "outputs": [], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", "\n", - "@ms.jit\n", - "def list_func():\n", - " a = [1, 2, 3, 4]\n", - " return a\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", + "\n", + "\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " a = [1, 2, 3, 4]\n", + " return a\n", "\n", - "output = list_func() # output: [1, 2, 3, 4]" + "net = Net()\n", + "output = net() # output: [1, 2, 3, 4]" ] }, { @@ -420,15 +477,22 @@ "outputs": [], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", "global_list = [1, 2, 3, 4]\n", "\n", - "@ms.jit\n", - "def list_func():\n", - " global_list.reverse()\n", - " return global_list\n", "\n", - "output = list_func() # output: [4, 3, 2, 1]" + "class Net(nn.Cell):\n", + " def construct(self):\n", + " global_list.reverse()\n", + " return global_list\n", + "\n", + "net = Net()\n", + "output = net() # output: [4, 3, 2, 1]" ] }, { @@ -451,14 +515,21 @@ "outputs": [], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", "list_input = [1, 2, 3, 4]\n", "\n", - "@ms.jit\n", - "def list_func(x):\n", - " return x\n", "\n", - "output = list_func(list_input) # output: [1, 2, 3, 4]" + "class Net(nn.Cell):\n", + " def construct(self, x):\n", + " return x\n", + "\n", + "net = Net()\n", + "output = net(list_input) # output: [1, 2, 3, 4]" ] }, { @@ -500,16 +571,22 @@ } ], "source": [ - "import mindspore as ms\n", - "@ms.jit()\n", - "def list_getitem_func():\n", - " x = [[1, 2], 3, 4]\n", - " a = x[0]\n", - " b = x[0][ms.Tensor([1])]\n", - " c = x[1:3:1]\n", - " return a, b, c\n", - "\n", - "a, b, c = list_getitem_func()\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", + "\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x = [[1, 2], 3, 4]\n", + " a = x[0]\n", + " b = x[0][ms.Tensor([1])]\n", + " c = x[1:3:1]\n", + " return a, b, c\n", + "\n", + "net = Net()\n", + "a, b, c = net()\n", "print('a:{}'.format(a))\n", "print('b:{}'.format(b))\n", "print('c:{}'.format(c))" @@ -551,17 +628,23 @@ ], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", "\n", - "@ms.jit()\n", - "def test_setitem_func():\n", - " x = [[0, 1], 2, 3, 4]\n", - " x[1] = 10\n", - " x[2] = \"ok\"\n", - " x[3] = (1, 2, 3)\n", - " x[0][1] = 88\n", - " return x\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", + "\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x = [[0, 1], 2, 3, 4]\n", + " x[1] = 10\n", + " x[2] = \"ok\"\n", + " x[3] = (1, 2, 3)\n", + " x[0][1] = 88\n", + " return x\n", "\n", - "output = test_setitem_func()\n", + "net = Net()\n", + "output = net()\n", "print('output:{}'.format(output))" ] }, @@ -597,14 +680,19 @@ ], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "@ms.jit()\n", - "def test_list():\n", - " x = [1, 2, 3]\n", - " x.append(4)\n", - " return x\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x = [1, 2, 3]\n", + " x.append(4)\n", + " return x\n", "\n", - "x = test_list()\n", + "net = Net()\n", + "x = net()\n", "print('x:{}'.format(x))" ] }, @@ -640,14 +728,19 @@ ], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", "\n", - "@ms.jit()\n", - "def test_list_clear():\n", - " x = [1, 3, 4]\n", - " x.clear()\n", - " return x\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "x = test_list_clear()\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x = [1, 3, 4]\n", + " x.clear()\n", + " return x\n", + "\n", + "net = Net()\n", + "x = net()\n", "print('x:{}'.format(x))" ] }, @@ -684,16 +777,21 @@ ], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "@ms.jit()\n", - "def test_list_extend():\n", - " x1 = [1, 2, 3]\n", - " x1.extend((4, \"a\"))\n", - " x2 = [1, 2, 3]\n", - " x2.extend(ms.Tensor([4, 5]))\n", - " return x1, x2\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x1 = [1, 2, 3]\n", + " x1.extend((4, \"a\"))\n", + " x2 = [1, 2, 3]\n", + " x2.extend(ms.Tensor([4, 5]))\n", + " return x1, x2\n", "\n", - "output1, output2 = test_list_extend()\n", + "net = Net()\n", + "output1, output2 = net()\n", "print('output1:{}'.format(output1))\n", "print('output2:{}'.format(output2))" ] @@ -729,14 +827,20 @@ ], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", "\n", - "@ms.jit()\n", - "def test_list_pop():\n", - " x = [1, 2, 3]\n", - " b = x.pop()\n", - " return b, x\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "pop_element, res_list = test_list_pop()\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x = [1, 2, 3]\n", + " b = x.pop()\n", + " return b, x\n", + "\n", + "net = Net()\n", + "pop_element, res_list = net()\n", "print('pop_element:{}'.format(pop_element))\n", "print('res_list:{}'.format(res_list))" ] @@ -770,15 +874,19 @@ } ], "source": [ - "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "@ms.jit()\n", - "def test_list_reverse():\n", - " x = [1, 2, 3]\n", - " x.reverse()\n", - " return x\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x = [1, 2, 3]\n", + " x.reverse()\n", + " return x\n", "\n", - "output = test_list_reverse()\n", + "net = Net()\n", + "output = net()\n", "print('output:{}'.format(output))" ] }, @@ -814,14 +922,19 @@ ], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "@ms.jit()\n", - "def test_list_insert():\n", - " x = [1, 2, 3]\n", - " x.insert(3, 4)\n", - " return x\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x = [1, 2, 3]\n", + " x.insert(3, 4)\n", + " return x\n", "\n", - "output = test_list_insert()\n", + "net = Net()\n", + "output = net()\n", "print('output:{}'.format(output))" ] }, @@ -875,19 +988,24 @@ "source": [ "import numpy as np\n", "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", "t = ms.Tensor(np.array([1, 2, 3]))\n", "\n", - "@ms.jit()\n", - "def test_index():\n", - " x = (1, (2, 3, 4), 3, 4, t)\n", - " y = x[1][1]\n", - " z = x[4]\n", - " m = x[1:4]\n", - " n = x[-4]\n", - " return y, z, m, n\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x = (1, (2, 3, 4), 3, 4, t)\n", + " y = x[1][1]\n", + " z = x[4]\n", + " m = x[1:4]\n", + " n = x[-4]\n", + " return y, z, m, n\n", "\n", - "y, z, m, n = test_index()\n", + "net = Net()\n", + "y, z, m, n = net()\n", "print('y:{}'.format(y))\n", "print('z:{}'.format(z))\n", "print('m:{}'.format(m))\n", @@ -967,14 +1085,19 @@ ], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "@ms.jit()\n", - "def test_index():\n", - " x = (1, 2, 3)\n", - " y = (4, 5, 6)\n", - " return x + y, x * 2\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x = (1, 2, 3)\n", + " y = (4, 5, 6)\n", + " return x + y, x * 2\n", "\n", - "out1, out2 = test_index()\n", + "net = Net()\n", + "out1, out2 = net()\n", "print('out1:{}'.format(out1))\n", "print('out2:{}'.format(out2))" ] @@ -1032,24 +1155,30 @@ } ], "source": [ - "import mindspore as ms\n", "import numpy as np\n", + "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", "x = {\"a\": ms.Tensor(np.array([1, 2, 3])), \"b\": ms.Tensor(np.array([4, 5, 6])), \"c\": ms.Tensor(np.array([7, 8, 9]))}\n", "\n", - "@ms.jit()\n", - "def test_dict():\n", - " x_keys = x.keys()\n", - " x_values = x.values()\n", - " x_items = x.items()\n", - " value_a = x.get(\"a\")\n", - " check_key = x.has_key(\"a\")\n", - " y = {\"a\": ms.Tensor(np.array([0, 0, 0]))}\n", - " x.update(y)\n", - " new_dict = x.fromkeys(\"abcd\", 123)\n", - " return x_keys, x_values, x_items, value_a, check_key, x, new_dict\n", - "\n", - "x_keys, x_values, x_items, value_a, check_key, new_x, new_dict = test_dict()\n", + "\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x_keys = x.keys()\n", + " x_values = x.values()\n", + " x_items = x.items()\n", + " value_a = x.get(\"a\")\n", + " check_key = x.has_key(\"a\")\n", + " y = {\"a\": ms.Tensor(np.array([0, 0, 0]))}\n", + " x.update(y)\n", + " new_dict = x.fromkeys(\"abcd\", 123)\n", + " return x_keys, x_values, x_items, value_a, check_key, x, new_dict\n", + "\n", + "net = Net()\n", + "x_keys, x_values, x_items, value_a, check_key, new_x, new_dict = net()\n", "print('x_keys:{}'.format(x_keys))\n", "print('x_values:{}'.format(x_values))\n", "print('x_items:{}'.format(x_items))\n", @@ -1097,7 +1226,6 @@ " def __init__(self):\n", " super(Net, self).__init__()\n", "\n", - " @ms.jit\n", " def construct(self, x):\n", " return ms.tensor(x.asnumpy(), dtype=ms.float32)\n", "\n", @@ -1554,15 +1682,21 @@ "source": [ "import numpy as np\n", "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "@ms.jit\n", - "def func():\n", - " a = np.array([1, 2, 3])\n", - " b = np.array([4, 5, 6])\n", - " out = a + b\n", - " return out\n", "\n", - "print(func())" + "class Net(nn.Cell):\n", + " def construct(self):\n", + " a = np.array([1, 2, 3])\n", + " b = np.array([4, 5, 6])\n", + " out = a + b\n", + " return out\n", + "\n", + "net = Net()\n", + "print(net())" ] }, { @@ -1592,13 +1726,19 @@ "source": [ "from scipy import linalg\n", "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "@ms.jit\n", - "def func():\n", - " x = [[1, 2], [3, 4]]\n", - " return linalg.qr(x)\n", "\n", - "out = func()\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x = [[1, 2], [3, 4]]\n", + " return linalg.qr(x)\n", + "\n", + "net = Net()\n", + "out = net()\n", "print(out[0].shape)" ] }, @@ -1629,14 +1769,19 @@ "source": [ "import numpy as np\n", "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", "\n", - "@ms.jit\n", - "def func():\n", - " x = np.array([1, 2, 3])\n", - " out = ms.Tensor(x) + 1\n", - " return out\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", + "\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x = np.array([1, 2, 3])\n", + " out = ms.Tensor(x) + 1\n", + " return out\n", "\n", - "print(func())" + "net = Net()\n", + "print(net())" ] }, { @@ -1837,15 +1982,22 @@ "outputs": [], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", "global_list = [1, 2, 3, 4]\n", "\n", - "@ms.jit\n", - "def list_func():\n", - " global_list.reverse()\n", - " return global_list\n", "\n", - "output = list_func() # output: [4, 3, 2, 1]\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " global_list.reverse()\n", + " return global_list\n", + "\n", + "net = Net()\n", + "output = net() # output: [4, 3, 2, 1]\n", "assert id(global_list) == id(output)" ] }, @@ -1891,15 +2043,21 @@ "outputs": [], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", "list_input = [1, 2, 3, 4]\n", "\n", - "@ms.jit\n", - "def list_func():\n", - " list_input.reverse()\n", - " return list_input\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " list_input.reverse()\n", + " return list_input\n", "\n", - "output = list_func() # output: [4, 3, 2, 1] list_input: [4, 3, 2, 1]\n", + "net = Net()\n", + "output = net() # output: [4, 3, 2, 1] list_input: [4, 3, 2, 1]\n", "assert id(output) == id(list_input)" ] }, @@ -1931,15 +2089,21 @@ ], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "@ms.jit()\n", - "def test_dict():\n", - " x = {'a': 'a', 'b': 'b'}\n", - " y = x.get('a')\n", - " z = dict(y=y)\n", - " return z\n", "\n", - "out = test_dict()\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x = {'a': 'a', 'b': 'b'}\n", + " y = x.get('a')\n", + " z = dict(y=y)\n", + " return z\n", + "\n", + "net = Net()\n", + "out = net()\n", "print(\"out:\", out)" ] }, @@ -1969,18 +2133,25 @@ } ], "source": [ - "import mindspore as ms\n", "import numpy as np\n", + "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", "x = {\"a\": ms.Tensor(np.array([1, 2, 3])), \"b\": ms.Tensor(np.array([4, 5, 6])), \"c\": ms.Tensor(np.array([7, 8, 9]))}\n", "\n", - "@ms.jit()\n", - "def test_dict():\n", - " y = x[\"b\"]\n", - " x[\"a\"] = (2, 3, 4)\n", - " return x, y\n", "\n", - "out1, out2 = test_dict()\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " y = x[\"b\"]\n", + " x[\"a\"] = (2, 3, 4)\n", + " return x, y\n", + "\n", + "net = Net()\n", + "out1, out2 = net()\n", "print('out1:{}'.format(out1))\n", "print('out2:{}'.format(out2))" ] @@ -2013,12 +2184,17 @@ ], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", "\n", - "@ms.jit\n", - "def test_return_none():\n", - " return 1, \"a\", None\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "res = test_return_none()\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " return 1, \"a\", None\n", + "\n", + "net = Net()\n", + "res = net()\n", "print(res)" ] }, @@ -2047,13 +2223,18 @@ ], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", "\n", - "@ms.jit\n", - "def foo():\n", - " x = 3\n", - " print(\"x:\", x)\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "res = foo()\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x = 3\n", + " print(\"x:\", x)\n", + "\n", + "net = Net()\n", + "res = net()\n", "assert res is None" ] }, @@ -2083,18 +2264,23 @@ ], "source": [ "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "@ms.jit\n", - "def foo(x, y=None):\n", - " if y is not None:\n", - " print(\"y:\", y)\n", - " else:\n", - " print(\"y is None\")\n", - " print(\"x:\", x)\n", - " return y\n", + "class Net(nn.Cell):\n", + " def construct(self, x, y=None):\n", + " if y is not None:\n", + " print(\"y:\", y)\n", + " else:\n", + " print(\"y is None\")\n", + " print(\"x:\", x)\n", + " return y\n", "\n", "x = [1, 2]\n", - "res = foo(x)\n", + "net = Net()\n", + "res = net(x)\n", "assert res is None" ] }, @@ -2160,15 +2346,21 @@ "source": [ "import numpy as np\n", "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "@ms.jit\n", - "def func():\n", - " x = np.array(1)\n", - " if x <= 1:\n", - " x += 1\n", - " return ms.Tensor(x)\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " x = np.array(1)\n", + " if x <= 1:\n", + " x += 1\n", + " return ms.Tensor(x)\n", "\n", - "res = func()\n", + "net = Net()\n", + "res = net()\n", "print(\"res: \", res)" ] }, @@ -2201,7 +2393,13 @@ } ], "source": [ - "from mindspore import jit\n", + "\n", + "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", + "from mindspore import Tensor\n", + "\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", "class AssignClass():\n", " def __init__(self):\n", @@ -2209,11 +2407,12 @@ "\n", "obj = AssignClass()\n", "\n", - "@jit\n", - "def foo():\n", - " obj.x = 100\n", + "class Net(nn.Cell):\n", + " def construct(self):\n", + " obj.x = 100\n", "\n", - "foo()\n", + "net = Net()\n", + "net()\n", "print(f\"obj.x is: {obj.x}\")" ] }, @@ -2241,18 +2440,22 @@ } ], "source": [ - "from mindspore import jit\n", "import numpy as np\n", + "import mindspore as ms\n", + "from mindspore import nn\n", + "from mindspore import context\n", "\n", - "@jit\n", - "def foo():\n", - " a = np.array([1, 2, 3, 4])\n", - " a.shape = (2, 2)\n", - " return a.shape\n", + "context.set_context(mode=ms.GRAPH_MODE)\n", "\n", - "shape = foo()\n", - "print(f\"shape is {shape}\")\n", - "\n" + "class Net(nn.Cell):\n", + " def construct(self):\n", + " a = np.array([1, 2, 3, 4])\n", + " a.shape = (2, 2)\n", + " return a.shape\n", + "\n", + "net = Net()\n", + "shape = net()\n", + "print(f\"shape is {shape}\")" ] }, { @@ -2407,15 +2610,16 @@ "outputs": [], "source": [ "import mindspore as ms\n", - "from mindspore import ops, set_context\n", + "from mindspore import ops, set_context, nn\n", "set_context(mode=ms.GRAPH_MODE)\n", "\n", - "@ms.jit\n", - "def dict_net(a):\n", - " x = {'a': a, 'b': 2}\n", - " return a, (x, (1, 2))\n", + "class Net(nn.Cell):\n", + " def construct(self, a):\n", + " x = {'a': a, 'b': 2}\n", + " return a, (x, (1, 2))\n", "\n", - "out = ops.grad(dict_net)(ms.Tensor([1]))\n", + "net = Net()\n", + "out = ops.grad(net)(ms.Tensor([1]))\n", "assert out == 2" ] }, @@ -2460,7 +2664,6 @@ " super(Net, self).__init__()\n", " self.abs = ops.Abs()\n", "\n", - " @ms.jit\n", " def construct(self, x, y):\n", " y1 = ms.tensor(x.asnumpy() + y.asnumpy(), dtype=ms.float32)\n", " y2 = ms.Tensor(x.asnumpy() + y.asnumpy(), dtype=ms.float32) # @jit.typing: () -> tensor_type[float32]\n", @@ -2524,4 +2727,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +}