Skip to content

Instantly share code, notes, and snippets.

@ricardoV94
Last active March 13, 2024 12:42
Show Gist options
  • Save ricardoV94/8b1f4361e50c05a95cb65058ce7536d5 to your computer and use it in GitHub Desktop.
Save ricardoV94/8b1f4361e50c05a95cb65058ce7536d5 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"source": [
"## Sampling models in background in a Jupyter environment"
],
"metadata": {
"collapsed": false
},
"id": "db25f827f8e4d616"
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Starting 1 engines with <class 'ipyparallel.cluster.launcher.LocalEngineSetLauncher'>\n",
"100%|██████████| 1/1 [00:05<00:00, 5.78s/engine]\n"
]
},
{
"data": {
"text/plain": "<AsyncResult(use_cloudpickle): pending>"
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import time\n",
"import ipyparallel as ipp\n",
"\n",
"rc = ipp.Cluster(n=1).start_and_connect_sync()\n",
"dv = rc[:]\n",
"dv.use_cloudpickle()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-13T12:41:26.791127Z",
"start_time": "2024-03-13T12:41:19.684806Z"
}
},
"id": "5323323bb42d880d",
"execution_count": 1
},
{
"cell_type": "code",
"outputs": [],
"source": [
"import pymc as pm"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-13T12:41:29.202533Z",
"start_time": "2024-03-13T12:41:26.792834Z"
}
},
"id": "b0e3d3beb75f1273",
"execution_count": 2
},
{
"cell_type": "code",
"outputs": [],
"source": [
"@dv.remote(block=False)\n",
"def sample(model):\n",
" return pm.sample(model=model, draws=5_000, progressbar=True)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-13T12:41:29.206844Z",
"start_time": "2024-03-13T12:41:29.203651Z"
}
},
"id": "bf90b72c692eeecb",
"execution_count": 3
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "<AsyncResult(sample): pending>"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"with pm.Model() as m:\n",
" x = pm.Normal('x', mu=0, sigma=1)\n",
" y = pm.Normal('y', mu=x, sigma=1, observed=[-2, -1, 0, 1, 2])\n",
" \n",
"idata_res = sample(m)\n",
"idata_res"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-13T12:41:29.239845Z",
"start_time": "2024-03-13T12:41:29.208612Z"
}
},
"id": "4af7a7ed81347bdf",
"execution_count": 4
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "[{'msg_id': None,\n 'submitted': datetime.datetime(2024, 3, 13, 12, 41, 29, 234459, tzinfo=datetime.timezone.utc),\n 'started': None,\n 'completed': None,\n 'received': None,\n 'engine_uuid': 'f8f055cb-560482594b93e99858d3e1b4',\n 'engine_id': 0,\n 'follow': None,\n 'after': None,\n 'status': None,\n 'execute_input': None,\n 'execute_result': None,\n 'error': None,\n 'stdout': ' |██████████----------------------| 32.08% [7700/24000 00:01<00:03 Sampling 4 chains, 0 divergences]\\r',\n 'stderr': 'Auto-assigning NUTS sampler...\\nInitializing NUTS using jitter+adapt_diag...\\nMultiprocess sampling (4 chains in 4 jobs)\\nNUTS: [x]\\n',\n 'outputs': [],\n 'data': {}}]"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"time.sleep(5)\n",
"idata_res.metadata"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-13T12:41:34.246870Z",
"start_time": "2024-03-13T12:41:29.241601Z"
}
},
"id": "12e2bb318bcadd6",
"execution_count": 5
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Doing some computation in the meantime\n"
]
},
{
"data": {
"text/plain": "{'x': -0.92, 'y': -9.59}"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Doing some computation in the meantime\")\n",
"time.sleep(2)\n",
"m.point_logps()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-13T12:41:36.601897Z",
"start_time": "2024-03-13T12:41:34.248705Z"
}
},
"id": "fb6c4f5285dfb436",
"execution_count": 6
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "<AsyncResult(sample): pending>"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"idata_res"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-13T12:41:36.613538Z",
"start_time": "2024-03-13T12:41:36.603735Z"
}
},
"id": "7f9faff17d3d7679",
"execution_count": 7
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "[{'msg_id': None,\n 'submitted': datetime.datetime(2024, 3, 13, 12, 41, 29, 234459, tzinfo=datetime.timezone.utc),\n 'started': None,\n 'completed': None,\n 'received': None,\n 'engine_uuid': 'f8f055cb-560482594b93e99858d3e1b4',\n 'engine_id': 0,\n 'follow': None,\n 'after': None,\n 'status': None,\n 'execute_input': None,\n 'execute_result': None,\n 'error': None,\n 'stdout': ' |███████████████████████████----| 87.16% [20919/24000 00:03<00:00 Sampling 4 chains, 0 divergences]\\r',\n 'stderr': 'Auto-assigning NUTS sampler...\\nInitializing NUTS using jitter+adapt_diag...\\nMultiprocess sampling (4 chains in 4 jobs)\\nNUTS: [x]\\n',\n 'outputs': [],\n 'data': {}}]"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"idata_res.metadata"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-13T12:41:36.623724Z",
"start_time": "2024-03-13T12:41:36.616070Z"
}
},
"id": "ee20ec1caafed268",
"execution_count": 8
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "[Inference data with groups:\n \t> posterior\n \t> sample_stats\n \t> observed_data]"
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"idata_res.get()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-13T12:41:45.966791Z",
"start_time": "2024-03-13T12:41:36.625464Z"
}
},
"id": "5c60d2735dde4285",
"execution_count": 9
}
],
"metadata": {
"kernelspec": {
"name": "pymc-examples",
"language": "python",
"display_name": "pymc-examples"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment