v0.3.0-dev-9 #324

Merged
altavir merged 265 commits from dev into master 2021-05-08 17:16:29 +03:00
3 changed files with 19 additions and 31 deletions
Showing only changes of commit 431db00f1a - Show all commits

View File

@ -9,7 +9,7 @@ import kotlin.test.assertTrue
internal class TestBroadcasting { internal class TestBroadcasting {
@Test @Test
fun broadcastShapes() = DoubleTensorAlgebra { fun testBroadcastShapes() = DoubleTensorAlgebra {
assertTrue( assertTrue(
broadcastShapes( broadcastShapes(
intArrayOf(2, 3), intArrayOf(1, 3), intArrayOf(1, 1, 1) intArrayOf(2, 3), intArrayOf(1, 3), intArrayOf(1, 1, 1)
@ -24,7 +24,7 @@ internal class TestBroadcasting {
} }
@Test @Test
fun broadcastTo() = DoubleTensorAlgebra { fun testBroadcastTo() = DoubleTensorAlgebra {
val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val tensor2 = fromArray(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0)) val tensor2 = fromArray(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0))
@ -34,7 +34,7 @@ internal class TestBroadcasting {
} }
@Test @Test
fun broadcastTensors() = DoubleTensorAlgebra { fun testBroadcastTensors() = DoubleTensorAlgebra {
val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val tensor2 = fromArray(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0)) val tensor2 = fromArray(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0))
val tensor3 = fromArray(intArrayOf(1, 1, 1), doubleArrayOf(500.0)) val tensor3 = fromArray(intArrayOf(1, 1, 1), doubleArrayOf(500.0))
@ -51,7 +51,7 @@ internal class TestBroadcasting {
} }
@Test @Test
fun broadcastOuterTensors() = DoubleTensorAlgebra { fun testBroadcastOuterTensors() = DoubleTensorAlgebra {
val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val tensor2 = fromArray(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0)) val tensor2 = fromArray(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0))
val tensor3 = fromArray(intArrayOf(1, 1, 1), doubleArrayOf(500.0)) val tensor3 = fromArray(intArrayOf(1, 1, 1), doubleArrayOf(500.0))
@ -68,7 +68,7 @@ internal class TestBroadcasting {
} }
@Test @Test
fun broadcastOuterTensorsShapes() = DoubleTensorAlgebra { fun testBroadcastOuterTensorsShapes() = DoubleTensorAlgebra {
val tensor1 = fromArray(intArrayOf(2, 1, 3, 2, 3), DoubleArray(2 * 1 * 3 * 2 * 3) {0.0}) val tensor1 = fromArray(intArrayOf(2, 1, 3, 2, 3), DoubleArray(2 * 1 * 3 * 2 * 3) {0.0})
val tensor2 = fromArray(intArrayOf(4, 2, 5, 1, 3, 3), DoubleArray(4 * 2 * 5 * 1 * 3 * 3) {0.0}) val tensor2 = fromArray(intArrayOf(4, 2, 5, 1, 3, 3), DoubleArray(4 * 2 * 5 * 1 * 3 * 3) {0.0})
val tensor3 = fromArray(intArrayOf(1, 1), doubleArrayOf(500.0)) val tensor3 = fromArray(intArrayOf(1, 1), doubleArrayOf(500.0))
@ -81,7 +81,7 @@ internal class TestBroadcasting {
} }
@Test @Test
fun minusTensor() = BroadcastDoubleTensorAlgebra.invoke { fun testMinusTensor() = BroadcastDoubleTensorAlgebra.invoke {
val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val tensor2 = fromArray(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0)) val tensor2 = fromArray(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0))
val tensor3 = fromArray(intArrayOf(1, 1, 1), doubleArrayOf(500.0)) val tensor3 = fromArray(intArrayOf(1, 1, 1), doubleArrayOf(500.0))

View File

@ -15,14 +15,14 @@ import kotlin.test.assertTrue
internal class TestDoubleTensor { internal class TestDoubleTensor {
@Test @Test
fun valueTest() = DoubleTensorAlgebra { fun testValue() = DoubleTensorAlgebra {
val value = 12.5 val value = 12.5
val tensor = fromArray(intArrayOf(1), doubleArrayOf(value)) val tensor = fromArray(intArrayOf(1), doubleArrayOf(value))
assertEquals(tensor.value(), value) assertEquals(tensor.value(), value)
} }
@Test @Test
fun stridesTest() = DoubleTensorAlgebra { fun testStrides() = DoubleTensorAlgebra {
val tensor = fromArray(intArrayOf(2, 2), doubleArrayOf(3.5, 5.8, 58.4, 2.4)) val tensor = fromArray(intArrayOf(2, 2), doubleArrayOf(3.5, 5.8, 58.4, 2.4))
assertEquals(tensor[intArrayOf(0, 1)], 5.8) assertEquals(tensor[intArrayOf(0, 1)], 5.8)
assertTrue( assertTrue(
@ -31,7 +31,7 @@ internal class TestDoubleTensor {
} }
@Test @Test
fun getTest() = DoubleTensorAlgebra { fun testGet() = DoubleTensorAlgebra {
val tensor = fromArray(intArrayOf(1, 2, 2), doubleArrayOf(3.5, 5.8, 58.4, 2.4)) val tensor = fromArray(intArrayOf(1, 2, 2), doubleArrayOf(3.5, 5.8, 58.4, 2.4))
val matrix = tensor[0].as2D() val matrix = tensor[0].as2D()
assertEquals(matrix[0, 1], 5.8) assertEquals(matrix[0, 1], 5.8)
@ -55,7 +55,7 @@ internal class TestDoubleTensor {
} }
@Test @Test
fun noBufferProtocol() { fun testNoBufferProtocol() {
// create buffer // create buffer
val doubleArray = DoubleBuffer(doubleArrayOf(1.0, 2.0, 3.0)) val doubleArray = DoubleBuffer(doubleArrayOf(1.0, 2.0, 3.0))

View File

@ -10,28 +10,28 @@ import kotlin.test.assertTrue
internal class TestDoubleTensorAlgebra { internal class TestDoubleTensorAlgebra {
@Test @Test
fun doublePlus() = DoubleTensorAlgebra { fun testDoublePlus() = DoubleTensorAlgebra {
val tensor = fromArray(intArrayOf(2), doubleArrayOf(1.0, 2.0)) val tensor = fromArray(intArrayOf(2), doubleArrayOf(1.0, 2.0))
val res = 10.0 + tensor val res = 10.0 + tensor
assertTrue(res.mutableBuffer.array() contentEquals doubleArrayOf(11.0, 12.0)) assertTrue(res.mutableBuffer.array() contentEquals doubleArrayOf(11.0, 12.0))
} }
@Test @Test
fun doubleDiv() = DoubleTensorAlgebra { fun TestDoubleDiv() = DoubleTensorAlgebra {
val tensor = fromArray(intArrayOf(2), doubleArrayOf(2.0, 4.0)) val tensor = fromArray(intArrayOf(2), doubleArrayOf(2.0, 4.0))
val res = 2.0/tensor val res = 2.0/tensor
assertTrue(res.mutableBuffer.array() contentEquals doubleArrayOf(1.0, 0.5)) assertTrue(res.mutableBuffer.array() contentEquals doubleArrayOf(1.0, 0.5))
} }
@Test @Test
fun divDouble() = DoubleTensorAlgebra { fun testDivDouble() = DoubleTensorAlgebra {
val tensor = fromArray(intArrayOf(2), doubleArrayOf(10.0, 5.0)) val tensor = fromArray(intArrayOf(2), doubleArrayOf(10.0, 5.0))
val res = tensor / 2.5 val res = tensor / 2.5
assertTrue(res.mutableBuffer.array() contentEquals doubleArrayOf(4.0, 2.0)) assertTrue(res.mutableBuffer.array() contentEquals doubleArrayOf(4.0, 2.0))
} }
@Test @Test
fun transpose1x1() = DoubleTensorAlgebra { fun testTranspose1x1() = DoubleTensorAlgebra {
val tensor = fromArray(intArrayOf(1), doubleArrayOf(0.0)) val tensor = fromArray(intArrayOf(1), doubleArrayOf(0.0))
val res = tensor.transpose(0, 0) val res = tensor.transpose(0, 0)
@ -40,7 +40,7 @@ internal class TestDoubleTensorAlgebra {
} }
@Test @Test
fun transpose3x2() = DoubleTensorAlgebra { fun testTranspose3x2() = DoubleTensorAlgebra {
val tensor = fromArray(intArrayOf(3, 2), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) val tensor = fromArray(intArrayOf(3, 2), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val res = tensor.transpose(1, 0) val res = tensor.transpose(1, 0)
@ -49,7 +49,7 @@ internal class TestDoubleTensorAlgebra {
} }
@Test @Test
fun transpose1x2x3() = DoubleTensorAlgebra { fun testTranspose1x2x3() = DoubleTensorAlgebra {
val tensor = fromArray(intArrayOf(1, 2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) val tensor = fromArray(intArrayOf(1, 2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val res01 = tensor.transpose(0, 1) val res01 = tensor.transpose(0, 1)
val res02 = tensor.transpose(-3, 2) val res02 = tensor.transpose(-3, 2)
@ -65,7 +65,7 @@ internal class TestDoubleTensorAlgebra {
} }
@Test @Test
fun linearStructure() = DoubleTensorAlgebra { fun testLinearStructure() = DoubleTensorAlgebra {
val shape = intArrayOf(3) val shape = intArrayOf(3)
val tensorA = full(value = -4.5, shape = shape) val tensorA = full(value = -4.5, shape = shape)
val tensorB = full(value = 10.9, shape = shape) val tensorB = full(value = 10.9, shape = shape)
@ -97,7 +97,7 @@ internal class TestDoubleTensorAlgebra {
} }
@Test @Test
fun dot() = DoubleTensorAlgebra { fun testDot() = DoubleTensorAlgebra {
val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val tensor11 = fromArray(intArrayOf(3, 2), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) val tensor11 = fromArray(intArrayOf(3, 2), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val tensor2 = fromArray(intArrayOf(3), doubleArrayOf(10.0, 20.0, 30.0)) val tensor2 = fromArray(intArrayOf(3), doubleArrayOf(10.0, 20.0, 30.0))
@ -118,22 +118,10 @@ internal class TestDoubleTensorAlgebra {
val res11 = tensor1.dot(tensor11) val res11 = tensor1.dot(tensor11)
assertTrue(res11.mutableBuffer.array() contentEquals doubleArrayOf(22.0, 28.0, 49.0, 64.0)) assertTrue(res11.mutableBuffer.array() contentEquals doubleArrayOf(22.0, 28.0, 49.0, 64.0))
assertTrue(res11.shape contentEquals intArrayOf(2, 2)) assertTrue(res11.shape contentEquals intArrayOf(2, 2))
var tensor4 = fromArray(intArrayOf(10, 3, 4), DoubleArray(10 * 3 * 4) {0.0})
var tensor5 = fromArray(intArrayOf(10, 4, 5), DoubleArray(10 * 4 * 5) {0.0})
assertTrue(tensor4.dot(tensor5).shape contentEquals intArrayOf(10, 3, 5))
tensor4 = fromArray(intArrayOf(10, 3, 4), DoubleArray(10 * 3 * 4) {0.0})
tensor5 = fromArray(intArrayOf(4, 5), DoubleArray(4 * 5) {0.0})
assertTrue(tensor4.dot(tensor5).shape contentEquals intArrayOf(10, 3, 5))
tensor4 = fromArray(intArrayOf(4, 2, 1, 3, 8, 1), DoubleArray(4 * 2 * 1 * 3 * 8 * 1) {0.0})
tensor5 = fromArray(intArrayOf(5, 1, 2, 8, 3, 1, 5), DoubleArray(5 * 1 * 2 * 8 * 3 * 1 * 5) {0.0})
assertTrue(tensor4.dot(tensor5).shape contentEquals intArrayOf(5, 4, 2, 8, 3, 8, 5))
} }
@Test @Test
fun diagonalEmbedding() = DoubleTensorAlgebra { fun testDiagonalEmbedding() = DoubleTensorAlgebra {
val tensor1 = fromArray(intArrayOf(3), doubleArrayOf(10.0, 20.0, 30.0)) val tensor1 = fromArray(intArrayOf(3), doubleArrayOf(10.0, 20.0, 30.0))
val tensor2 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) val tensor2 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val tensor3 = zeros(intArrayOf(2, 3, 4, 5)) val tensor3 = zeros(intArrayOf(2, 3, 4, 5))