Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Tensorflow.NET version of set_weights() #1025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
FrancescoRusticali opened this issue Apr 18, 2023 · 7 comments
Closed

Tensorflow.NET version of set_weights() #1025

FrancescoRusticali opened this issue Apr 18, 2023 · 7 comments
Assignees
Labels
api New API or change API enhancement New feature or request

Comments

@FrancescoRusticali
Copy link

Description

I am trying to find a way to set weights of an individual neuron or layer manually without training.
Is there any corresponding in Tensorflow.NET to python function set_weights?
In version 100.4 I couldn't find any.

Alternatives

No response

@AsakusaRinne
Copy link
Collaborator

What if just using the setter of the property Weights of an Layer object?

@FrancescoRusticali
Copy link
Author

FrancescoRusticali commented Apr 18, 2023

I'm not sure if this is the right way to do it.
Currently it's throwing a NullReferenceException.

The following code snippet is an example:

        var layers = new LayersApi();

        var sequential = keras.Sequential();
        var inputs = keras.Input(shape: new Shape(8));
        sequential.add(inputs);
        sequential.add(layers.Dense(8, "relu"));

        sequential.compile(loss: keras.losses.SparseCategoricalCrossentropy(from_logits: true),
                            optimizer: keras.optimizers.Adam(),
                            metrics: new[] { "accuracy" });

        float[,] myArray = new float[8, 8]
        {
            { 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f },
            { 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f },
            { 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f },
            { 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f },
            { 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f },
            { 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f },
            { 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f },
            { 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f },
        };

        Tensor myTensor = new Tensor(myArray);
        sequential.Weights[0] = new ResourceVariable(myTensor);

It's maybe wrong to use the ResourceVariable constructor?

@AsakusaRinne
Copy link
Collaborator

Please use Tensor myTensor = tf.convert_to_tensor(myArray); because directly using constructor of Tensor won't produce an EagerTensor.

I'm not sure if this is the right way to do it.

I agree and we'll add the set_weights API. If you met any other problem when using the setter of Weights, please tell us and we'll help you.

@AsakusaRinne
Copy link
Collaborator

BTW, tf.Variable is recommended compared to using constructor of ResourceVariable.

@Wanglongzhi2001
Copy link
Collaborator

It works well when use sequential.Weights[0].assign(myTensor);

@AsakusaRinne AsakusaRinne added enhancement New feature or request api New API or change API labels Apr 19, 2023
@FrancescoRusticali
Copy link
Author

Thank you all for the useful suggestions.
The following works fine:

Tensor myTensor = tf.convert_to_tensor(myArray);
sequential.Weights[0].assign(myTensor);

Instead with:

Tensor myTensor = new Tensor (myArray);
sequential.Weights[0].assign(myTensor);

I get the following exception:

Tensorflow.RuntimeError: 'Attempting to capture an EagerTensor without building a function.'

The tf.convert_to_tensor function seems to be mandatory.
Provided you're using it, other formulations of the second row (like tf.Variable, or ResourseVariable constructor) seem not to throw exceptions, but weights are actually not updated.

@AsakusaRinne
Copy link
Collaborator

The set_weights API has already been added, close this issue as completed. You can reopen this issue if there's any problem with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api New API or change API enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants