-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/JetBrains/KotlinDL
- Loading branch information
Showing
7 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
api/src/main/kotlin/org/jetbrains/kotlinx/dl/api/core/layer/pooling/GlobalAvgPool1D.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2020 JetBrains s.r.o. and Kotlin Deep Learning project contributors. All Rights Reserved. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package org.jetbrains.kotlinx.dl.api.core.layer.pooling | ||
|
||
import org.jetbrains.kotlinx.dl.api.core.KGraph | ||
import org.jetbrains.kotlinx.dl.api.core.layer.Layer | ||
import org.jetbrains.kotlinx.dl.api.core.util.TF | ||
import org.tensorflow.Operand | ||
import org.tensorflow.Shape | ||
import org.tensorflow.op.Ops | ||
|
||
/** | ||
* Global average pooling operation for temporal data. | ||
* NOTE: Works with tensors which must have rank 3 (batch, steps, features). | ||
* Input shape: 3D tensor with shape `(batch_size, steps, features)`. | ||
* Output shape: 2D tensor with shape `(batch_size, features)`. | ||
* @property [name] Custom layer name. | ||
* @constructor Creates [GlobalAvgPool1D] object. | ||
*/ | ||
public class GlobalAvgPool1D( | ||
name: String = "" | ||
) : Layer(name) { | ||
override fun build(tf: Ops, kGraph: KGraph, inputShape: Shape) { } | ||
|
||
override fun computeOutputShape(inputShape: Shape): Shape { | ||
return Shape.make(inputShape.size(0), inputShape.size(2)) | ||
} | ||
|
||
override fun forward(tf: Ops, input: Operand<Float>, isTraining: Operand<Boolean>, numberOfLosses: Operand<Float>?): Operand<Float> { | ||
// TODO support for different dataFormat("channel_last", "channel_first") | ||
var stepAxis = 1 | ||
// TODO support for masking | ||
return TF.mean(tf, input, tf.constant(stepAxis)) | ||
} | ||
|
||
override val weights: Map<String, Array<*>> get() = emptyMap() | ||
|
||
override val hasActivation: Boolean get() = false | ||
|
||
override val paramCount: Int get() = 0 | ||
|
||
override fun toString(): String { | ||
return "GlobalAvgPool1D(name=$name)" | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
api/src/test/kotlin/org/jetbrains/kotlinx/dl/api/core/layer/GlobalAvgPooling1DTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.jetbrains.kotlinx.dl.api.core.layer | ||
|
||
import org.jetbrains.kotlinx.dl.api.core.layer.pooling.GlobalAvgPool1D | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class GlobalAvgPooling1DTest : PoolLayerTest() { | ||
@Test | ||
fun globalAvgPool1DTest(){ | ||
val input = Array(2, { Array(3, { FloatArray(4) { 0f } } ) } ) | ||
val expected = Array(2, {FloatArray(4) { 0f } }) | ||
assertGlobalAvgPool1DEquals(GlobalAvgPool1D(),input, expected ) | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
api/src/test/kotlin/org/jetbrains/kotlinx/dl/api/core/layer/PoolLayerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.jetbrains.kotlinx.dl.api.core.layer | ||
|
||
import org.jetbrains.kotlinx.dl.api.core.KGraph | ||
import org.jetbrains.kotlinx.dl.api.core.activation.EPS | ||
import org.jetbrains.kotlinx.dl.api.core.shape.* | ||
import org.junit.jupiter.api.Assertions.assertArrayEquals | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.tensorflow.EagerSession | ||
import org.tensorflow.Graph | ||
import org.tensorflow.* | ||
import org.tensorflow.op.Ops | ||
|
||
open class PoolLayerTest { | ||
protected fun assertGlobalAvgPool1DEquals( | ||
layer: Layer, | ||
input:Array<Array<FloatArray>>, | ||
expected: Array<FloatArray>, | ||
) { | ||
val actual = Array(expected.size) {FloatArray(expected[0].size) { 0.toFloat() } } | ||
assertPoolingLayer(layer,input, expected,actual,::assertGlobalAvgPool1DEquals) | ||
} | ||
|
||
private fun assertPoolingLayer( | ||
layer: Layer, | ||
input:Array<Array<FloatArray>>, | ||
expected: Array<FloatArray>, | ||
actual:Array<FloatArray>, | ||
assertEqual: (Array<FloatArray>, Array<FloatArray>)->Unit, | ||
){ | ||
val inputSize = input.size | ||
val inputShape = Shape.make(inputSize.toLong()) | ||
EagerSession.create().use { | ||
val tf = Ops.create(it) | ||
val inputOp = tf.constant(input) | ||
layer.build(tf, KGraph(Graph().toGraphDef()), inputShape) | ||
val isTraining = tf.constant(true) | ||
val numberOfLosses = tf.constant(1.0f) | ||
val output = layer.forward(tf, inputOp, isTraining, numberOfLosses).asOutput().tensor() | ||
|
||
val expectedShape = Shape.make( | ||
expected.size.toLong(), | ||
expected[0].size.toLong() | ||
) | ||
|
||
val actualShape = shapeFromDims(*output.shape()) | ||
output.copyTo(actual) | ||
assertEquals(expectedShape, actualShape) | ||
assertEqual(expected,actual) | ||
} | ||
} | ||
|
||
private fun assertGlobalAvgPool1DEquals( | ||
expected: Array<FloatArray>, | ||
actual: Array<FloatArray> | ||
) { | ||
val expectedTensor = expected | ||
val actualTensor = actual | ||
val msg = "Expected ${expectedTensor.contentDeepToString()} " + | ||
"to equal ${actualTensor.contentDeepToString()}" | ||
for (i in expectedTensor.indices) { | ||
assertArrayEquals(expectedTensor[i], actualTensor[i], EPS, msg) | ||
} | ||
} | ||
} |