def test_elementwise(): A1 = np.float32(np.random.randn(100,100)) A2 = np.float32(np.random.randn(100,100)) B1 = gpu.array(A1) B2 = gpu.array(A2) t.assert_almost_equal(gpu.abs(B1).tocpu(), np.abs(A1), 3, "Abs not working") #t.assert_almost_equal(gpu.log(B1).tocpu(), np.log(A1), 3, "Log not working") #t.assert_almost_equal(gpu.sqrt(B1).tocpu(), np.sqrt(A1), 3, "Sqrt not working") t.assert_almost_equal(gpu.pow(B1,2.0).tocpu(), np.power(A1,2.0), 3, "Pow not working") t.assert_almost_equal(gpu.logistic(B1).tocpu(), 1.0/(1.0+np.exp(-A1)), 3, "Logistic not working") t.assert_almost_equal(gpu.logistic_grad(B1).tocpu(), A1*(1.0-A1), 3, "Logistic grad not working") t.assert_almost_equal(gpu.rectified_linear(B1).tocpu(), A1*(A1>0), 3, "Rectified not working") t.assert_almost_equal(gpu.rectified_linear_grad(B1).tocpu(), (A1>0), 3, "Rectified grad not working") t.assert_almost_equal(gpu.add(B1,B2).tocpu(), A1+A2, 3, "Add not working") t.assert_almost_equal(gpu.sub(B1,B2).tocpu(), A1-A2, 3, "Sub not working") t.assert_almost_equal(gpu.mul(B1,B2).tocpu(), A1*A2, 3, "Mul not working") t.assert_almost_equal(gpu.div(B1,B2).tocpu(), A1/A2, 3, "Div not working") t.assert_almost_equal(gpu.equal(B1,B2).tocpu(), np.equal(A1,A2), 3, "Equal not working") t.assert_almost_equal(gpu.less(B1,B2).tocpu(), np.less(A1,A2), 3, "Less not working") t.assert_almost_equal(gpu.greater(B1,B2).tocpu(), np.greater(A1,A2), 3, "Greater not working") t.assert_almost_equal(gpu.greater_equal(B1,B2).tocpu(), np.greater_equal(A1,A2), 3, "Greater equal not working") t.assert_almost_equal(gpu.less_equal(B1,B2).tocpu(), np.less_equal(A1,A2), 3, "Less equal not working") t.assert_almost_equal(gpu.not_equal(B1,B2).tocpu(), np.not_equal(A1,A2), 3, "Not equal not working") t.assert_almost_equal(gpu.squared_difference(B1,B2).tocpu(), (A1-A2)**2, 3, "Squared difference not working") t.assert_almost_equal(gpu.copy(B1).tocpu(), A1.copy(), 3, "Copynot working")
def test_elementwise(): A1 = np.float32(np.random.randn(100, 100)) A2 = np.float32(np.random.randn(100, 100)) B1 = gpu.array(A1) B2 = gpu.array(A2) t.assert_almost_equal( gpu.abs(B1).tocpu(), np.abs(A1), 3, "Abs not working") #t.assert_almost_equal(gpu.log(B1).tocpu(), np.log(A1), 3, "Log not working") #t.assert_almost_equal(gpu.sqrt(B1).tocpu(), np.sqrt(A1), 3, "Sqrt not working") t.assert_almost_equal( gpu.pow(B1, 2.0).tocpu(), np.power(A1, 2.0), 3, "Pow not working") t.assert_almost_equal( gpu.logistic(B1).tocpu(), 1.0 / (1.0 + np.exp(-A1)), 3, "Logistic not working") t.assert_almost_equal( gpu.logistic_grad(B1).tocpu(), A1 * (1.0 - A1), 3, "Logistic grad not working") t.assert_almost_equal( gpu.rectified_linear(B1).tocpu(), A1 * (A1 > 0), 3, "Rectified not working") t.assert_almost_equal( gpu.rectified_linear_grad(B1).tocpu(), (A1 > 0), 3, "Rectified grad not working") t.assert_almost_equal( gpu.add(B1, B2).tocpu(), A1 + A2, 3, "Add not working") t.assert_almost_equal( gpu.sub(B1, B2).tocpu(), A1 - A2, 3, "Sub not working") t.assert_almost_equal( gpu.mul(B1, B2).tocpu(), A1 * A2, 3, "Mul not working") t.assert_almost_equal( gpu.div(B1, B2).tocpu(), A1 / A2, 3, "Div not working") t.assert_almost_equal( gpu.equal(B1, B2).tocpu(), np.equal(A1, A2), 3, "Equal not working") t.assert_almost_equal( gpu.less(B1, B2).tocpu(), np.less(A1, A2), 3, "Less not working") t.assert_almost_equal( gpu.greater(B1, B2).tocpu(), np.greater(A1, A2), 3, "Greater not working") t.assert_almost_equal( gpu.greater_equal(B1, B2).tocpu(), np.greater_equal(A1, A2), 3, "Greater equal not working") t.assert_almost_equal( gpu.less_equal(B1, B2).tocpu(), np.less_equal(A1, A2), 3, "Less equal not working") t.assert_almost_equal( gpu.not_equal(B1, B2).tocpu(), np.not_equal(A1, A2), 3, "Not equal not working") t.assert_almost_equal( gpu.squared_difference(B1, B2).tocpu(), (A1 - A2)**2, 3, "Squared difference not working") t.assert_almost_equal( gpu.copy(B1).tocpu(), A1.copy(), 3, "Copynot working")
def test_Transpose(): A = np.float32(np.random.rand(17,83)) B = gpu.array(A.T) C = B.tocpu() t.assert_array_equal(A.T,C,"Transpose error!") A = np.float32(np.random.rand(100,1)) B = gpu.array(A.T) C = B.tocpu() t.assert_array_equal(A.T,C,"Transpose error!") A = np.float32(np.random.rand(1,100)) B = gpu.array(A.T) C = B.tocpu() t.assert_array_equal(A.T,C,"Transpose error!")
def test_Transpose(): A = np.float32(np.random.rand(17, 83)) B = gpu.array(A.T) C = B.tocpu() t.assert_array_equal(A.T, C, "Transpose error!") A = np.float32(np.random.rand(100, 1)) B = gpu.array(A.T) C = B.tocpu() t.assert_array_equal(A.T, C, "Transpose error!") A = np.float32(np.random.rand(1, 100)) B = gpu.array(A.T) C = B.tocpu() t.assert_array_equal(A.T, C, "Transpose error!")
def slice_test(): A = np.float32(np.random.randn(100,100)) B = gpu.array(A) C = gpu.slice(B,17,83,7,23).tocpu() print( A) t.assert_almost_equal(C, A[17:83,7:23], 3, "Slicing not working")
def slice_test(): A = np.float32(np.random.randn(100, 100)) B = gpu.array(A) C = gpu.slice(B, 17, 83, 7, 23).tocpu() print(A) t.assert_almost_equal(C, A[17:83, 7:23], 3, "Slicing not working")
def test_dot(): A1 = np.float32(np.random.rand(2,4)) A2 = np.float32(np.random.rand(4,2)) B1 = gpu.array(A1) B2 = gpu.array(A2) B3 = gpu.dot(B1,B2) C = B3.tocpu() t.assert_array_almost_equal(np.dot(A1,A2),C,4,"array.tocpu not equal to init array!") B1 = gpu.array(A1) B2 = gpu.array(A2) B3 = gpu.empty((2,2)) gpu.dot(B1,B2,B3) t.assert_array_almost_equal(np.dot(A1,A2),B3.tocpu(),4,"array.tocpu not equal to init array!")
def test_dot(): A1 = np.float32(np.random.rand(2, 4)) A2 = np.float32(np.random.rand(4, 2)) B1 = gpu.array(A1) B2 = gpu.array(A2) B3 = gpu.dot(B1, B2) C = B3.tocpu() t.assert_array_almost_equal(np.dot(A1, A2), C, 4, "array.tocpu not equal to init array!") B1 = gpu.array(A1) B2 = gpu.array(A2) B3 = gpu.empty((2, 2)) gpu.dot(B1, B2, B3) t.assert_array_almost_equal(np.dot(A1, A2), B3.tocpu(), 4, "array.tocpu not equal to init array!")
def test_cpu(): A = np.ones((5,7)) B = gpu.ones((5,7)) C = B.tocpu() t.assert_array_equal(A, C, "To GPU does not work!") A = np.float32(np.random.rand(5,7)) B = gpu.array(A) C = B.tocpu() t.assert_array_equal(A, C, "To GPU does not work!")
def test_matrix_reductions(): A = np.float32(np.random.randn(100,100)) B = gpu.array(A) C = gpu.sum(B) t.assert_almost_equal(C, np.sum(A), 2, "Sum not working") C = gpu.max(B) t.assert_almost_equal(C, np.max(A), 3, "Max not working") C = gpu.mean(B) t.assert_almost_equal(C, np.mean(A), 3, "Max not working")
def test_matrix_reductions(): A = np.float32(np.random.randn(100, 100)) B = gpu.array(A) C = gpu.sum(B) t.assert_almost_equal(C, np.sum(A), 2, "Sum not working") C = gpu.max(B) t.assert_almost_equal(C, np.max(A), 3, "Max not working") C = gpu.mean(B) t.assert_almost_equal(C, np.mean(A), 3, "Max not working")
def test_cpu(): A = np.ones((5, 7)) B = gpu.ones((5, 7)) C = B.tocpu() t.assert_array_equal(A, C, "To GPU does not work!") A = np.float32(np.random.rand(5, 7)) B = gpu.array(A) C = B.tocpu() t.assert_array_equal(A, C, "To GPU does not work!")
def test_col_reductions(): A = np.float32(np.random.randn(100,110)) B = gpu.array(A) C = gpu.col_sum(B).tocpu() t.assert_almost_equal(C, np.sum(A,0), 3, "Colsum not working") C = gpu.col_max(B).tocpu() t.assert_almost_equal(C, np.max(A,0), 3, "Colmax not working") C = gpu.col_mean(B).tocpu() t.assert_almost_equal(C, np.mean(A,0), 3, "Colmean not working")
def test_row_reductions(): A = np.float32(np.random.randn(100, 110)) B = gpu.array(A) C = gpu.row_sum(B).tocpu() t.assert_almost_equal(C, np.sum(A, 1), 3, "Rowsum not working") C = gpu.row_max(B).tocpu() t.assert_almost_equal(C, np.max(A, 1), 3, "Rowmax not working") C = gpu.row_mean(B).tocpu() t.assert_almost_equal(C, np.mean(A, 1), 3, "Rowmean not working")
def test_col_reductions(): A = np.float32(np.random.randn(100, 110)) B = gpu.array(A) C = gpu.col_sum(B).tocpu() t.assert_almost_equal(C, np.sum(A, 0), 3, "Colsum not working") C = gpu.col_max(B).tocpu() t.assert_almost_equal(C, np.max(A, 0), 3, "Colmax not working") C = gpu.col_mean(B).tocpu() t.assert_almost_equal(C, np.mean(A, 0), 3, "Colmean not working")
def test_row_reductions(): A = np.float32(np.random.randn(100,110)) B = gpu.array(A) C = gpu.row_sum(B).tocpu() t.assert_almost_equal(C, np.sum(A,1), 3, "Rowsum not working") C = gpu.row_max(B).tocpu() t.assert_almost_equal(C, np.max(A,1), 3, "Rowmax not working") C = gpu.row_mean(B).tocpu() t.assert_almost_equal(C, np.mean(A,1), 3, "Rowmean not working")
def test_vectorwise(): def create_t_matrix(y, classes): t = np.zeros((y.shape[0], classes+1)) for i in range(y.shape[0]): t[np.int32(i), np.int32(y[i])] = 1.0 return t A = np.float32(np.random.randn(2,4)) v = np.float32(np.random.randn(1,4)) labels = np.float32(np.random.randint(0,4,128)).reshape(128,1) B = gpu.array(A) V = gpu.array(v) Y = gpu.array(labels) t.assert_almost_equal(gpu.vector_add(B, V).tocpu(), A+v, 3, "Vec add not working") t.assert_almost_equal(gpu.create_t_matrix(Y, 9).tocpu(), create_t_matrix(labels,9), 3, "Tmatrix not working")
def test_vectorwise(): def create_t_matrix(y, classes): t = np.zeros((y.shape[0], classes + 1)) for i in range(y.shape[0]): t[np.int32(i), np.int32(y[i])] = 1.0 return t A = np.float32(np.random.randn(2, 4)) v = np.float32(np.random.randn(1, 4)) labels = np.float32(np.random.randint(0, 4, 128)).reshape(128, 1) B = gpu.array(A) V = gpu.array(v) Y = gpu.array(labels) t.assert_almost_equal( gpu.vector_add(B, V).tocpu(), A + v, 3, "Vec add not working") t.assert_almost_equal( gpu.create_t_matrix(Y, 9).tocpu(), create_t_matrix(labels, 9), 3, "Tmatrix not working")
def softmax_test(): def softmax(X): #numerically stable softmax function max_row_values = np.matrix(np.max(X,axis=1)).T result = np.exp(X - max_row_values) sums = np.matrix(np.sum(result,axis=1)) return result/sums A = np.float32(np.random.randn(17,83)) B = gpu.array(A) C = gpu.softmax(B).tocpu() t.assert_almost_equal(C, softmax(A), 3, "Softmax not working")
def softmax_test(): def softmax(X): #numerically stable softmax function max_row_values = np.matrix(np.max(X, axis=1)).T result = np.exp(X - max_row_values) sums = np.matrix(np.sum(result, axis=1)) return result / sums A = np.float32(np.random.randn(17, 83)) B = gpu.array(A) C = gpu.softmax(B).tocpu() t.assert_almost_equal(C, softmax(A), 3, "Softmax not working")
def test_get_view(): X = np.float32(np.random.rand(10,10)) Y = np.copy(X) A = gpu.array(X) B = gpu.get_view(A, rstart=0, rend=5) t.assert_equal(X[0:5], B.tocpu(), "Get view not working!") B = gpu.get_view(A, rstart=5, rend=10) t.assert_equal(X[5:10], B.tocpu(), "Get view not working!") gpu.sqrt(B, B) t.assert_equal(Y[0:5], A.tocpu()[0:5], "Partial application to view not working!") t.assert_equal(np.sqrt(Y[5:10]), A.tocpu()[5:10], "Partial application to view not working!")
def test_get_view(): X = np.float32(np.random.rand(10, 10)) Y = np.copy(X) A = gpu.array(X) B = gpu.get_view(A, rstart=0, rend=5) t.assert_equal(X[0:5], B.tocpu(), "Get view not working!") B = gpu.get_view(A, rstart=5, rend=10) t.assert_equal(X[5:10], B.tocpu(), "Get view not working!") gpu.sqrt(B, B) t.assert_equal(Y[0:5], A.tocpu()[0:5], "Partial application to view not working!") t.assert_equal(np.sqrt(Y[5:10]), A.tocpu()[5:10], "Partial application to view not working!")
def test_printmat(): X = np.float32(np.random.rand(2, 2)) A = gpu.array(X) gpu.printmat(A) print(X)
def test_togpu(): A = np.float32(np.random.rand(17, 83)) B = gpu.array(A) C = B.tocpu() t.assert_array_almost_equal(A, C, 4, "array.tocpu not equal to init array!")
def argmax_test(): A = np.float32(np.random.randn(123, 83)) B = gpu.array(A) C = gpu.argmax(B).tocpu() t.assert_almost_equal(C, np.argmax(A, 1), 3, "Softmax not working")
def argmax_test(): A = np.float32(np.random.randn(123,83)) B = gpu.array(A) C = gpu.argmax(B).tocpu() t.assert_almost_equal(C, np.argmax(A,1), 3, "Softmax not working")
def test_togpu(): A = np.float32(np.random.rand(17,83)) B = gpu.array(A) C = B.tocpu() t.assert_array_almost_equal(A,C,4,"array.tocpu not equal to init array!")
def test_printmat(): X = np.float32(np.random.rand(2,2)) A = gpu.array(X) gpu.printmat(A) print( X)