-
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.
* Add AvgPool1D layer * Remove dataFormat; Update poolSize and strides types; Update with recent changes * Change strides property type and default value
- Loading branch information
Showing
5 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
api/src/main/kotlin/org/jetbrains/kotlinx/dl/api/core/layer/pooling/AvgPool1D.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,90 @@ | ||
/* | ||
* 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.layer.convolutional.ConvPadding | ||
import org.jetbrains.kotlinx.dl.api.core.shape.convOutputLength | ||
import org.tensorflow.Operand | ||
import org.tensorflow.Shape | ||
import org.tensorflow.op.Ops | ||
import org.tensorflow.op.core.Squeeze | ||
|
||
/** | ||
* Average pooling operation for 1D temporal data (e.g. audio, timseries). | ||
* | ||
* Downsamples the input by taking the average over a temporal window of size [poolSize]. | ||
* | ||
* @property [poolSize] Size of the temporal pooling window for each dimension of input. | ||
* @property [strides] The amount of shift for pooling window per each input dimension in each pooling step. | ||
* @property [padding] Padding strategy; can be either of [ConvPadding.VALID] which means no | ||
* padding, or [ConvPadding.SAME] which means padding the input equally such that the output | ||
* has the same dimension as the input. | ||
*/ | ||
public class AvgPool1D( | ||
public val poolSize: LongArray = longArrayOf(1, 2, 1), | ||
public val strides: LongArray = longArrayOf(1, 2, 1), | ||
public val padding: ConvPadding = ConvPadding.VALID, | ||
name: String = "" | ||
) : Layer(name) { | ||
|
||
override val hasActivation: Boolean | ||
get() = false | ||
override val paramCount: Int | ||
get() = 0 | ||
override var weights: Map<String, Array<*>> | ||
get() = emptyMap() | ||
set(value) = assignWeights(value) | ||
|
||
init { | ||
require(poolSize.size == 3) { | ||
"The poolSize should be an array of size 3." | ||
} | ||
|
||
require(strides.size == 3) { | ||
"The strides should be an array of size 3." | ||
} | ||
|
||
require(padding == ConvPadding.VALID || padding == ConvPadding.SAME) { | ||
"The padding should be either of ${ConvPadding.VALID} or ${ConvPadding.SAME}." | ||
} | ||
} | ||
|
||
override fun build(tf: Ops, kGraph: KGraph, inputShape: Shape) {} | ||
|
||
override fun computeOutputShape(inputShape: Shape): Shape { | ||
var steps = inputShape.size(1) | ||
steps = convOutputLength(steps, poolSize[1].toInt(), padding, strides[1].toInt()) | ||
return Shape.make(inputShape.size(0), steps, inputShape.size(2)) | ||
} | ||
|
||
override fun forward( | ||
tf: Ops, | ||
input: Operand<Float>, | ||
isTraining: Operand<Boolean>, | ||
numberOfLosses: Operand<Float>? | ||
): Operand<Float> { | ||
val expandAxis = 2 | ||
val tfInput = tf.expandDims(input, tf.constant(expandAxis)) | ||
val tfPoolSize = longArrayOf(1, 1, 1, 1) | ||
val tfStrides = longArrayOf(1, 1, 1, 1) | ||
tfPoolSize[expandAxis-1] = poolSize[1] | ||
tfStrides[expandAxis-1] = strides[1] | ||
val tfPadding = padding.paddingName | ||
|
||
val avgPool = tf.nn.avgPool( | ||
tfInput, | ||
tfPoolSize.toList(), | ||
tfStrides.toList(), | ||
tfPadding | ||
) | ||
return tf.squeeze(avgPool, Squeeze.axis(listOf(expandAxis.toLong()))) | ||
} | ||
|
||
override fun toString(): String = | ||
"AvgPool1D(poolSize=$poolSize, strides=$strides, padding=$padding)" | ||
} |
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
205 changes: 205 additions & 0 deletions
205
api/src/test/kotlin/org/jetbrains/kotlinx/dl/api/core/layer/AvgPool1DTest.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,205 @@ | ||
/* | ||
* 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 | ||
|
||
import org.jetbrains.kotlinx.dl.api.core.KGraph | ||
import org.jetbrains.kotlinx.dl.api.core.layer.convolutional.ConvPadding | ||
import org.jetbrains.kotlinx.dl.api.core.layer.pooling.AvgPool1D | ||
import org.jetbrains.kotlinx.dl.api.core.shape.toIntArray | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
import org.tensorflow.EagerSession | ||
import org.tensorflow.Graph | ||
import org.tensorflow.Shape | ||
import org.tensorflow.op.Ops | ||
|
||
const val EPS: Float = 1e-6f | ||
|
||
internal class AvgPool1DTest { | ||
@Test | ||
fun default() { | ||
val input = arrayOf( | ||
arrayOf( | ||
floatArrayOf(1.0f, -2.0f, 3.0f), | ||
floatArrayOf(0.5f, 2.0f, 5.0f), | ||
floatArrayOf(-1.0f, 3.0f, 2.0f), | ||
floatArrayOf(1.5f, -1.0f, 0.5f) | ||
), | ||
arrayOf( | ||
floatArrayOf(5.0f, 3.0f, 1.0f), | ||
floatArrayOf(6.0f, -2.5f, 4.0f), | ||
floatArrayOf(7.0f, 0.0f, 5.0f), | ||
floatArrayOf(1.0f, 2.0f, 4.0f) | ||
), | ||
) | ||
val expected = arrayOf( | ||
arrayOf( | ||
floatArrayOf(0.75f, 0.0f, 4.0f), | ||
floatArrayOf(0.25f, 1.0f, 1.25f) | ||
), | ||
arrayOf( | ||
floatArrayOf(5.5f, 0.25f, 2.5f), | ||
floatArrayOf(4.0f, 1.0f, 4.5f) | ||
) | ||
) | ||
val layer = AvgPool1D() | ||
|
||
EagerSession.create().use { | ||
val tf = Ops.create() | ||
val inputShape = Shape.make(input.size.toLong(), input[0].size.toLong(), input[0][0].size.toLong()) | ||
layer.build(tf, KGraph(Graph().toGraphDef()), inputShape) | ||
|
||
val inputOp = tf.constant(input) | ||
val isTraining = tf.constant(true) | ||
val numberOfLosses = tf.constant(1.0f) | ||
val output = layer.forward(tf, inputOp, isTraining, numberOfLosses).asOutput() | ||
|
||
// Check output shape is correct. | ||
val expectedShape = intArrayOf(input.size, 2, input[0][0].size) | ||
Assertions.assertArrayEquals( | ||
expectedShape, | ||
output.shape().toIntArray() | ||
) | ||
|
||
// Check output values are correct. | ||
val actual = Array(input.size) { Array(2) { FloatArray(input[0][0].size) } } | ||
output.tensor().copyTo(actual) | ||
for (i in expected.indices) { | ||
for (j in expected[i].indices) { | ||
Assertions.assertArrayEquals( | ||
expected[i][j], | ||
actual[i][j], | ||
EPS | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun withPaddingAndStride() { | ||
val input = arrayOf( | ||
arrayOf( | ||
floatArrayOf(1.0f, -2.0f, 3.0f), | ||
floatArrayOf(0.5f, 2.0f, 5.0f), | ||
floatArrayOf(-1.0f, 3.0f, 2.0f), | ||
floatArrayOf(1.5f, -1.0f, 0.5f) | ||
), | ||
arrayOf( | ||
floatArrayOf(5.0f, 3.0f, 1.0f), | ||
floatArrayOf(6.0f, -2.5f, 4.0f), | ||
floatArrayOf(7.0f, 0.0f, 5.0f), | ||
floatArrayOf(1.0f, 2.0f, 4.0f) | ||
), | ||
) | ||
val expected = arrayOf( | ||
arrayOf( | ||
floatArrayOf(0.75f, 0.0f, 4.0f), | ||
floatArrayOf(-0.25f, 2.5f, 3.5f), | ||
floatArrayOf(0.25f, 1.0f, 1.25f), | ||
floatArrayOf(1.5f, -1.0f, 0.5f), | ||
), | ||
arrayOf( | ||
floatArrayOf(5.5f, 0.25f, 2.5f), | ||
floatArrayOf(6.5f, -1.25f, 4.5f), | ||
floatArrayOf(4.0f, 1.0f, 4.5f), | ||
floatArrayOf(1.0f, 2.0f, 4.0f) | ||
) | ||
) | ||
val layer = AvgPool1D(strides = longArrayOf(1, 1, 1), padding = ConvPadding.SAME) | ||
|
||
EagerSession.create().use { | ||
val tf = Ops.create() | ||
val inputShape = Shape.make(input.size.toLong(), input[0].size.toLong(), input[0][0].size.toLong()) | ||
layer.build(tf, KGraph(Graph().toGraphDef()), inputShape) | ||
|
||
val inputOp = tf.constant(input) | ||
val isTraining = tf.constant(true) | ||
val numberOfLosses = tf.constant(1.0f) | ||
val output = layer.forward(tf, inputOp, isTraining, numberOfLosses).asOutput() | ||
|
||
// Check output shape is correct. | ||
val expectedShape = intArrayOf(input.size, input[0].size, input[0][0].size) | ||
Assertions.assertArrayEquals( | ||
expectedShape, | ||
output.shape().toIntArray() | ||
) | ||
|
||
// Check output values are correct. | ||
val actual = Array(input.size) { Array(input[0].size) { FloatArray(input[0][0].size) } } | ||
output.tensor().copyTo(actual) | ||
for (i in expected.indices) { | ||
for (j in expected[i].indices) { | ||
Assertions.assertArrayEquals( | ||
expected[i][j], | ||
actual[i][j], | ||
EPS | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun withPoolSizeAndStride() { | ||
val input = arrayOf( | ||
arrayOf( | ||
floatArrayOf(1.0f, -2.0f, 3.0f), | ||
floatArrayOf(0.5f, 2.0f, 5.0f), | ||
floatArrayOf(-1.0f, 3.0f, 2.0f), | ||
floatArrayOf(1.5f, -1.0f, 0.5f) | ||
), | ||
arrayOf( | ||
floatArrayOf(5.0f, 3.0f, 1.0f), | ||
floatArrayOf(6.0f, -2.5f, 4.0f), | ||
floatArrayOf(7.0f, 0.0f, 5.0f), | ||
floatArrayOf(1.0f, 2.0f, 4.0f) | ||
), | ||
) | ||
val expected = arrayOf( | ||
arrayOf( | ||
floatArrayOf(0.5f/3, 1.0f, 10.0f/3), | ||
floatArrayOf(1.0f/3, 4.0f/3, 2.5f), | ||
), | ||
arrayOf( | ||
floatArrayOf(6.0f, 0.5f/3, 10.0f/3), | ||
floatArrayOf(14.0f/3, -0.5f/3, 13.0f/3), | ||
) | ||
) | ||
val layer = AvgPool1D(poolSize = longArrayOf(1, 3, 1), strides = longArrayOf(1, 1, 1)) | ||
|
||
EagerSession.create().use { | ||
val tf = Ops.create() | ||
val inputShape = Shape.make(input.size.toLong(), input[0].size.toLong(), input[0][0].size.toLong()) | ||
layer.build(tf, KGraph(Graph().toGraphDef()), inputShape) | ||
|
||
val inputOp = tf.constant(input) | ||
val isTraining = tf.constant(true) | ||
val numberOfLosses = tf.constant(1.0f) | ||
val output = layer.forward(tf, inputOp, isTraining, numberOfLosses).asOutput() | ||
|
||
// Check output shape is correct. | ||
val expectedShape = intArrayOf(input.size, 2, input[0][0].size) | ||
Assertions.assertArrayEquals( | ||
expectedShape, | ||
output.shape().toIntArray() | ||
) | ||
|
||
// Check output values are correct. | ||
val actual = Array(input.size) { Array(2) { FloatArray(input[0][0].size) } } | ||
output.tensor().copyTo(actual) | ||
for (i in expected.indices) { | ||
for (j in expected[i].indices) { | ||
Assertions.assertArrayEquals( | ||
expected[i][j], | ||
actual[i][j], | ||
EPS | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |