TensorOps_F32 { public static void elementMult(Tensor_F32 tensor , float value ) { int index = tensor.startIndex; int end = index + tensor.length(); while( index < end ) { tensor.d[index++] *= value; } }  static void elementMult(Tensor_F32 tensor , float value  ); static void elementMult(Tensor_F32 input , float value , Tensor_F32 output ); static void elementMult(Tensor_F32 A , Tensor_F32 B , Tensor_F32 output ); static void elementAdd(Tensor_F32 A , Tensor_F32 B , Tensor_F32 output ); static float elementSum( Tensor_F32 tensor ); static void insertSubChannel( Tensor_F32 src , int srcStartIndex , int srcStride ,
										 Tensor_F32 dst , int dstStartIndex , int dstStride ,
										 int rows , int columns ); static void insertSpatial(Tensor_F32 src , int []srcCoor,
									 Tensor_F32 dst , int []dstCoor ); static void fillSpatialBorder(Tensor_F32 tensor , int[] coor ,
										 int borderY0 , int borderX0 , int borderY1 , int borderX1 ,
										 float value ); static void printSpatial( Tensor_F32 tensor , int batch , int channel ); static void fill( Tensor_F32 tensor , float value );  }
@Test public void elementMult_scalar() { for( boolean sub : new boolean[]{false,true}) { Tensor_F32 T = TensorFactory_F32.random(rand,sub, 5,3,1); Tensor_F32 original = T.copy(); float s = 2.1f; TensorOps_F32.elementMult(T,s); int N = original.length(); for (int i = 0; i < N; i++) { assertEquals( original.getAtIndex(i)*s , T.getAtIndex(i) , DeepBoofConstants.TEST_TOL_F32 ); } } }
@Test public void elementMult_scalar_tensor() { for( boolean sub : new boolean[]{false,true}) { Tensor_F32 T = TensorFactory_F32.random(rand,sub, 5,3,1); Tensor_F32 O = TensorFactory_F32.random(rand,sub, 5,3,1); Tensor_F32 original = T.copy(); float s = 2.1f; TensorOps_F32.elementMult(T,s,O); int N = original.length(); for (int i = 0; i < N; i++) { assertEquals( original.getAtIndex(i) , T.getAtIndex(i) , DeepBoofConstants.TEST_TOL_F32 ); assertEquals( original.getAtIndex(i)*s , O.getAtIndex(i) , DeepBoofConstants.TEST_TOL_F32 ); } } }
@Test public void elementMult_tensor() { List<int[]> shapes = new ArrayList<>(); shapes.add(new int[]{5}); shapes.add(new int[]{2,3,4}); for( boolean subtensor : new boolean[]{false,true}) { for( int[] shape : shapes ) { Tensor_F32 A = TensorFactory_F32.random(rand, subtensor, shape); Tensor_F32 B = TensorFactory_F32.random(rand, subtensor, shape); Tensor_F32 found = TensorFactory_F32.random(rand, subtensor, shape); Tensor_F32 expected = new Tensor_F32(shape); for (int i = 0; i < A.length(); i++) { expected.d[expected.startIndex+i] = A.d[A.startIndex+i]*B.d[B.startIndex+i]; } TensorOps_F32.elementMult(A,B,found); DeepUnitTest.assertEquals(expected,found,DeepBoofConstants.TEST_TOL_F32); } } }