|
- import torch
- import mindspore
- import mindspore.ops as ops
- from mindspore import Tensor, context
- import numpy as np
- import mindspore.numpy as mp
- from mindspore.common.initializer import initializer, XavierUniform
- import torch.nn as nn
- import mindspore.nn as mm
- import torch.nn.functional as F
- #from svoice.models.swave import SWave
- #
- # b = Tensor(np.random.randn(4,4), dtype=mindspore.float32)
- # a = torch.randn(4, 4)
- # print(a)
- # print(torch.mean(a))
- # print(torch.mean(a, 0))
- #
- # mean = ops.ReduceMean()
- # print(mean(b))
- # a = torch.randn(4, 1)
- # b = torch.randn(1, 4)
- # print(a)
- # print(b)
- # print(torch.mul(a,b))
- #
- # c = Tensor(np.random.randn(4,1), dtype=mindspore.float32)
- # d = Tensor(np.random.randn(1,4), dtype=mindspore.float32)
- # print(c)
- # print(d)
- # mul = ops.Mul()
- # print(mul(c,100))
- # x = torch.randn(3, 3,3)
- # print(x)
- # print(torch.cat([x,x],2))
- # c = Tensor(np.random.randn(3,3,3), dtype=mindspore.float32)
- # print(c)
- # op = ops.Concat(2)
- # print(op([c,c]))
-
-
- # print(torch.zeros(3,3,3))
- # c = Tensor(np.random.randn(3,3,3), dtype=mindspore.float32)
- # zeros = ops.Zeros()
- # print(type(c))
- # print(zeros((3,3,3),mindspore.float32))
-
- # x = torch.randn(3, 3,3)
- # c = Tensor(np.random.randn(3,3,3), dtype=mindspore.float32)
- # print(x)
- # print(x.view(3,3,-1,3).transpose(2,3))
- # print(c)
- # transpose = ops.Transpose()
- # input_perm = (0, 1, 3, 2)
- # print(transpose(c.view(3,3,-1,3),input_perm))
-
- # x = torch.randn(3, 3)
- # print(x)
- # print(torch.stack([x,x]))
- # c = Tensor(np.random.randn(3,3), dtype=mindspore.float32)
- # stack = ops.Stack()
- # print(c)
- # print(stack([c,c]))
-
- # c = Tensor(np.random.randn(3,3,3), dtype=mindspore.float32)
- # x = torch.randn(3, 3,3)
- # tensor1 = initializer(XavierUniform(), c.shape, mindspore.float32)
- # print(c,x)
- # print(tensor1)
- # print(nn.init.xavier_uniform_(x))
-
-
- #
- # input = torch.randn(12,12,12,12)
- # n = nn.AvgPool2d((1,2))
- # print(n(input).shape)
- # #print(nn.AvgPool2d((1,2))(input))
- #
- # c = Tensor(np.random.randn(12,12,12,12), dtype=mindspore.float32)
- # pool = mm.AvgPool2d((1,2), stride=(1,2))
- # print(pool(c).shape)
-
-
- # from mindspore import context
- #
- # context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU")
- # kwargs = {'N': 128, 'L': 8, 'H': 128, 'R': 6, 'C': 2, 'input_normalize': False, 'sr': 8000, 'segment': 4}
- # model = SWave(**kwargs)
-
- # x = torch.randn(5, 5)
- # x = x.unfold(0, 3, 1)
- # print(x)
- # c = Tensor(np.random.randn(5,5), dtype=mindspore.float32)
- # unfold = mm.Unfold(ksizes=[3, 1],strides=[3, 1], rates=[3, 1])
- # print(unfold(c))
-
- # x = Tensor(np.ones((1,2,3,4), dtype=np.float32))
- # x = x.transpose(0,3,2,1)
- # print(x.shape)
-
- # x = torch.randn(1,2,3,4)
- # c = Tensor(np.random.randn(1,2,3,4), dtype=mindspore.float32)
- # print(c.shape[-2])
- # a ,b = x.size()[-2:]
- # print(a,b)
-
-
- # x=torch.arange(0,10).unfold(0,4,1)
- # print(x.shape)
- # print(x)
- # ans = np.empty((1,4))
-
-
- # x=torch.arange(0,10)
- # ans = np.array([0,1,2,3])
- # for i in range(1,7):
- # a = x[i:i+4]
- # ans = np.append(ans, a, axis=0)
- # ans = ans.reshape(-1,4)
- # print(ans)
- # x = mindspore.numpy.arange(0,10)
- # ans = mindspore.numpy.arange(0,4)
- # for i in range(1,10):
- # a = x[i:i+4]
- # print(a.shape)
- # if a.shape==(4,):
- # ans = mp.append(ans, a, axis=0)
- # reshape = ops.Reshape()
- # ans = reshape(ans,(-1,4))
- # print(ans)
- # output_subframes = 8002
- # subframe_step = 1
- # subframes_per_frame = 2
- # frame = mindspore.numpy.arange(0, output_subframes)
- # # frame = ops.Concat(-1)((ops.expand_dims(frame[0:-1:subframe_step], 1), ops.expand_dims(frame[1::subframe_step], 1)))
- # ans = mindspore.numpy.arange(0, subframes_per_frame)
- # for i in range(subframe_step, output_subframes - subframes_per_frame + 1, subframe_step):
- # a = frame[i:i + subframes_per_frame]
- # if a.shape == (subframes_per_frame,):
- # ans = mp.append(ans, a, axis=0)
- # print(ans,ans.shape)
- # reshape = ops.Reshape()
- # frame = reshape(ans, (-1, subframes_per_frame))
- # print(frame,frame.shape)
- #
- # frame = torch.arange(0, output_subframes).unfold(
- # 0, subframes_per_frame, subframe_step)
- # print(frame,frame.shape)
-
- # indices = (0, 1, 0, 1)
- # x = Tensor(np.array([[1, 2], [3, 4], [5, 6]]), mindspore.float32)
- # input_v = Tensor(np.array([[0.5, 1.0], [1.0, 1.5], [1.0, 1.5], [1.0, 1.5]]), mindspore.float32)
- # inplaceAdd = ops.InplaceAdd(indices)
- # output = inplaceAdd(x, input_v)
- # print(output)
-
-
- #
- # output_subframes = 8002
- # subframe_step = 1
- # subframes_per_frame = 2
- # frame = mindspore.numpy.arange(0, output_subframes)
- # frame = ops.Concat(-1)((ops.expand_dims(frame[0:-3:subframe_step], 1), ops.expand_dims(frame[1:-2:subframe_step], 1), ops.expand_dims(frame[2:-1:subframe_step], 1), ops.expand_dims(frame[3::subframe_step], 1)))
- # print(frame,frame.shape)
-
-
- # output_ii = Tensor(np.random.randn(1,2,3), dtype=mindspore.float32)
- # print(output_ii,output_ii.shape)
- # pad = mm.Pad(paddings=((0,0),(0,0),(1, 1)))
- # output_ii = pad(output_ii)
- # print(output_ii,output_ii.shape)
-
- # x = torch.randn(1, 2, 3)
- # print(x)
- # ans = F.pad(x,(0,-1))
- # print(ans,ans.shape)
-
- # class a():
- # def __init__(self):
- # print("hello")
- #
- # def __init__(self):
- # print("word")
- #
- # b = a()
-
- # context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
- # context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
- # print(Tensor(np.array([[[1]]]), mindspore.float32))
- # a = Tensor(6)
- # b = 2.0
- # c = Tensor([1, 2])
- # print(a*b)
- # print(b*c)
- # cnt = 3
- # print(1/cnt)
- # a = Tensor([[[1, 2],
- # [1, 2]]])
- # print(a.shape)
-
- # context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend",device_id=4)
- # a = Tensor(np.zeros((8002, 32008)), dtype=mindspore.float32)
- # print(a, a.shape)
- # expand_dims = ops.ExpandDims()
- # l = 0
- # for i in range(8002):
- # a[i, l] = 1
- # a[i, l + 1] = 1
- # a[i, l + 2] = 1
- # a[i, l + 3] = 1
- # l = l + 4
- #
- # a = ops.Cast()(a, mindspore.float32)
- # print(a, a.shape)
- # context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend",device_id=6)
- # ones = ops.Ones()
- # input_x1 = ones((2, 2, 3, 4), mindspore.float32)
- # input_x2 = ones((2, 2, 4, 5), mindspore.float32)
- # print(input_x2, input_x2.shape)
- # # print(np.matmul(input_x1, input_x2))
- # ans = ops.matmul(input_x1, input_x2)
- # # ans = matmul(input_x1, input_x2)
- # print(ans, ans.shape)
-
- context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU")
- # a = np.zeros(20, dtype=np.int16)
- # for i in range(1, 20):
- # if i % 4 == 0:
- # a[i] = a[i - 4] + 1
- # else:
- # a[i] = a[i - 1] + 1
- # mat = np.zeros((8, 20), dtype=np.int16)
- # for i in range(20):
- # mat[a[i]][i] = 1
- # mat = Tensor(mat, dtype=mindspore.float32)
- # transpose = ops.Transpose()
- # mat = transpose(mat, (1, 0))
- # print(mat)
- # a = Tensor(np.random.randn(4, 20), dtype=mindspore.float32)
- # print(a)
- # print(ops.matmul(a, mat))
-
- # milestone = []
- # learning_rates = []
- # for i in range(3000):
- # if (i % 2 == 0):
- # milestone.append(i)
- # learning_rates.append(1 * (0.5 ** (i / 2)))
- # print(milestone[:10])
- # print(learning_rates[:10])
-
- a = Tensor(np.random.randn(4, 20, 10), dtype=mindspore.float32)
- print(a.shape)
- a= a.transpose(2, 1, 0)
- print(a.shape)
|