Browse Source

keras average pooling #870

tags/TimeSeries
Oceania2018 3 years ago
parent
commit
0a5e1815e5
7 changed files with 125 additions and 3 deletions
  1. +7
    -0
      src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/AveragePooling2DArgs.cs
  2. +0
    -0
      src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/MaxPooling2DArgs.cs
  3. +47
    -0
      src/TensorFlowNET.Core/Operations/NnOps/AveragePoolFunction.cs
  4. +15
    -0
      src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs
  5. +20
    -0
      src/TensorFlowNET.Keras/Layers/LayersApi.cs
  6. +14
    -0
      src/TensorFlowNET.Keras/Layers/Pooling/AveragePooling2D.cs
  7. +22
    -3
      test/TensorFlowNET.Keras.UnitTest/Layers/LayersTest.cs

+ 7
- 0
src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/AveragePooling2DArgs.cs View File

@@ -0,0 +1,7 @@
namespace Tensorflow.Keras.ArgsDefinition
{
public class AveragePooling2DArgs : Pooling2DArgs
{

}
}

src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/MaxPooling2D.cs → src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/MaxPooling2DArgs.cs View File


+ 47
- 0
src/TensorFlowNET.Core/Operations/NnOps/AveragePoolFunction.cs View File

@@ -0,0 +1,47 @@
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/

using static Tensorflow.Binding;

namespace Tensorflow.Operations
{
/// <summary>
/// Performs the average pooling on the input.
/// </summary>
public class AveragePoolFunction : IPoolFunction
{
public Tensor Apply(Tensor value,
int[] ksize,
int[] strides,
string padding,
string data_format = "NHWC",
string name = null)
{
return tf_with(ops.name_scope(name, "AveragePool", value), scope =>
{
name = scope;
value = ops.convert_to_tensor(value, name: "input");
return gen_nn_ops.average_pool(
value,
ksize: ksize,
strides: strides,
padding: padding,
data_format: data_format,
name: name);
});
}
}
}

+ 15
- 0
src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs View File

@@ -255,6 +255,21 @@ namespace Tensorflow.Operations
=> tf.Context.ExecuteOp("LeakyRelu", name,
new ExecuteOpArgs(features).SetAttributes(new { alpha }));

public static Tensor average_pool(Tensor input,
int[] ksize,
int[] strides,
string padding,
string data_format = "NHWC",
string name = null)
=> tf.Context.ExecuteOp("AvgPool", name, new ExecuteOpArgs(input)
.SetAttributes(new
{
ksize,
strides,
padding,
data_format
}));

public static Tensor max_pool(Tensor input,
int[] ksize,
int[] strides,


+ 20
- 0
src/TensorFlowNET.Keras/Layers/LayersApi.cs View File

@@ -531,6 +531,26 @@ namespace Tensorflow.Keras.Layers
Ragged = ragged
});

/// <summary>
/// Average pooling operation for spatial data.
/// </summary>
/// <param name="pool_size"></param>
/// <param name="strides"></param>
/// <param name="padding"></param>
/// <param name="data_format"></param>
/// <returns></returns>
public AveragePooling2D AveragePooling2D(Shape pool_size = null,
Shape strides = null,
string padding = "valid",
string data_format = null)
=> new AveragePooling2D(new AveragePooling2DArgs
{
PoolSize = pool_size ?? (2, 2),
Strides = strides,
Padding = padding,
DataFormat = data_format
});

/// <summary>
/// Max pooling operation for 1D temporal data.
/// </summary>


+ 14
- 0
src/TensorFlowNET.Keras/Layers/Pooling/AveragePooling2D.cs View File

@@ -0,0 +1,14 @@
using Tensorflow.Keras.ArgsDefinition;
using Tensorflow.Operations;

namespace Tensorflow.Keras.Layers
{
public class AveragePooling2D : Pooling2D
{
public AveragePooling2D(AveragePooling2DArgs args)
: base(args)
{
args.PoolFunction = new AveragePoolFunction();
}
}
}

+ 22
- 3
test/TensorFlowNET.Keras.UnitTest/Layers/LayersTest.cs View File

@@ -15,7 +15,24 @@ namespace TensorFlowNET.Keras.UnitTest
[TestClass]
public class LayersTest : EagerModeTestBase
{
// [TestMethod]
[TestMethod]
public void AveragePooling2D()
{
var x = tf.constant(new float[,]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
});
x = tf.reshape(x, (1, 3, 3, 1));
var avg_pool_2d = keras.layers.AveragePooling2D(pool_size: (2, 2),
strides: (1, 1), padding: "valid");
Tensor avg = avg_pool_2d.Apply(x);
Assert.AreEqual((1, 2, 2, 1), avg.shape);
Equal(new float[] { 3, 4, 6, 7 }, avg.ToArray<float>());
}

[TestMethod]
public void InputLayer()
{
var model = keras.Sequential(new List<ILayer>
@@ -23,8 +40,10 @@ namespace TensorFlowNET.Keras.UnitTest
keras.layers.InputLayer(input_shape: 4),
keras.layers.Dense(8)
});
model.compile(optimizer: keras.optimizers.RMSprop(0.001f));
model.fit(np.zeros((10, 4)), np.ones((10, 8)));
model.compile(optimizer: keras.optimizers.RMSprop(0.001f),
loss: keras.losses.MeanSquaredError(),
metrics: new[] { "accuracy" });
model.fit(np.zeros((10, 4), dtype: tf.float32), np.ones((10, 8), dtype: tf.float32));
}

[TestMethod]


Loading…
Cancel
Save