Skip to content

Commit 565d694

Browse files
authored
Do not use os.path.join for variable paths. (#21680)
Variable paths do not represent a filesystem path and should not be OS dependent, they should always use "/" as a separator. This reverts #21343 just for variables.
1 parent fa9c4ba commit 565d694

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

keras/src/backend/common/variables.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import os.path
2-
31
import numpy as np
42

53
from keras.src import backend
@@ -144,7 +142,7 @@ def __init__(
144142
self._name = name
145143
parent_path = current_path()
146144
if parent_path:
147-
self._path = os.path.join(current_path(), name)
145+
self._path = f"{parent_path}/{name}"
148146
else:
149147
self._path = name
150148
self._shape = None

keras/src/backend/common/variables_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ def test_variable_path_creation(self):
328328
v = backend.Variable(initializer=np.ones((2, 2)), name="test_var")
329329
self.assertEqual(v.path, "test_var")
330330

331+
with backend.name_scope("test_scope"):
332+
v = backend.Variable(initializer=np.ones((2, 2)), name="test_var")
333+
self.assertEqual(v.path, "test_scope/test_var")
334+
331335
def test_overwrite_with_gradient_setter(self):
332336
v = backend.Variable(
333337
initializer=initializers.RandomNormal(),

0 commit comments

Comments
 (0)