Skip to content

Commit

Permalink
Fix torch reshape (#18641)
Browse files Browse the repository at this point in the history
ops.reshape(tensor, -1) wasn't working on torch backend. It's a fairly
common numpy-ism.
  • Loading branch information
mattdangerw authored Oct 17, 2023
1 parent ea3f7e8 commit 6d66a12
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
2 changes: 2 additions & 0 deletions keras/backend/torch/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,8 @@ def repeat(x, repeats, axis=None):


def reshape(x, new_shape):
if not isinstance(new_shape, (list, tuple)):
new_shape = (new_shape,)
x = convert_to_tensor(x)
return torch.reshape(x, new_shape)

Expand Down
1 change: 1 addition & 0 deletions keras/ops/numpy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3631,6 +3631,7 @@ def test_reshape(self):
x = np.array([[1, 2, 3], [3, 2, 1]])
self.assertAllClose(knp.reshape(x, [3, 2]), np.reshape(x, [3, 2]))
self.assertAllClose(knp.Reshape([3, 2])(x), np.reshape(x, [3, 2]))
self.assertAllClose(knp.Reshape(-1)(x), np.reshape(x, -1))

@pytest.mark.skipif(
not backend.SUPPORTS_SPARSE_TENSORS,
Expand Down

0 comments on commit 6d66a12

Please sign in to comment.