Skip to content

Commit b3dbad2

Browse files
Merge pull request #54 from sk-ai-net/feature/mnist-0.0.5
Feature/mnist 0.0.5
2 parents 2ab561b + 90d609f commit b3dbad2

50 files changed

Lines changed: 5990 additions & 37 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010

1111
allprojects {
1212
group = "sk.ai.net"
13-
version = "0.0.5"
13+
version = "0.0.7"
1414
}
1515

1616
moduleGraphConfig {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package sk.ai.net
2+
3+
4+
interface Distribution<T> {
5+
6+
/**
7+
* Seed for the random number generator.
8+
*/
9+
val seed: Long
10+
11+
/**
12+
* Retrieves a new sample from the distribution.
13+
*
14+
* @return A T value.
15+
*/
16+
fun sample(): T
17+
18+
19+
/**
20+
* Retrieves the distribution's mean.
21+
*
22+
* @return The mean value.
23+
*/
24+
val mean: T
25+
26+
/**
27+
* Retrieves the distribution's standard deviation.
28+
*
29+
* @return The standard deviation.
30+
*/
31+
val deviation: T
32+
}

core/src/commonMain/kotlin/sk/ai/net/Shape.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package sk.ai.net
22

3+
import sk.ai.net.impl.assert
34
import sk.ai.net.impl.zipFold
45

56
class Shape(vararg dimensions: Int) {
@@ -11,6 +12,22 @@ class Shape(vararg dimensions: Int) {
1112
val rank: Int
1213
get() = dimensions.size
1314

15+
internal fun index(indices: IntArray): Int {
16+
assert(
17+
{ indices.size == dimensions.size },
18+
{ "`indices.size` must be ${dimensions.size}: ${indices.size}" })
19+
return dimensions.zip(indices).fold(0) { a, x ->
20+
assert(
21+
{ 0 <= x.second && x.second < x.first },
22+
{ "Illegal index: indices = ${indices}, shape = $dimensions" })
23+
a * x.first + x.second
24+
}
25+
}
26+
27+
operator fun get(vararg indices: Int): Int {
28+
return dimensions[index(indices)]
29+
}
30+
1431
override fun equals(other: Any?): Boolean {
1532
if (other !is Shape) {
1633
return false

core/src/commonMain/kotlin/sk/ai/net/Tensor.kt

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ interface Tensor {
5050

5151
fun relu(): Tensor
5252

53-
fun softmax(i: Int): Tensor
53+
fun softmax(dim: Int): Tensor
5454

5555
fun softmax(): Tensor
5656

@@ -63,30 +63,35 @@ interface Tensor {
6363
fun cos(): Tensor
6464

6565
fun tan(): Tensor
66-
66+
6767
fun asin(): Tensor
6868

6969
fun acos(): Tensor
70-
70+
7171
fun atan(): Tensor
7272

73-
fun sinh():Tensor
73+
fun sinh(): Tensor
7474

75-
fun cosh():Tensor
75+
fun cosh(): Tensor
7676

77-
fun tanh():Tensor
77+
fun tanh(): Tensor
7878

79-
fun exp():Tensor
79+
fun exp(): Tensor
8080

81-
fun log():Tensor
81+
fun log(): Tensor
8282

83-
fun sqrt():Tensor
83+
fun sqrt(): Tensor
8484

85-
fun cbrt():Tensor
85+
fun cbrt(): Tensor
8686

87-
fun sigmoid():Tensor
87+
fun sigmoid(): Tensor
8888

89-
fun ln():Tensor
89+
fun ln(): Tensor
90+
91+
fun flatten(startDim: Int = 1, endDim: Int = -1): Tensor
92+
}
9093

94+
fun Shape.toRanges(): Array<IntRange> {
95+
return dimensions.map { 0 until it -1 }.toTypedArray()
9196
}
9297

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package sk.ai.net
22

3+
import sk.ai.net.impl.BuiltInDoubleDataDescriptor
4+
import sk.ai.net.impl.DoublesTensor
5+
import kotlin.random.Random
6+
37
interface TensorFactory {
48
fun createTensor(shape: Shape, dataDescriptor: DataDescriptor, elements: DoubleArray): Tensor
5-
}
9+
}
10+
11+
interface DataDescriptorFactory {
12+
fun createDataDescriptor(): DataDescriptor
13+
}
14+
15+
16+
fun rand(shape: Shape, dataDescriptor: DataDescriptor = BuiltInDoubleDataDescriptor()): Tensor {
17+
val random: Random = Random.Default
18+
19+
return DoublesTensor(shape, DoubleArray(shape.volume) { random.nextFloat().toDouble() })
20+
}
21+
22+
fun zeros(shape: Shape, dataDescriptor: DataDescriptor = BuiltInDoubleDataDescriptor()): Tensor {
23+
return DoublesTensor(shape, DoubleArray(shape.volume))
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package sk.ai.net.core
2+
3+
import sk.ai.net.Tensor
4+
5+
data class Slice(val tensor: Tensor, val dimensionIndex: Int, val startIndex: Long, val endIndex: Long) {
6+
fun toRange() = startIndex..endIndex
7+
}
8+
9+
fun Slice.start() = startIndex
10+
11+
fun Slice.end() = endIndex
12+
13+
fun Slice.all() = startIndex..tensor.shape.dimensions[dimensionIndex]
14+
15+
fun Slice.range(): LongRange = startIndex..endIndex

core/src/commonMain/kotlin/sk/ai/net/impl/TypedTensor.kt renamed to core/src/commonMain/kotlin/sk/ai/net/core/TypedTensor.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package sk.ai.net.impl
1+
package sk.ai.net.core
22

33
import sk.ai.net.Tensor
44

@@ -11,7 +11,9 @@ interface TypedTensor<T> : Tensor {
1111
operator fun get(vararg indices: Int): T
1212

1313
operator fun get(vararg ranges: IntRange): TypedTensor<T>
14-
}
1514

15+
operator fun get(vararg ranges: Slice): Tensor
1616

17+
val allElements: List<T>
1718

19+
}

0 commit comments

Comments
 (0)