0.3.0-dev-8. Cleanup

This commit is contained in:
Alexander Nozik 2021-05-08 14:20:16 +03:00
parent e4e661a3bf
commit 7ce0829597
7 changed files with 19 additions and 18 deletions

View File

@ -35,6 +35,7 @@
- `contentEquals` from Buffer. It moved to the companion. - `contentEquals` from Buffer. It moved to the companion.
- MSTExpression - MSTExpression
- Expression algebra builders - Expression algebra builders
- Comples and Quaternion no longer are elements.
### Fixed ### Fixed
- Ring inherits RingOperations, not GroupOperations - Ring inherits RingOperations, not GroupOperations

View File

@ -15,7 +15,7 @@ allprojects {
} }
group = "space.kscience" group = "space.kscience"
version = "0.3.0-dev-7" version = "0.3.0-dev-8"
} }
subprojects { subprojects {

View File

@ -6,8 +6,8 @@
package space.kscience.kmath.tensors package space.kscience.kmath.tensors
import space.kscience.kmath.operations.invoke import space.kscience.kmath.operations.invoke
import space.kscience.kmath.tensors.core.DoubleTensor
import space.kscience.kmath.tensors.core.BroadcastDoubleTensorAlgebra import space.kscience.kmath.tensors.core.BroadcastDoubleTensorAlgebra
import space.kscience.kmath.tensors.core.DoubleTensor
import space.kscience.kmath.tensors.core.DoubleTensorAlgebra import space.kscience.kmath.tensors.core.DoubleTensorAlgebra
import space.kscience.kmath.tensors.core.toDoubleArray import space.kscience.kmath.tensors.core.toDoubleArray
import kotlin.math.sqrt import kotlin.math.sqrt
@ -106,6 +106,7 @@ fun accuracy(yPred: DoubleTensor, yTrue: DoubleTensor): Double {
} }
// neural network class // neural network class
@OptIn(ExperimentalStdlibApi::class)
class NeuralNetwork(private val layers: List<Layer>) { class NeuralNetwork(private val layers: List<Layer>) {
private fun softMaxLoss(yPred: DoubleTensor, yTrue: DoubleTensor): DoubleTensor = BroadcastDoubleTensorAlgebra { private fun softMaxLoss(yPred: DoubleTensor, yTrue: DoubleTensor): DoubleTensor = BroadcastDoubleTensorAlgebra {
@ -120,7 +121,7 @@ class NeuralNetwork(private val layers: List<Layer>) {
(-onesForAnswers + softmaxValue) / (yPred.shape[0].toDouble()) (-onesForAnswers + softmaxValue) / (yPred.shape[0].toDouble())
} }
@OptIn(ExperimentalStdlibApi::class)
private fun forward(x: DoubleTensor): List<DoubleTensor> { private fun forward(x: DoubleTensor): List<DoubleTensor> {
var input = x var input = x
@ -133,7 +134,6 @@ class NeuralNetwork(private val layers: List<Layer>) {
} }
} }
@OptIn(ExperimentalStdlibApi::class)
private fun train(xTrain: DoubleTensor, yTrain: DoubleTensor) { private fun train(xTrain: DoubleTensor, yTrain: DoubleTensor) {
val layerInputs = buildList { val layerInputs = buildList {
add(xTrain) add(xTrain)
@ -180,7 +180,7 @@ fun main() {
val features = 5 val features = 5
val sampleSize = 250 val sampleSize = 250
val trainSize = 180 val trainSize = 180
val testSize = sampleSize - trainSize //val testSize = sampleSize - trainSize
// take sample of features from normal distribution // take sample of features from normal distribution
val x = randomNormal(intArrayOf(sampleSize, features), seed) * 2.5 val x = randomNormal(intArrayOf(sampleSize, features), seed) * 2.5

View File

@ -48,7 +48,7 @@ fun main() {
// inverse Sigma matrix can be restored from singular values with diagonalEmbedding function // inverse Sigma matrix can be restored from singular values with diagonalEmbedding function
val sigma = diagonalEmbedding(singValues.map{ x -> if (abs(x) < 1e-3) 0.0 else 1.0/x }) val sigma = diagonalEmbedding(singValues.map{ if (abs(it) < 1e-3) 0.0 else 1.0/it })
val alphaOLS = v dot sigma dot u.transpose() dot y val alphaOLS = v dot sigma dot u.transpose() dot y
println("Estimated alpha:\n" + println("Estimated alpha:\n" +

View File

@ -9,7 +9,10 @@ import space.kscience.kmath.memory.MemoryReader
import space.kscience.kmath.memory.MemorySpec import space.kscience.kmath.memory.MemorySpec
import space.kscience.kmath.memory.MemoryWriter import space.kscience.kmath.memory.MemoryWriter
import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.* import space.kscience.kmath.operations.ExtendedField
import space.kscience.kmath.operations.Norm
import space.kscience.kmath.operations.NumbersAddOperations
import space.kscience.kmath.operations.ScaleOperations
import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.MemoryBuffer import space.kscience.kmath.structures.MemoryBuffer
import space.kscience.kmath.structures.MutableBuffer import space.kscience.kmath.structures.MutableBuffer
@ -180,12 +183,10 @@ public object ComplexField : ExtendedField<Complex>, Norm<Complex, Complex>, Num
* @property im The imaginary part. * @property im The imaginary part.
*/ */
@OptIn(UnstableKMathAPI::class) @OptIn(UnstableKMathAPI::class)
public data class Complex(val re: Double, val im: Double) : FieldElement<Complex, ComplexField> { public data class Complex(val re: Double, val im: Double) {
public constructor(re: Number, im: Number) : this(re.toDouble(), im.toDouble()) public constructor(re: Number, im: Number) : this(re.toDouble(), im.toDouble())
public constructor(re: Number) : this(re.toDouble(), 0.0) public constructor(re: Number) : this(re.toDouble(), 0.0)
public override val context: ComplexField get() = ComplexField
public override fun toString(): String = "($re + i*$im)" public override fun toString(): String = "($re + i*$im)"
public companion object : MemorySpec<Complex> { public companion object : MemorySpec<Complex> {

View File

@ -27,9 +27,11 @@ public val Quaternion.conjugate: Quaternion
*/ */
public val Quaternion.reciprocal: Quaternion public val Quaternion.reciprocal: Quaternion
get() { get() {
val n = QuaternionField { norm(this@reciprocal) } QuaternionField {
val n = norm(this@reciprocal)
return conjugate / (n * n) return conjugate / (n * n)
} }
}
/** /**
* Absolute value of the quaternion. * Absolute value of the quaternion.
@ -198,7 +200,7 @@ public object QuaternionField : Field<Quaternion>, Norm<Quaternion, Quaternion>,
@OptIn(UnstableKMathAPI::class) @OptIn(UnstableKMathAPI::class)
public data class Quaternion( public data class Quaternion(
val w: Double, val x: Double, val y: Double, val z: Double, val w: Double, val x: Double, val y: Double, val z: Double,
) : FieldElement<Quaternion, QuaternionField> { ) {
public constructor(w: Number, x: Number, y: Number, z: Number) : this( public constructor(w: Number, x: Number, y: Number, z: Number) : this(
w.toDouble(), w.toDouble(),
x.toDouble(), x.toDouble(),
@ -219,8 +221,6 @@ public data class Quaternion(
require(!z.isNaN()) { "x-component of quaternion is not-a-number" } require(!z.isNaN()) { "x-component of quaternion is not-a-number" }
} }
public override val context: QuaternionField get() = QuaternionField
/** /**
* Returns a string representation of this quaternion. * Returns a string representation of this quaternion.
*/ */

View File

@ -5,15 +5,14 @@
package space.kscience.kmath.stat package space.kscience.kmath.stat
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.launch
import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import space.kscience.kmath.samplers.GaussianSampler import space.kscience.kmath.samplers.GaussianSampler
internal class CommonsDistributionsTest { internal class CommonsDistributionsTest {
@Test @Test
fun testNormalDistributionSuspend() = GlobalScope.launch { fun testNormalDistributionSuspend() = runBlocking {
val distribution = GaussianSampler(7.0, 2.0) val distribution = GaussianSampler(7.0, 2.0)
val generator = RandomGenerator.default(1) val generator = RandomGenerator.default(1)
val sample = distribution.sample(generator).nextBuffer(1000) val sample = distribution.sample(generator).nextBuffer(1000)