{"record":{"id":"31b38c84e6ef872f","repo":"keras-team/keras","slug":"you-are-attempting-to-initialize-a-variable-while-31b38c","errorCode":null,"errorMessage":"You are attempting to initialize a variable while in a stateless scope. This is disallowed. Make sure that all variables are initialized before you start using your layer/model objects.","messagePattern":"You are attempting to initialize a variable while in a stateless scope\\. This is disallowed\\. Make sure that all variables are initialized before you start using your layer/model objects\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/tensorflow/core.py","lineNumber":61,"sourceCode":"            self._value = tf.Variable(\n                value,\n                shape=self._shape,\n                dtype=self._dtype,\n                trainable=self.trainable,\n                name=self.name,\n                aggregation=self._map_aggregation(self.aggregation),\n                synchronization=self._map_synchronization(self.synchronization),\n            )\n\n    def _initialize_with_initializer(self, initializer):\n        self._initialize(lambda: initializer(self._shape, dtype=self._dtype))\n\n    def _deferred_initialize(self):\n        if self._value is not None:\n            raise ValueError(f\"Variable {self.path} is already initialized.\")\n\n        if in_stateless_scope():\n            raise ValueError(\n                \"You are attempting to initialize a variable \"\n                \"while in a stateless scope. This is disallowed. \"\n                \"Make sure that all variables are initialized \"\n                \"before you start using your layer/model objects.\"\n            )\n        with tf.init_scope():\n            self._initialize_with_initializer(self._initializer)\n            self._initializer = None\n\n    def _direct_assign(self, value):\n        self._value.assign(tf.cast(value, self._value.dtype))\n\n    def _convert_to_tensor(self, value, dtype=None):\n        return convert_to_tensor(value, dtype=dtype)\n\n    def numpy(self):  # noqa: F811\n        return self.value.numpy()\n","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/tensorflow/core.py#L43-L79","documentation":"Error \"You are attempting to initialize a variable while in a stateless scope. This is disallowed. Make sure that all variables are initialized before you start using your layer/model objects.\" thrown in keras-team/keras.","triggerScenarios":"Thrown at keras/src/backend/tensorflow/core.py:61 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}