def checkDType(dtype): nError = 0 for transA in ['N', 'T', 'C']: if (transA == 'N'): sizeA = [2, 3] else: sizeA = [3, 2] for transB in ['N', 'T', 'C']: if (transB == 'N'): sizeB = [3, 4] else: sizeB = [4, 3] for orderA in ['F', 'C']: A = ocean.tensor(sizeA, orderA, dtype) At = ocean.arange(2 * sizeA[0] * sizeA[1], dtype.basetype) At = ocean.tensor(At.storage, 0, [sizeA[0], sizeA[1]], dtype) A.copy(At) An = ocean.tensor(sizeA, dtype) An.copy(A) for orderB in ['F', 'C']: B = ocean.tensor(sizeB, orderB, dtype) Bt = ocean.arange(2 * sizeB[0] * sizeB[1], dtype.basetype) Bt = ocean.tensor(Bt.storage, 0, [sizeB[0], sizeB[1]], dtype) B.copy(Bt) Bn = ocean.tensor(sizeB, dtype) Bn.copy(B) for orderC in ['F', 'C']: C = ocean.tensor([2, 4], orderC, dtype) print( "Setting: transA='%s', transB='%s', orderA='%s', orderB='%s', orderC='%s'\n" % (transA, transB, orderA, orderB, orderC)) ocean.gemm(1, A, transA, B, transB, 0, C) Cn = ocean.gemm(1, An, transA, Bn, transB) if (not ocean.all(C == Cn)): expectedStr = str(C).replace('\n', '\n ') obtainedStr = str(Cn).replace('\n', '\n ') print("Expected : %s" % expectedStr) print("Obtained : %s" % obtainedStr) nError += 1 else: expectedStr = str(C).replace('\n', '\n ') print("Success : %s" % expectedStr) if (nError == 0): print("All checks passed for data type %s" % dtype.name)
import pyOcean_cpu as ocean A = ocean.arange(12,ocean.float) print(A) A.byteswap() print(A) print(A.reshape([3,4])) A.reshape([3,4],True) print(A)
import pyOcean_cpu as ocean a = ocean.arange(3 * 5 * 3, ocean.int8).reshape([3, 5, 3]) a %= 12 print(a) print(ocean.nnz(a)) print(ocean.nnz(a, 0, True)) print(ocean.nnz(a, 1, True)) print(ocean.nnz(a, 2, True)) print(ocean.nnz(a, [0, 1], True))
import pyOcean_cpu as ocean a = ocean.arange(15).reshape(3, 5) print(a) idx = ocean.index[[[0, 2], [1, 1], [2, -1]]] print(a[idx])
import pyOcean_cpu as ocean a = ocean.arange(6) print(a.reshape([2, 3])) print(a.reshape(2, 3)) a.reshape(2, 3, True) print(a) a.reshape([3, 2], True) print(a)
import pyOcean_cpu as ocean a = ocean.arange(24).reshape([4, 6]) print(a) print(a[3]) print(a[3, [1, 3, 5]]) print(a[[0, 1, 3], 3])
import pyOcean_cpu as ocean a = ocean.arange(15).reshape([5, 3]) print(a + 100) print(a.T + 100) a += 100 print(a) b = a.T b += 100 print(a)
### Masked indexing, in-place addition and subtraction import pyOcean_cpu as ocean a = ocean.arange(25).reshape(5,5) print(a) print(a % 2) print(ocean.find(a % 2)) a[a%2 == 1] = 10 * ocean.arange(12) + 10; print(a) a[:,1] += 1 print(a) a[2] += a[2] print(a)
import pyOcean_cpu as ocean a = ocean.zeros([5, 5, 5]) a.permuteAxes([1, 2, 0], True) print(a.strides) a[:, [1, 2], :] = ocean.arange(50).reshape([5, 2, 5]) print(a)
## Indexing with ellipsis #1 import pyOcean_cpu as ocean import sys def success(command): print("%s = " % command) print(eval(command)) print("-----------------------------------") def fail(command): try: eval(command) print("%s: SUCCEEDED BUT SHOULD HAVE FAILED!" % command) except: print("%s: Expected error: %s" % (command, str(sys.exc_info()[1]))) a = ocean.arange(2 * 6 * 2 * 3).reshape([2, 6, 2, 3]) success("a[...]") success("a[...,2]") success("a[...,-1,:]") success("a[1,...]") success("a[1,None,...]") success("a[1,1,1,1,...]") fail("a[1,1,1,1,...,1]")
import pyOcean_cpu as ocean A = ocean.arange(24, ocean.float).reshape([2, 4, 3]) print(A) print(A.imag) print(A.imag.strides) A = ocean.asTensor([1 + 2j, 3 + 4j, 5 + 6j], ocean.chalf) r = A.imag print(r) r.fill(7) print(A)
import pyOcean_cpu as ocean a = ocean.arange(20, ocean.int8) print(a) b = ocean.tensor(a.storage, 10, [5], [-1]) print(b) b.zero() print(b) print(a)
import pyOcean_cpu as ocean def run(cmd) : print("====== %s ======" % cmd) print(eval(cmd)) a = ocean.arange(25).reshape(5,5) print(a) run("a[[1,2,1]]") run("a[:,[0,2,1]]") run("a[[2,1],:]") run("a[:,[1,2]]") run("a[[2,1],[1,2,2,2],None]") run("a[[[1,2],[2,3],[3,4]]]") b = (a <= a.T); run("b") f = ocean.find(b); run("f") run("a[None,b]") run("a[None,f]") run("a[[True,False,True,False,True],[True,True,False,False,True]]")
import pyOcean_cpu as ocean print(ocean.arange(10)) print(ocean.arange(2,10)) print(ocean.arange(2,10,3,ocean.int8)) print(ocean.arange(10,2,-3,ocean.half)) print(ocean.arange(10.)) print(ocean.arange(2.,10)) print(ocean.arange(2,10,3.,ocean.int8)) print(ocean.arange(10,2.,-3,ocean.half)) print("\n--------- Non-integer step ---------") print(ocean.arange(2,5,0.3)) print("\n--------- Single element ---------") print(ocean.arange(2,5,10)) print(ocean.arange(2,5,ocean.inf)) print("\n--------- Empty ---------") print(ocean.arange(5,2)) print(ocean.arange(2,5,-10)) print(ocean.arange(2,5,-ocean.inf)) print(ocean.arange(5,2.)) print(ocean.arange(2,5,-10.)) print(ocean.arange(2,5,-ocean.inf))
import pyOcean_cpu as ocean A = ocean.arange(2 * 3 * 4, ocean.float).reshape([2, 3, 4]) print(A) print(ocean.norm2(A, [0, 1])) A.fliplr(True) print(ocean.minimum(A, 0)) print(ocean.maximum(A, 2, True)) B = ocean.tensor([4], ocean.half) ocean.norm(A, 3, [0, 1], B) print(B) print(ocean.norm(A, 3, [0, 1]))
def exceptionMsg(): print("Expected error: %s" % str(sys.exc_info()[1])) def failTest(command): try: eval(command) except: exceptionMsg() # Boolean a = ocean.asTensor([True, False, False]) print(-a) # Unsigned integer (8) a = ocean.arange(5, ocean.uint8) print(-a) # Different types types = [ocean.int16, ocean.half, ocean.float, ocean.cdouble] for t in types: a = ocean.linspace(-5, 5, 11, t) print(-a) # Result in unsigned integer a = ocean.arange(-5, 5, ocean.int8) b = ocean.tensor(a.size, ocean.uint8) failTest("ocean.negative(a,b)")
import pyOcean_cpu as ocean import sys def exceptionMsg(): print("Expected error: %s" % str(sys.exc_info()[1])) # Tensor is byteswapped A = ocean.zeros([4, 5]) A.byteswap() A[2:4, 1:3] = 3 print(A) B = ocean.arange(2 * 2).reshape(2, 2) + 1 print(B) A.fill(0) A[1:3, 2:4] = B print(A) B.byteswap() A.fill(0) A[2:0:-1, 3:1:-1] = B print(A) A = ocean.zeros([4, 5]) A[[0, 3], [3, 0]] = B print(A)
import pyOcean_cpu as ocean a = ocean.arange(6, ocean.float).reshape([3, 2]) print(a)
import pyOcean_cpu as ocean import sys def success(command) : print("%s = " % command) print(eval(command)) print("-----------------------------------") def fail(command) : try : eval(command) print("%s: SUCCEEDED BUT SHOULD HAVE FAILED!" % command) except : print("%s: Expected error: %s" % (command, str(sys.exc_info()[1]))) a = ocean.arange(10) success("a") success("a[0]") success("a[1]") success("a[9]") success("a[-1]") success("a[-10]") fail("a[10]") fail("a[-11]") fail("a[1+2j]") fail("a[ocean.nan]") fail("a[1e200]") fail("a[ocean.inf]")
## Get index of byteswapped tensors import pyOcean_cpu as ocean A = ocean.arange(30).reshape([6, 5]) A.byteswap() A.readonly = True print(A) print(A[1, 3]) print(A[2, :]) print(A[..., 2]) print(A[2]) print(A[1, None, :, None, None, None]) print(A[0::3, ::-1]) print(A[0:3]) print(A[[1, 3, 1]])
import pyOcean_cpu as ocean a = ocean.arange(10,ocean.double) a.byteswap() print(a) print(ocean.sqrt(a)) ocean.sqrt(a,a) print(a)
import pyOcean_cpu as ocean a = ocean.asTensor([[1, 2], [3, 4]], 'r', ocean.float) b = ocean.arange(6, ocean.float).reshape([2, 3]) print(a) print(b) print(ocean.gemm(ocean.float(3), a, b)) alpha = ocean.asTensor([1, 2, 3], ocean.float).reshape([1, 1, 3]) print(ocean.gemm(alpha, a, b)) print(ocean.gemm(ocean.float(1), a.T, b))
import pyOcean_cpu as ocean a = ocean.asTensor([[0, 1, 2.], [3, 4, 5]]) b = ocean.arange(12, ocean.double).reshape([3, 4]) print(a.T) print(b) print(ocean.gemm(1, a, 'T', b)) print(ocean.gemm(1, a, a, 'T'))
import pyOcean_cpu as ocean import sys def exceptionMsg(): print("Expected error: %s" % str(sys.exc_info()[1])) def failTest(command): try: eval(command) except: exceptionMsg() a = ocean.arange(12).reshape([3, 4]) print(a) print(a.unsqueeze(0)) print(a.unsqueeze(2)) a.unsqueeze(0, True) print(a) a.unsqueeze(2, True) print(a) failTest("a.unsqueeze(-1)") failTest("a.unsqueeze(5)")
import pyOcean_cpu as ocean import sys def exceptionMsg(): print("Expected error: %s" % str(sys.exc_info()[1])) def failTest(command): try: eval(command) except: exceptionMsg() print(ocean.arange(250, 256, 1, ocean.uint8)) print(ocean.arange(0, 2, 0.2, ocean.bool)) print(ocean.arange(5, ocean.cdouble)) #failTest("ocean.arange(110,310,50,ocean.uint8)") #failTest("ocean.arange(28,130,20,ocean.int8)")