Skip to content

Bad results of simple XOR training #952

Closed

Description

@neojg

I have windows 10 CPU with:

Python 3.9.13
tensorflow 2.10.0

I'm testing a simple XOR training:

            var x = np.array(new float[,] { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } });
            var y = np.array(new float[,] { { 0 }, { 1 }, { 1 }, { 0 } });

            var model = keras.Sequential();
            model.add(keras.Input(2));
            model.add(keras.layers.Dense(32, keras.activations.Relu)); // 
            model.add(keras.layers.Dense(64, keras.activations.Relu));
            model.add(keras.layers.Dense(1, keras.activations.Sigmoid));
            model.compile(keras.optimizers.Adam(), keras.losses.MeanSquaredError(), new[] { "accuracy" });
            model.fit(x, y, epochs: 800, verbose: 2);
            print(model.predict(x, 4));

The results from print command are as follow:

tf.Tensor: shape=(4, 1), dtype=float32, numpy=array([[0,5],
[0,492741],
[0,4584209],
[0,4671426]])

PROBLEM

  1. When I run same sample on python/keras all is OK
  2. The console does not show the training messages!
  3. The c# results are incorrect!

What is wrong? Is there a way to get simmilar results for python/tensorflow/keras and the Tensorflow.Net?

Activity

  1. added
    bug
    discussion
    on Nov 14, 2022
  2. Oceania2018 commented on Nov 16, 2022

    @Oceania2018
    Member

    @neojg Hi, I've found an issue regarding the trainable_variables in the Model. It will be fixed for next release. Below is the latest test in my local machine. The result looks pretty awesome.

    Microsoft Windows NT 10.0.22621.0
    64Bit Operating System: True
    .NET CLR: 6.0.11
    TensorFlow Binary v2.10.0
    TensorFlow.NET v0.100.0.0
    TensorFlow.Keras v0.10.0.0
    C:\Users\haipi\source\repos\SciSharp\SciSharp-Stack-Examples\src\TensorFlowNET.Examples\bin\Debug\net6.0
    11/16/2022 7:53:10 PM Starting NN XOR in Keras
    2022-11-16 13:53:10.318050: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2
    To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
    Epoch: 001/050
    0004/0004 [============================>] - 3ms/step loss: 0.249935, accuracy: 0.2500000
    Epoch: 002/050
    0004/0004 [============================>] - 3ms/step loss: 0.248105, accuracy: 0.250000
    Epoch: 003/050
    0004/0004 [============================>] - 3ms/step loss: 0.247583, accuracy: 0.250000
    Epoch: 004/050
    0004/0004 [============================>] - 1ms/step loss: 0.246641, accuracy: 0.250000
    Epoch: 005/050
    0004/0004 [============================>] - 3ms/step loss: 0.246125, accuracy: 0.250000
    Epoch: 006/050
    0004/0004 [============================>] - 2ms/step loss: 0.245473, accuracy: 0.250000
    Epoch: 007/050
    0004/0004 [============================>] - 2ms/step loss: 0.244915, accuracy: 0.250000
    Epoch: 008/050
    0004/0004 [============================>] - 2ms/step loss: 0.244200, accuracy: 0.250000
    Epoch: 009/050
    0004/0004 [============================>] - 2ms/step loss: 0.243570, accuracy: 0.500000
    Epoch: 010/050
    0004/0004 [============================>] - 3ms/step loss: 0.242876, accuracy: 0.500000
    Epoch: 011/050
    0004/0004 [============================>] - 2ms/step loss: 0.242310, accuracy: 0.500000
    Epoch: 012/050
    0004/0004 [============================>] - 2ms/step loss: 0.241574, accuracy: 0.500000
    Epoch: 013/050
    0004/0004 [============================>] - 1ms/step loss: 0.240878, accuracy: 0.500000
    Epoch: 014/050
    0004/0004 [============================>] - 1ms/step loss: 0.240328, accuracy: 0.500000
    Epoch: 015/050
    0004/0004 [============================>] - 1ms/step loss: 0.239720, accuracy: 0.500000
    Epoch: 016/050
    0004/0004 [============================>] - 2ms/step loss: 0.239134, accuracy: 0.500000
    Epoch: 017/050
    0004/0004 [============================>] - 1ms/step loss: 0.238470, accuracy: 0.500000
    Epoch: 018/050
    0004/0004 [============================>] - 1ms/step loss: 0.237623, accuracy: 0.500000
    Epoch: 019/050
    0004/0004 [============================>] - 2ms/step loss: 0.237053, accuracy: 0.500000
    Epoch: 020/050
    0004/0004 [============================>] - 1ms/step loss: 0.236413, accuracy: 0.500000
    Epoch: 021/050
    0004/0004 [============================>] - 2ms/step loss: 0.235718, accuracy: 0.500000
    Epoch: 022/050
    0004/0004 [============================>] - 1ms/step loss: 0.235132, accuracy: 0.500000
    Epoch: 023/050
    0004/0004 [============================>] - 2ms/step loss: 0.234610, accuracy: 0.500000
    Epoch: 024/050
    0004/0004 [============================>] - 1ms/step loss: 0.233913, accuracy: 0.500000
    Epoch: 025/050
    0004/0004 [============================>] - 1ms/step loss: 0.233496, accuracy: 0.500000
    Epoch: 026/050
    0004/0004 [============================>] - 1ms/step loss: 0.232802, accuracy: 0.500000
    Epoch: 027/050
    0004/0004 [============================>] - 1ms/step loss: 0.232160, accuracy: 0.500000
    Epoch: 028/050
    0004/0004 [============================>] - 1ms/step loss: 0.231730, accuracy: 0.500000
    Epoch: 029/050
    0004/0004 [============================>] - 1ms/step loss: 0.231119, accuracy: 0.500000
    Epoch: 030/050
    0004/0004 [============================>] - 1ms/step loss: 0.230454, accuracy: 0.500000
    Epoch: 031/050
    0004/0004 [============================>] - 1ms/step loss: 0.229833, accuracy: 0.500000
    Epoch: 032/050
    0004/0004 [============================>] - 1ms/step loss: 0.229436, accuracy: 0.500000
    Epoch: 033/050
    0004/0004 [============================>] - 1ms/step loss: 0.228646, accuracy: 0.500000
    Epoch: 034/050
    0004/0004 [============================>] - 1ms/step loss: 0.228098, accuracy: 0.500000
    Epoch: 035/050
    0004/0004 [============================>] - 1ms/step loss: 0.227608, accuracy: 0.500000
    Epoch: 036/050
    0004/0004 [============================>] - 1ms/step loss: 0.226957, accuracy: 0.500000
    Epoch: 037/050
    0004/0004 [============================>] - 1ms/step loss: 0.226425, accuracy: 0.500000
    Epoch: 038/050
    0004/0004 [============================>] - 2ms/step loss: 0.225798, accuracy: 0.750000
    Epoch: 039/050
    0004/0004 [============================>] - 1ms/step loss: 0.225017, accuracy: 0.750000
    Epoch: 040/050
    0004/0004 [============================>] - 1ms/step loss: 0.224596, accuracy: 0.750000
    Epoch: 041/050
    0004/0004 [============================>] - 2ms/step loss: 0.223943, accuracy: 0.750000
    Epoch: 042/050
    0004/0004 [============================>] - 2ms/step loss: 0.223310, accuracy: 1.000000
    Epoch: 043/050
    0004/0004 [============================>] - 1ms/step loss: 0.222728, accuracy: 1.000000
    Epoch: 044/050
    0004/0004 [============================>] - 1ms/step loss: 0.222116, accuracy: 1.000000
    Epoch: 045/050
    0004/0004 [============================>] - 1ms/step loss: 0.221506, accuracy: 1.000000
    Epoch: 046/050
    0004/0004 [============================>] - 1ms/step loss: 0.221035, accuracy: 1.000000
    Epoch: 047/050
    0004/0004 [============================>] - 1ms/step loss: 0.220380, accuracy: 1.000000
    Epoch: 048/050
    0004/0004 [============================>] - 1ms/step loss: 0.219896, accuracy: 1.000000
    Epoch: 049/050
    0004/0004 [============================>] - 1ms/step loss: 0.219306, accuracy: 1.000000
    Epoch: 050/050
    0004/0004 [============================>] - 1ms/step loss: 0.218623, accuracy: 1.000000
    tf.Tensor: shape=(4, 1), dtype=float32, numpy=array([[0.48882273],
    [0.6142097],
    [0.5059311],
    [0.49007967]])
    11/16/2022 7:53:13 PM Completed NN XOR in Keras
    Example: NN XOR in Keras in 3.2669222s is OK!
    Microsoft Windows NT 10.0.22621.0
    64Bit Operating System: True
    .NET CLR: 6.0.11
    TensorFlow Binary v2.10.0
    TensorFlow.NET v0.100.0.0
    TensorFlow.Keras v0.10.0.0
    1 of 1 example(s) are completed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugdiscussion

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions