Refactoring/optimisations +tesing on Iris
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+11
-7
@@ -7,8 +7,8 @@ var petal_length_max = 0.0
|
|||||||
var petal_width_max = 0.0
|
var petal_width_max = 0.0
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
val file = File("/run/media/sweetbread/50AF29954CE66E9F/Coding/Kotlin/AI/src/main/resources/IRIS.csv")
|
val file = File("src/main/resources/Iris.csv")
|
||||||
val data: List<Iris> = csvReader().readAll(file).drop(1).map { Iris(it[0].toDouble(), it[1].toDouble(), it[2].toDouble(), it[3].toDouble(), it[4]) } .shuffled()
|
val data: List<Iris> = csvReader().readAll(file).drop(1).map { Iris(it[1].toDouble(), it[2].toDouble(), it[3].toDouble(), it[4].toDouble(), it[5]) } .shuffled()
|
||||||
data.forEach {
|
data.forEach {
|
||||||
if (it.sepal_length > sepal_length_max) sepal_length_max = it.sepal_length
|
if (it.sepal_length > sepal_length_max) sepal_length_max = it.sepal_length
|
||||||
if (it.sepal_width > sepal_width_max) sepal_width_max = it.sepal_width
|
if (it.sepal_width > sepal_width_max) sepal_width_max = it.sepal_width
|
||||||
@@ -20,13 +20,17 @@ fun main() {
|
|||||||
Layer(4),
|
Layer(4),
|
||||||
Layer(4),
|
Layer(4),
|
||||||
Layer(3, false)
|
Layer(3, false)
|
||||||
))
|
), .2)
|
||||||
println(model.output())
|
println(model.output())
|
||||||
|
|
||||||
model.teach(data.subList(0, 50).map { it.data_field() }.toTypedArray(), 100, false)
|
model.teach(data.subList(0, 50).map { it.data_field() }.toTypedArray(), 200, false)
|
||||||
data[100].data_field()[1].forEach { print(it.toInt()); print(" ") }
|
var errors = 0
|
||||||
println()
|
data.forEach {
|
||||||
model.input(data[100].data_field()[0]); println(model.output())
|
val cor = it.data_field()[1].map{ it.toInt() }; model.input(it.data_field()[0])
|
||||||
|
println("Correct: $cor, out: ${model.output_int()}, ${cor==model.output_int()}")
|
||||||
|
if (cor!=model.output_int()) errors++
|
||||||
|
}
|
||||||
|
println((errors.toDouble()/data.size*100).toInt().toString()+"%")
|
||||||
}
|
}
|
||||||
|
|
||||||
class Iris (
|
class Iris (
|
||||||
|
|||||||
@@ -2,8 +2,13 @@ import kotlin.math.pow
|
|||||||
import kotlin.properties.Delegates
|
import kotlin.properties.Delegates
|
||||||
|
|
||||||
class Perceptron (private val layers: Array<Layer>, private val k: Double = 0.5) {
|
class Perceptron (private val layers: Array<Layer>, private val k: Double = 0.5) {
|
||||||
|
val input_layer: Layer
|
||||||
|
val output_layer: Layer
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
input_layer = layers.first()
|
||||||
|
output_layer = layers.last()
|
||||||
|
|
||||||
layers.mapIndexed { index, layer -> layer.position = index }
|
layers.mapIndexed { index, layer -> layer.position = index }
|
||||||
for (i in 0..layers.size-2) {
|
for (i in 0..layers.size-2) {
|
||||||
println(i)
|
println(i)
|
||||||
@@ -12,17 +17,17 @@ class Perceptron (private val layers: Array<Layer>, private val k: Double = 0.5)
|
|||||||
this.count()
|
this.count()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun count() { for (layer in layers) { layer.count() } }
|
private fun count() { layers.forEach { it.count() }}
|
||||||
|
|
||||||
fun input(array: Array<Double>) {
|
fun input(array: Array<Double>) {
|
||||||
for (index in 0 until layers[0].nodes.dropLast(1).size) {
|
input_layer.nodes.mapIndexed { index, node ->
|
||||||
layers[0].nodes[index].valuE = array[index]
|
if (node !is Bias) node.valuE = array[index]
|
||||||
}
|
}
|
||||||
this.count()
|
this.count()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun backPropagation(input: Array<Double>) {
|
private fun backPropagation(input: Array<Double>) {
|
||||||
input.mapIndexed { i, v -> layers.last().nodes[i].error = v - layers.last().nodes[i].valuE }
|
output_layer.nodes.mapIndexed { index, node -> node.error = input[index] - node.valuE }
|
||||||
for (layer in layers.drop(1).dropLast(1).reversed()) {
|
for (layer in layers.drop(1).dropLast(1).reversed()) {
|
||||||
for (node in layer.nodes) {
|
for (node in layer.nodes) {
|
||||||
node.error = 0.toDouble()
|
node.error = 0.toDouble()
|
||||||
@@ -57,6 +62,12 @@ class Perceptron (private val layers: Array<Layer>, private val k: Double = 0.5)
|
|||||||
for (node in layers.last().nodes) { output += node.valuE }
|
for (node in layers.last().nodes) { output += node.valuE }
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun output_int(): List<Int> {
|
||||||
|
var output = listOf<Int>()
|
||||||
|
for (node in layers.last().nodes) { output += if (node.valuE > 0.5) 1 else 0 }
|
||||||
|
return output
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
open class Node(val parent: Layer, private val position: Int) {
|
open class Node(val parent: Layer, private val position: Int) {
|
||||||
@@ -88,7 +99,7 @@ class Bias(parent: Layer, position: Int) : Node(parent, position) {
|
|||||||
override fun getValue() = 1.toDouble()
|
override fun getValue() = 1.toDouble()
|
||||||
}
|
}
|
||||||
|
|
||||||
class Layer(amount: Int, bias: Boolean = true) {
|
open class Layer(amount: Int, bias: Boolean = true) {
|
||||||
var position by Delegates.notNull<Int>()
|
var position by Delegates.notNull<Int>()
|
||||||
var nodes = listOf<Node>()
|
var nodes = listOf<Node>()
|
||||||
var nextWeight: Weight? = null
|
var nextWeight: Weight? = null
|
||||||
|
|||||||
Reference in New Issue
Block a user