https://www.mindspore.cn/docs/zh-CN/r1.8/note/api_mapping/pytorch_api_mapping.html
https://www.mindspore.cn/install#installTip
https://www.mindspore.cn/tutorials/zh-CN/r1.9/beginner/train.html
通过设置model.set_train()训练,model.set_train(False)进行测试验证
直接参照官方文档例子模仿
碰到不能搜到的点右上角搜索
1.construct注意是小写
2.MindSpore not support to get attribute ‘mul’ of a type[Tensor[Float32]]
用ops.Mul(a,b)
3.不能引入einops
Unsupported statement ‘Try’.
自己模拟一下内部的功能,写个函数调用
4.对2维扩展到4维
即(2,1)->(2,1,252,52)
from mindspore.ops.function import broadcast_to
from mindspore import ops
import mindspore
import numpy as np
x = np.random.randn(2, 1)
x = mindspore.Tensor(x, mindspore.float32)
output = ops.expand_dims(x, 2)
output = ops.expand_dims(x, 3)
此时输出
output
Tensor(shape=[2, 1, 1, 1], dtype=Float32, value=
[[[[-1.20102668e+00]]],
[[[-1.35897219e+00]]]])
shape=(2,1,252,252)
output = broadcast_to(output, shape)
此时输出
output
Tensor(shape=[2, 1, 252, 252], dtype=Float32, value=
[[[[-1.20102668e+00, -1.20102668e+00, -1.20102668e+00 … -1.20102668e+00, -1.20102668e+00, -1.20102668e+00],
[-1.20102668e+00, -1.20102668e+00, -1.20102668e+00 … -1.20102668e+00, -1.20102668e+00, -1.20102668e+00],
[-1.20102668e+00, -1.20102668e+00, -1.20102668e+00 … -1.20102668e+00, -1.20102668e+00, -1.20102668e+00],
…
[-1.20102668e+00, -1.20102668e+00, -1.20102668e+00 … -1.20102668e+00, -1.20102668e+00, -1.20102668e+00],
[-1.20102668e+00, -1.20102668e+00, -1.20102668e+00 … -1.20102668e+00, -1.20102668e+00, -1.20102668e+00],
[-1.20102668e+00, -1.20102668e+00, -1.20102668e+00 … -1.20102668e+00, -1.20102668e+00, -1.20102668e+00]]],
[[[-1.35897219e+00, -1.35897219e+00, -1.35897219e+00 … -1.35897219e+00, -1.35897219e+00, -1.35897219e+00],
[-1.35897219e+00, -1.35897219e+00, -1.35897219e+00 … -1.35897219e+00, -1.35897219e+00, -1.35897219e+00],
[-1.35897219e+00, -1.35897219e+00, -1.35897219e+00 … -1.35897219e+00, -1.35897219e+00, -1.35897219e+00],
…
[-1.35897219e+00, -1.35897219e+00, -1.35897219e+00 … -1.35897219e+00, -1.35897219e+00, -1.35897219e+00],
[-1.35897219e+00, -1.35897219e+00, -1.35897219e+00 … -1.35897219e+00, -1.35897219e+00, -1.35897219e+00],
[-1.35897219e+00, -1.35897219e+00, -1.35897219e+00 … -1.35897219e+00, -1.35897219e+00, -1.35897219e+00]]]])
5.不能用a.size()[-1]
用a.shape[0]这种
6.TypeError: too many positional arguments
见到这种,把操作符放出来,重命名为一个函数,再对操作数进行操作
add = ops.Add()
a_b=add(a, b)
7.有的很奇怪
For primitive[AdaptiveMaxPool2DGrad], the input argument[argmax_dtype] must be a type of {Tensor[Int64]}, but got Tensor[Float64].
但是输入又只能是Float
8 BatchNorm2d要改momentum为0.1
9 设置has_bias=True