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.25k stars 523 forks source link

Assign from one Tensor to another throws exception. #968

Open Tomazzzzz opened 1 year ago

Tomazzzzz commented 1 year ago

I have a problem when trying to copy variables between Tensors. The test bellow fails with exception. I am using latest nuget package TensorFlow.NET 0.70.2 & SciSharp.TensorFlow.Redist-Windows-GPU 2.10.0

[Test]
public void Assign2()
{
    int inputCount = 100;
    int outputCount = 10;
    var stddev = 1 / Math.Sqrt(inputCount);

    tf.compat.v1.disable_eager_execution();
    using (var graph = tf.Graph().as_default())
    using (var sess = tf.Session(graph))
    {
        var a1 = tf.Variable(
            tf.truncated_normal(
                new[] { inputCount, outputCount },
                seed: 1,
                stddev: (float)stddev),
            dtype: TF_DataType.DtFloatRef,
            name: "A1");

        var b1 = tf.Variable(
            tf.truncated_normal(
                new[] { inputCount, outputCount },
                seed: 1,
                stddev: (float)stddev),
            dtype: TF_DataType.DtFloatRef,
            name: "B1");

        sess.run(tf.global_variables_initializer());

        var opHolder = new List<Tensor>();
        opHolder.Add(a1.assign(b1));

        sess.run(opHolder.ToArray());
    }
}

Exception:

Tensorflow.TensorflowException HResult=0x80131500 Message=Cast float to resource is not supported [[{{node ReadVariableOp/resource}}]] Source=Tensorflow.Binding StackTrace: at Tensorflow.BaseSession._call_tf_sessionrun(KeyValuePair'2[] feed_dict, TF_Output[] fetch_list, List'1 target_list) at Tensorflow.BaseSession._do_run(List'1 target_list, List'1 fetch_list, Dictionary'2 feed_dict) at Tensorflow.BaseSession._run(Object fetches, FeedItem[] feed_dict) at TomNeuralNetwork.Test.TensorFlowNet.Test.Assign.Assign2() in E:\Projects\NueralNetwork\TomNeuralNetwork.Test\TensorFlowNet.Test\Assign.cs:line 60

Oceania2018 commented 1 year ago

Why not use Eager mode?