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.2k stars 514 forks source link

refactor: Model.eval #1092

Closed DevNullx64 closed 1 year ago

Wanglongzhi2001 commented 1 year ago

LGTM,Model.Evaluate does have some repetitive code. For example, test_function and test_step_multi_inputs_function are actually doing the same thing, this modification can reduce the repetitive code of overloading.

Wanglongzhi2001 commented 1 year ago

BTW, in the test_step_multi_inputs_function of Model.Evaluate, should train_step be changed to test_step,? can you help change it?

DevNullx64 commented 1 year ago

Thank ! It's my mess, I've not well seen this. I'm on it :)

DevNullx64 commented 1 year ago

thank @AsakusaRinne to had change pull request title. I will try to take care next. @Wanglongzhi2001, it should be done. But is it a normal behaviour to have "step_train" here ?

Wanglongzhi2001 commented 1 year ago

Looks good to me.

Wanglongzhi2001 commented 1 year ago

@DevNullx64 , Seems there exists some bug in the model.evaluate, If the input data is NDArray type, it will matchs both evaluate(Tensor,..) and evaluate(IEnumable,... signatures, which will result in an ambiguous error in the call model.evaluate. The following is a example:

            var x_train = np.array(new float[,] { { 0.1f }, { 0.2f }, { 0.3f }, { 0.4f } });
            var y_train = np.array(0, 1, 2, 3);

            var x_test = x_train;
            var y_test = y_train;

            var model = keras.Sequential(new List<ILayer>() {
                tf.keras.layers.Dense(128, activation: "relu", input_shape: new Shape(1)),
                tf.keras.layers.Dense(4)
            });

            var loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits: true);

            model.compile(optimizer: new Tensorflow.Keras.Optimizers.Adam(),
                loss: loss_fn,
                metrics: new[] { "accuracy" });

            model.fit(x_train, y_train, epochs: 10, verbose: 0);

            model.evaluate(x_test, y_test, verbose: 2);