Translated TensorFlow Python to C# XOR sample throws exception

Viewed 146

I am trying to translate the working python XOR sample to the C# binding using TensorFlow.NET

The working Python code, which I tested in VS Code is the following:

import numpy as np
from keras.models import Sequential
from keras.layers.core import Activation, Dense

training_data = np.array([[0,0],[0,1],[1,0],[1,1]], "float32")
target_data = np.array([[0],[1],[1],[0]], "float32")

model = Sequential()
model.add(Dense(32, input_dim=2, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['binary_accuracy'])
model.fit(training_data, target_data, epochs=100, verbose=2)
print(model.predict(training_data))

My C# translation to the TensorFlow.NET binding is the following:

using System;
using NumSharp;
using static Tensorflow.Binding;
using static Tensorflow.KerasApi;

var trainingData = np.array((Array) new float[,] {{0, 0}, {0, 1}, {1, 0}, {1, 1}});
var targetData = np.array((Array) new float[,] {{0}, {1}, {1}, {0}});
var model = keras.Sequential();
model.add(keras.Input(2)); // NOTE: based on debugging this will be shape [None, 2] in the C# binding
model.add(keras.layers.Dense(32, keras.activations.Relu));
model.add(keras.layers.Dense(1, keras.activations.Sigmoid));
model.compile(keras.losses.MeanSquaredError(), keras.optimizers.Adam(), new[] {"accuracy"}); // binary_accuracy is not implemented yet in the C# binding
model.fit(trainingData, targetData, 4, 100, 2); // batch_size = 4, epochs = 100, verbose = 2
print(model.predict(trainingData)); // This line throws the exception below

EDIT, my bad: not the model.fit(), instead model.predict() throws...

However, when executing model.predict() this throws a

Tensorflow.StopIteration : End of sequence at Tensorflow.OwnedIterator.next() at Tensorflow.Keras.Engine.Model.run_predict_step(OwnedIterator iterator) at Tensorflow.Keras.Engine.Model.predict(Tensor x, Int32 batch_size, Int32 verbose, Int32 steps, Int32 max_queue_size, Int32 workers, Boolean use_multiprocessing) at

1 Answers

NOTE: This is only a partial answer, which resolves only the exception. However the C# code in the OP, even corrected based on this partial answer still has some inherent problem: It converges the NN to produce 0, 0, 0, 0 output (instead of the expected XOR rule 0, 1, 1, 0). I am posting this in hope it helps to iterate to the correct answer.


Maybe I am missing something, but my translation uses the batch size 4, while the original Python version somehow infers it being the training data in shape[2,4]

Anyway this causes the extra parameter batch_size with value 4 in fit() as I figured it out originally.

What I did not figured out, (and the resolution of the issue) is that this extra parameter batch_size with value 4 must be provided to model.predict() too, so instead the line

 print(model.predict(trainingData));

we should have

print(model.predict(trainingData, 4)); 

If anyone knows how to be more faithful to the original Python in regard using the API please correct me.

Related