SciSharp / TensorFlow.NET

.NET Standard bindings for Google's TensorFlow for developing, training and deploying Machine Learning models in C# and F#.
https://scisharp.github.io/tensorflow-net-docs
Apache License 2.0
3.24k stars 521 forks source link

Very simple working Python XOR sample translated to identical TensorFlow.NET does not work at all or behaves very differently #806

Closed g-pickardou closed 3 years ago

g-pickardou commented 3 years ago

I am trying to translate a working Python XOR example to TensorFlow.NET. Both versions are a very short few liners, hopefully just a matter of copy and paste to reproduce (supposing a TensorFlow 2.4.1 environment is already set up)

The translated C# is compiles and runs, but works way differently. When using the loss function keras.losses.MeanSquaredError() the C# model does not work at all (loss is increasing, while the very same Python code, with the very same defaults learns pretty well). When using keras.losses.MeanAbsoluteError() the C# version earns, but loss decreasing ten times slower compared to the Python version.

Regardless the first layer is 32, 64 or 1024 and regardless the epoch is 100 or 1000 the very same difference between occurs the Python and C# implementations. Python works even with first layer 32 units and epoch 100, and in the C# version the loss is increasing.

Please note, I am not asking, why C# model is not working, I am asking what issue causes the different behavior.

What I've done and checked so far:

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(Input((2,))) model.add(Dense(64, activation='relu')) model.add(Dense(1, activation='sigmoid')) model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy']) model.fit(training_data, target_data, epochs=1000 , verbose=1)

model.summary()

print(model.predict(training_data))

**C# translated sample code**
```csharp
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));
model.add(keras.layers.Dense(64, keras.activations.Relu));
model.add(keras.layers.Dense(1, keras.activations.Sigmoid));
model.compile(keras.losses.MeanSquaredError(), keras.optimizers.Adam(), new[] {"accuracy"});
model.fit(trainingData, targetData, 1, 1000);
print(model.predict(trainingData, 4));

Results with the exact sample programs

Python with 'mean_squared_error': start loss: 0.4953, accuracy: 0.7500 end loss: 0.0046, accuracy: 1,000000 prediction: (correct XOR) [[0.07034078] [0.93302286] [0.9332601 ] [0.06674343]]

C# with keras.losses.MeanSquaredError(): start loss: 0,254314, accuracy: 1,000000 end loss: 0,461851, accuracy: 1,000000 prediction: (incorrect XOR) [[0,036265105], [0,005444467], [0,0042176247], [0,00059726834]]

Results with the exact sample programs, except using absolute error instead square error

Python with 'mean_absolute_error': start loss: loss: 0.4830 - accuracy: 0.7500 end loss: 0.0141 - accuracy: 1.0000 prediction: (correct XOR) [[0.01455569] [0.98603207] [0.98603785] [0.01395237]]

C# with keras.losses.MeanAbsoluteError(): Note: Although this case the loss decreases, but still 10 times greater than in the Python version Interestingly the res start loss: 0,501608, accuracy: 1,000000 end loss: 0,185201, accuracy: 1,000000 prediction: (correct XOR, actually better than the Python version) [[0,010161787], [0,9958803], [0,99654853], [0,0029382408]]

Oceania2018 commented 3 years ago

@g-pickardou Thank you for your elaborated description. The bug is fixed in the new source code. We'll release it this weekend.

g-pickardou commented 3 years ago

Many thanks, that was fast. Could you please point to the commit, which is the fix, just for curiosity.

Oceania2018 commented 3 years ago

Should be this fix if I don’t remember wrong: https://github.com/SciSharp/TensorFlow.NET/commit/f0030ca9bb407c66c2767b7ea445b3c531b0cef5