Fix timeseries_dataset_from_array counts when sequence_stride > 1
diff --git a/keras/utils/timeseries_dataset.py b/keras/utils/timeseries_dataset.py
index a53860ec98e..60c37b116d9 100644
--- a/keras/utils/timeseries_dataset.py
+++ b/keras/utils/timeseries_dataset.py
@@ -84,7 +84,7 @@ def timeseries_dataset_from_array(
 
     Example 1:
 
-    Consider indices `[0, 1, ... 99]`.
+    Consider indices `[0, 1, ... 98]`.
     With `sequence_length=10,  sampling_rate=2, sequence_stride=3`,
     `shuffle=False`, the dataset will yield batches of sequences
     composed of the following indices:
@@ -97,9 +97,9 @@ def timeseries_dataset_from_array(
     Last sequence:   [78 80 82 84 86 88 90 92 94 96]
     ```
 
-    In this case the last 3 data points are discarded since no full sequence
+    In this case the last 2 data points are discarded since no full sequence
     can be generated to include them (the next sequence would have started
-    at index 81, and thus its last step would have gone over 99).
+    at index 81, and thus its last step would have gone over 98).
 
     Example 2: Temporal regression.
 
@@ -209,7 +209,7 @@ def timeseries_dataset_from_array(
 
     # Determine the lowest dtype to store start positions (to lower memory
     # usage).
-    num_seqs = end_index - start_index - (sequence_length * sampling_rate) + 1
+    num_seqs = end_index - start_index - (sequence_length - 1) * sampling_rate
     if targets is not None:
         num_seqs = min(num_seqs, len(targets))
     if num_seqs < 2147483647:
diff --git a/keras/utils/timeseries_dataset_test.py b/keras/utils/timeseries_dataset_test.py
index 28fc932dfe5..77f6acd33d3 100644
--- a/keras/utils/timeseries_dataset_test.py
+++ b/keras/utils/timeseries_dataset_test.py
@@ -130,8 +130,8 @@ def test_sampling_rate(self):
             if i < 16:
                 self.assertEqual(inputs.shape, (5, 9))
             if i == 16:
-                # Last batch: size 3
-                self.assertEqual(inputs.shape, (3, 9))
+                # Last batch: size 4
+                self.assertEqual(inputs.shape, (4, 9))
             # Check target values
             self.assertAllClose(inputs[:, 0] * 2, targets)
             for j in range(min(5, len(inputs))):

Fixes https://github.com/keras-team/tf-keras/issues/314