Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {

allprojects {
group = "sk.ai.net"
version = "0.0.5"
version = "0.0.7"
}

moduleGraphConfig {
Expand Down
32 changes: 32 additions & 0 deletions core/src/commonMain/kotlin/sk/ai/net/Distribution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package sk.ai.net


interface Distribution<T> {

/**
* Seed for the random number generator.
*/
val seed: Long

/**
* Retrieves a new sample from the distribution.
*
* @return A T value.
*/
fun sample(): T


/**
* Retrieves the distribution's mean.
*
* @return The mean value.
*/
val mean: T

/**
* Retrieves the distribution's standard deviation.
*
* @return The standard deviation.
*/
val deviation: T
}
17 changes: 17 additions & 0 deletions core/src/commonMain/kotlin/sk/ai/net/Shape.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sk.ai.net

import sk.ai.net.impl.assert
import sk.ai.net.impl.zipFold

class Shape(vararg dimensions: Int) {
Expand All @@ -11,6 +12,22 @@ class Shape(vararg dimensions: Int) {
val rank: Int
get() = dimensions.size

internal fun index(indices: IntArray): Int {
assert(
{ indices.size == dimensions.size },
{ "`indices.size` must be ${dimensions.size}: ${indices.size}" })
return dimensions.zip(indices).fold(0) { a, x ->
assert(
{ 0 <= x.second && x.second < x.first },
{ "Illegal index: indices = ${indices}, shape = $dimensions" })
a * x.first + x.second
}
}

operator fun get(vararg indices: Int): Int {
return dimensions[index(indices)]
}

override fun equals(other: Any?): Boolean {
if (other !is Shape) {
return false
Expand Down
29 changes: 17 additions & 12 deletions core/src/commonMain/kotlin/sk/ai/net/Tensor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ interface Tensor {

fun relu(): Tensor

fun softmax(i: Int): Tensor
fun softmax(dim: Int): Tensor

fun softmax(): Tensor

Expand All @@ -63,30 +63,35 @@ interface Tensor {
fun cos(): Tensor

fun tan(): Tensor

fun asin(): Tensor

fun acos(): Tensor

fun atan(): Tensor

fun sinh():Tensor
fun sinh(): Tensor

fun cosh():Tensor
fun cosh(): Tensor

fun tanh():Tensor
fun tanh(): Tensor

fun exp():Tensor
fun exp(): Tensor

fun log():Tensor
fun log(): Tensor

fun sqrt():Tensor
fun sqrt(): Tensor

fun cbrt():Tensor
fun cbrt(): Tensor

fun sigmoid():Tensor
fun sigmoid(): Tensor

fun ln():Tensor
fun ln(): Tensor

fun flatten(startDim: Int = 1, endDim: Int = -1): Tensor
}

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

21 changes: 20 additions & 1 deletion core/src/commonMain/kotlin/sk/ai/net/TensorFactory.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package sk.ai.net

import sk.ai.net.impl.BuiltInDoubleDataDescriptor
import sk.ai.net.impl.DoublesTensor
import kotlin.random.Random

interface TensorFactory {
fun createTensor(shape: Shape, dataDescriptor: DataDescriptor, elements: DoubleArray): Tensor
}
}

interface DataDescriptorFactory {
fun createDataDescriptor(): DataDescriptor
}


fun rand(shape: Shape, dataDescriptor: DataDescriptor = BuiltInDoubleDataDescriptor()): Tensor {
val random: Random = Random.Default

return DoublesTensor(shape, DoubleArray(shape.volume) { random.nextFloat().toDouble() })
}

fun zeros(shape: Shape, dataDescriptor: DataDescriptor = BuiltInDoubleDataDescriptor()): Tensor {
return DoublesTensor(shape, DoubleArray(shape.volume))
}
15 changes: 15 additions & 0 deletions core/src/commonMain/kotlin/sk/ai/net/core/Slice.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sk.ai.net.core

import sk.ai.net.Tensor

data class Slice(val tensor: Tensor, val dimensionIndex: Int, val startIndex: Long, val endIndex: Long) {
fun toRange() = startIndex..endIndex
}

fun Slice.start() = startIndex

fun Slice.end() = endIndex

fun Slice.all() = startIndex..tensor.shape.dimensions[dimensionIndex]

fun Slice.range(): LongRange = startIndex..endIndex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sk.ai.net.impl
package sk.ai.net.core

import sk.ai.net.Tensor

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

operator fun get(vararg ranges: IntRange): TypedTensor<T>
}

operator fun get(vararg ranges: Slice): Tensor

val allElements: List<T>

}
Loading
Loading