Cant assign trainable attribute to variables ->AttributeError: can't set attribute
Hi @samdabadei,
 
The error occurs because you are trying to set the trainable attribute of a non-trainable variable `self.a`.

In the __init__ method of your model class, you set `self.a` to a non-trainable variable with the statement `self.a = tf.Variable([1.0], trainable=False)`. Therefore, when you try to set the trainable attribute of `self.a` in the following line:

```
model.a.trainable = True
```
you get the AttributeError: can't set attribute error.  To fix this error, you can remove these statements `self.a.trainable = True` , `model.b.trainable = True` and set the trainable attribute of `self.b` to **True** in the `__init__` method by passing the `trainable=True` argument to the add_weight method and  tf.Variable, as shown below:

Here's the working code:
```
import tensorflow as tf
from keras import layers
import numpy as np


class model(tf.keras.Model):

    def __init__(self):
        super(model, self).__init__()
        self.b = self.add_weight(shape=(1, 5), trainable=True)
        self.a = tf.Variable([1.0], trainable=True)

    def call(self, inputs, training=None, mask=None):
        return self.a * self.b * inputs


input = np.random.randn(10, 5)
target = np.random.randn(10, 1)

model = model()
model.compile("adam", loss="mse")

model.fit(input, target, validation_data=(input, target), epochs=3)

```

Thank you! Is there no way to change the trainable attribute of a Variable after the first creation? Between the model-init() and the model-compile(). Its possible for layers and models and its quite tedious to change this  always in the init when doing transfer learning Hi @samdabadei,
Could you please elaborate how the above model is used in transfer learning?   okay concrete example: I do transfer learning. first i transfer some parameters, layers etc and make them not trainable. after saving the model weights i want to load them  again for finetuning but in order to do this i have to set the same variable to non trainable since the order of the weights would be different then when they where saved. but now there is no way for me to free them again. and again somehow it has to be possbile  right? i mean layer are also just a collection of variables and there i can change the trainability like i want before compiling my current work-around for anybody interested:

```
class VarablesLayer(tf.keras.layers.Layer):
    def __init__(self, shape, name, trainable=True, activation=None, **kwargs):
        super(VarablesLayer, self).__init__(name=name, trainable=trainable)
        self.var = self.add_weight(name=f"{name}/var", shape=shape, **kwargs)
        self.activation = activation


    def call(self, inputs, *args, **kwargs):
        if self.activation:
            return self.activation(self.var)
        return self.var
```

its a wrapper layer for a variable. you then can set the trainablitlity of the layer and get the values by calling the layer with None as argument Currently the variable trainable is set as [@property](https://docs.python.org/2/library/functions.html#property) object.
https://github.com/tensorflow/tensorflow/blob/0db597d0d758aba578783b5bf46c889700a45085/tensorflow/python/ops/variables.py#L516-L518

Which means you will not be able to modify the variable unless it is defined under `@trainable.setter`

Consider the below scenario.

1. Trying to modify the trainable variable when it is defined as `@property`, it throws error as expected.

```
class a:

  def __init__(self,trainable):
    self.trainable = trainable

  @property
  def trainable(self):
    return self._trainable

c = a(False)
c.trainable = True
```

2. Trying to modify the variable with the `@traiable.setter`
```
class a:

  def __init__(self,trainable):
    self.trainable = trainable

  @property
  def trainable(self):
    return self._trainable

  @trainable.setter
  def trainable(self, value):
    self._trainable = value


c = a(False)
c.trainable = True
``` This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60004">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60004">No</a>

<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Bug

### Have you reproduced the bug with TF nightly?

Yes

### Source

source

### Tensorflow Version

tf.2.10

### Custom Code

Yes

### OS Platform and Distribution

Windows 10

### Mobile device

_No response_

### Python version

3.9

### Bazel version

_No response_

### GCC/Compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current Behaviour?

```shell
When i try to change the trainability of a variable of a model i get the error : AttributeError: can't set attribute
```


### Standalone code to reproduce the issue

```shell
import tensorflow as tf
from keras import layers
import numpy as np


class model(tf.keras.Model):

    def __init__(self):
        super(model, self).__init__()
        self.b = self.add_weight(shape=(1, 5), trainable=True)
        self.a = tf.Variable([1.0], trainable=False)
        self.a.trainable = True

    def call(self, inputs, training=None, mask=None):
        return self.a * self.b * inputs


input = np.random.randn(10, 5)
target = np.random.randn(10, 1)

model = model()
model.b.trainable = True
model.a.trainable = True
model.compile("adam", loss="mse")

model.fit(input, target, validation_data=(input, target), epochs=3)
```


### Relevant log output

```shell
self.a.trainable = True
AttributeError: can't set attribute

Process finished with exit code 1
```
</details>