add jupyter notebook tricks

This commit is contained in:
hobogalaxy committed 2020-11-18 03:32:51 +01:00
1 parent 99d213f6cf
commit 71a4d7b637
2 files changed
+309 -1

No files matched your search

+1 -1
View File
@@ -25,7 +25,7 @@ loggers:
group: None
job_type: "train"
tags: []
log_model: False
log_model: True
offline: False
callbacks:
+308
View File
@@ -0,0 +1,308 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import modules from parent directory"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [],
"source": [
"# add directory above current directory to path\n",
"import sys\n",
"sys.path.insert(0, '..')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [],
"source": [
"# now we can import modules from parent directory\n",
"import train\n",
"import pipeline_modules"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define function to print object length with commas"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2,395,940\n"
]
}
],
"source": [
"showlen = lambda data: print(f\"{len(data):,}\")\n",
"showlen([0 for i in range(2395940)])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create quick progress bars with tqdm"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|█████████████████████████████████████████████████████████| 10/10 [00:10<00:00, 1.00s/it]\n"
]
}
],
"source": [
"from tqdm import tqdm\n",
"import time\n",
"\n",
"for i in tqdm(range(10)):\n",
" time.sleep(1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"source": [
"### Prettier ipywidgets version of the progress bar specifically for the notebook environment"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5a5edf6f473c4a35961c7573fcbec317",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(HTML(value=''), FloatProgress(value=0.0, max=10.0), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"from tqdm.auto import tqdm\n",
"\n",
"for i in tqdm(range(10)):\n",
" time.sleep(1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"source": [
"### Beyond basic for loops, you can also use it in list comprehensions"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "fd75636e82d84b76ac73ade86e3a8516",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(HTML(value=''), FloatProgress(value=0.0, max=10.0), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"some_list = [x**2 for x in tqdm(10*[0])]"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"source": [
"### You can even use it with multiprocessing\n",
"https://stackoverflow.com/questions/41920124/multiprocessing-use-tqdm-to-display-a-progress-bar"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### If you’re iterating over a generator, tqdm won’t know the length and the progress bar will be more limited but if you know or can calculate the total length you can supply that as an argument"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6f3a6d0f982e47c1b14c0e046997a44d",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(HTML(value=''), FloatProgress(value=0.0, max=4.0), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"# generator expressions are surrounded by parenthesis ()\n",
"generator = (x**2 for x in [1, 2, 3, 4])\n",
"# print(next(generator))\n",
"\n",
"some_list = [x for x in tqdm(generator, total=4)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Jupyter Notebook Shortcuts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Esc will take you into command mode where you can navigate around your notebook with arrow keys.\n",
"- While in command mode:\n",
" - A to insert a new cell above the current cell, B to insert a new cell below.\n",
" - M to change the current cell to Markdown, Y to change it back to code\n",
" - D + D (press the key twice) to delete the current cell\n",
" - Enter will take you from command mode back into edit mode for the given cell.\n",
"- Shift + Tab will show you the Docstring (documentation) for the the object you have just typed in a code cell – you can keep pressing this short cut to cycle through a few modes of documentation.\n",
"- Ctrl + Shift + - will split the current cell into two from where your cursor is.\n",
"- Esc + O Toggle cell output.\n",
"- Select Multiple Cells:\n",
" - Shift + J or Shift + Down selects the next sell in a downwards direction. You can also select sells in an upwards direction by using Shift + K or Shift + Up.\n",
" - Once cells are selected, you can then delete / copy / cut / paste / run them as a batch. This is helpful when you need to move parts of a notebook.\n",
" - You can also use Shift + M to merge multiple cells."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
},
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"metadata": {
"collapsed": false
},
"source": []
}
}
},
"nbformat": 4,
"nbformat_minor": 1
}