Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make the initialization of the layer's name correct #1148

Merged
merged 2 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/TensorFlowNET.Keras/Utils/generic_utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ limitations under the License.
using Tensorflow.Keras.Layers;
using Tensorflow.Keras.Saving;
using Tensorflow.Train;
using System.Text.RegularExpressions;

namespace Tensorflow.Keras.Utils
{
Expand Down Expand Up @@ -126,12 +127,15 @@ public static FunctionalConfig deserialize_model_config(JToken json)

public static string to_snake_case(string name)
{
return string.Concat(name.Select((x, i) =>
string intermediate = Regex.Replace(name, "(.)([A-Z][a-z0-9]+)", "$1_$2");
string insecure = Regex.Replace(intermediate, "([a-z])([A-Z])", "$1_$2").ToLower();

if (insecure[0] != '_')
{
return i > 0 && char.IsUpper(x) && !Char.IsDigit(name[i - 1]) ?
"_" + x.ToString() :
x.ToString();
})).ToLower();
return insecure;
}

return "private" + insecure;
}

/// <summary>
Expand Down
33 changes: 33 additions & 0 deletions test/TensorFlowNET.Keras.UnitTest/InitLayerNameTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tensorflow.Keras.Layers;
using static Tensorflow.Binding;
using static Tensorflow.KerasApi;

namespace Tensorflow.Keras.UnitTest
{
[TestClass]
public class InitLayerNameTest
{
[TestMethod]
public void RNNLayerNameTest()
{
var simpleRnnCell = keras.layers.SimpleRNNCell(1);
Assert.AreEqual("simple_rnn_cell", simpleRnnCell.Name);
var simpleRnn = keras.layers.SimpleRNN(2);
Assert.AreEqual("simple_rnn", simpleRnn.Name);
var lstmCell = keras.layers.LSTMCell(2);
Assert.AreEqual("lstm_cell", lstmCell.Name);
var lstm = keras.layers.LSTM(3);
Assert.AreEqual("lstm", lstm.Name);
}

[TestMethod]
public void ConvLayerNameTest()
{
var conv2d = keras.layers.Conv2D(8, activation: "linear");
Assert.AreEqual("conv2d", conv2d.Name);
var conv2dTranspose = keras.layers.Conv2DTranspose(8);
Assert.AreEqual("conv2d_transpose", conv2dTranspose.Name);
}
}
}
Loading