Skip to content

Commit

Permalink
Remove tensorflow dependency. (#484)
Browse files Browse the repository at this point in the history
* enh: remove tensorflow dependency. closes #481

* fix: ammend last commit

* fix: change params

* fix: checkout tutorial 07 on main and apply changes to reduce diff
  • Loading branch information
jnsbck authored Nov 4, 2024
1 parent 981ab64 commit 6495162
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions docs/tutorials/07_gradient_descent.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,33 @@
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow.data import Dataset"
"class Dataset:\n",
" def __init__(self, inputs: np.ndarray, labels: np.ndarray):\n",
" \"\"\"Simple Dataloader.\n",
" \n",
" Args:\n",
" inputs: Array of shape (num_samples, num_dim)\n",
" labels: Array of shape (num_samples,)\n",
" \"\"\"\n",
" assert len(inputs) == len(labels), \"Inputs and labels must have same length\"\n",
" self.inputs = inputs\n",
" self.labels = labels\n",
" self.num_samples = len(inputs)\n",
" \n",
" def shuffle(self, seed=None):\n",
" \"\"\"Shuffle the dataset in-place\"\"\"\n",
" if seed is not None:\n",
" np.random.seed(seed)\n",
" indices = np.random.permutation(self.num_samples)\n",
" self.inputs = self.inputs[indices]\n",
" self.labels = self.labels[indices]\n",
" return self\n",
" \n",
" def batch(self, batch_size):\n",
" \"\"\"Create batches of the data\"\"\"\n",
" for start in range(0, self.num_samples, batch_size):\n",
" end = min(start + batch_size, self.num_samples)\n",
" yield self.inputs[start:end], self.labels[start:end]"
]
},
{
Expand All @@ -768,9 +793,8 @@
"source": [
"batch_size = 4\n",
"\n",
"tf.random.set_seed(1)\n",
"dataloader = Dataset.from_tensor_slices((inputs, labels))\n",
"dataloader = dataloader.shuffle(dataloader.cardinality()).batch(batch_size)"
"dataloader = Dataset(inputs, labels)\n",
"dataloader = dataloader.shuffle(seed=1).batch(batch_size)"
]
},
{
Expand Down

0 comments on commit 6495162

Please sign in to comment.