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