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.
- MSTExpression
- Expression algebra builders
- Comples and Quaternion no longer are elements.
### Fixed
- Ring inherits RingOperations, not GroupOperations

View File

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

View File

@ -6,8 +6,8 @@
package space.kscience.kmath.tensors
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.DoubleTensor
import space.kscience.kmath.tensors.core.DoubleTensorAlgebra
import space.kscience.kmath.tensors.core.toDoubleArray
import kotlin.math.sqrt
@ -106,6 +106,7 @@ fun accuracy(yPred: DoubleTensor, yTrue: DoubleTensor): Double {
}
// neural network class
@OptIn(ExperimentalStdlibApi::class)
class NeuralNetwork(private val layers: List<Layer>) {
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())
}
@OptIn(ExperimentalStdlibApi::class)
private fun forward(x: DoubleTensor): List<DoubleTensor> {
var input = x
@ -133,7 +134,6 @@ class NeuralNetwork(private val layers: List<Layer>) {
}
}
@OptIn(ExperimentalStdlibApi::class)
private fun train(xTrain: DoubleTensor, yTrain: DoubleTensor) {
val layerInputs = buildList {
add(xTrain)
@ -180,7 +180,7 @@ fun main() {
val features = 5
val sampleSize = 250
val trainSize = 180
val testSize = sampleSize - trainSize
//val testSize = sampleSize - trainSize
// take sample of features from normal distribution
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
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
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.MemoryWriter
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.MemoryBuffer
import space.kscience.kmath.structures.MutableBuffer
@ -180,12 +183,10 @@ public object ComplexField : ExtendedField<Complex>, Norm<Complex, Complex>, Num
* @property im The imaginary part.
*/
@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) : this(re.toDouble(), 0.0)
public override val context: ComplexField get() = ComplexField
public override fun toString(): String = "($re + i*$im)"
public companion object : MemorySpec<Complex> {

View File

@ -27,8 +27,10 @@ public val Quaternion.conjugate: Quaternion
*/
public val Quaternion.reciprocal: Quaternion
get() {
val n = QuaternionField { norm(this@reciprocal) }
return conjugate / (n * n)
QuaternionField {
val n = norm(this@reciprocal)
return conjugate / (n * n)
}
}
/**
@ -198,7 +200,7 @@ public object QuaternionField : Field<Quaternion>, Norm<Quaternion, Quaternion>,
@OptIn(UnstableKMathAPI::class)
public data class Quaternion(
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(
w.toDouble(),
x.toDouble(),
@ -219,8 +221,6 @@ public data class Quaternion(
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.
*/

View File

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