PageSourceSearch

https://ax.dev/assets/js/0f54b9a5.80b54717.js

js ax.dev collected 2026-09-24 10:34:43 UTC 9,266 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunk=self.webpackChunk||[]).push([[5147],{28453(e,n,a){a.d(n,{R:()=>s,x:()=>r});var t=a(96540);const i={},o=t.createContext(i);function s(e){const n=t.useContext(o);return t.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function r(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:s(e.components),t.createElement(o.Provider,{value:n},e.children)}},41781(e,n,a){a.r(n),a.d(n,{assets:()=>l,contentTitle:()=>r,default:()=>h,frontMatter:()=>s,metadata:()=>t,toc:()=>d});const t=JSON.parse('{"id":"recipes/experiment-to-sqlite","title":"Saving and Loading an Ax Experiment to Your SQL Database","description":"Ax provides a convenient way to save and load experiments in SQL format, making","source":"@site/versioned_docs/version-1.3.1/recipes/experiment-to-sqlite.md","sourceDirName":"recipes","slug":"/recipes/experiment-to-sqlite","permalink":"/docs/recipes/experiment-to-sqlite","draft":false,"unlisted":false,"tags":[],"version":"1.3.1","lastUpdatedBy":"github-actions[bot]","lastUpdatedAt":1781025267000,"frontMatter":{},"sidebar":"tutorials","previous":{"title":"Saving and Loading an Ax Experiment to JSON","permalink":"/docs/recipes/experiment-to-json"},"next":{"title":"Multi-Objective Optimization with Ax","permalink":"/docs/recipes/multi-objective-optimization"}}');var i=a(74848),o=a(28453);const s={},r="Saving and Loading an Ax Experiment to Your SQL Database",l={},d=[{value:"Introduction",id:"introduction",level:2},{value:"Steps",id:"steps",level:2},{value:"Saving to a SQL database",id:"saving-to-a-sql-database",level:3},{value:"1. Initialize a Client with a StorageConfig, and configure it with an experiment",id:"1-initialize-a-client-with-a-storageconfig-and-configure-it-with-an-experiment",level:4},{value:"2. View the state of the attached experiment via <code>summarize()</code>",id:"2-view-the-state-of-the-attached-experiment-via-summarize",level:4},{value:"Load an Experiment from a SQL database",id:"load-an-experiment-from-a-sql-database",level:3},{value:"1. Call load_from_database to initialize a new Client",id:"1-call-load_from_database-to-initialize-a-new-client",level:4},{value:"2. Validate the state of the attached experiment via <code>summarize()</code>",id:"2-validate-the-state-of-the-attached-experiment-via-summarize",level:4},{value:"Customizing the Serialization Process",id:"customizing-the-serialization-process",level:3},{value:"Learn more",id:"learn-more",level:2}];function c(e){const n={a:"a",code:"code",h1:"h1",h2:"h2",h3:"h3",h4:"h4",header:"header",li:"li",ol:"ol",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,o.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.header,{children:(0,i.jsx)(n.h1,{id:"saving-and-loading-an-ax-experiment-to-your-sql-database",children:"Saving and Loading an Ax Experiment to Your SQL Database"})}),"\n",(0,i.jsx)(n.p,{children:"Ax provides a convenient way to save and load experiments in SQL format, making\nit easy to store and manage experiment data. In this recipe, we will walk\nthrough the steps of saving and loading an Ax experiment to/from your SQL\ndatabase."}),"\n",(0,i.jsx)(n.h2,{id:"introduction",children:"Introduction"}),"\n",(0,i.jsx)(n.p,{children:"Saving and loading an experiment to/from your SQL database is useful when you\nwant to store the experiment data in a structured and queryable format. This can\nbe particularly useful for production applications that require a centralized,\nhigh-performance database."}),"\n",(0,i.jsx)(n.p,{children:"The Ax Client automatically saves experiment data at every stage, including\nconfiguration and metric setup. This eliminates the need for manual saving and\nallows you to easily load your experiment data back into a Client whenever\nneeded."}),"\n",(0,i.jsx)(n.h2,{id:"steps",children:"Steps"}),"\n",(0,i.jsx)(n.p,{children:"In this recipe, we plan to save a snapshot of an Ax Client to a SQL database,\nreload the client from the SQL database instance, and validate its contents."}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"Saving to a SQL database"}),"\n",(0,i.jsxs)(n.ol,{children:["\n",(0,i.jsx)(n.li,{children:"Initialize a Client with a StorageConfig, and configure it with an\nexperiment"}),"\n",(0,i.jsxs)(n.li,{children:["View the state of the attached experiment via ",(0,i.jsx)(n.code,{children:"summarize()"})]}),"\n"]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"Loading from a SQL database"}),"\n",(0,i.jsxs)(n.ol,{children:["\n",(0,i.jsx)(n.li,{children:"Call load_from_database to initialize a new Client"}),"\n",(0,i.jsxs)(n.li,{children:["Validate the state of the attached experiment via ",(0,i.jsx)(n.code,{children:"summarize()"})]}),"\n"]}),"\n"]}),"\n"]}),"\n",(0,i.jsx)(n.h3,{id:"saving-to-a-sql-database",children:"Saving to a SQL database"}),"\n",(0,i.jsx)(n.h4,{id:"1-initialize-a-client-with-a-storageconfig-and-configure-it-with-an-experiment",children:"1. Initialize a Client with a StorageConfig, and configure it with an experiment"}),"\n",(0,i.jsxs)(n.p,{children:["Instantiate a ",(0,i.jsx)(n.code,{children:"Client"})," with a StorageConfig and configure it for your\nexperiment. In order to initialize a StorageConfig, we need a URL pointing to\nyour SQL database. Such a URL is typically composed of a dialect (e.g. sqlite,\nmysql, postgresql), optional driver (DBAPI used to connect to the database; e.g.\npsycopg2 for postgresql), username, password, hostname, and database name."]}),"\n",(0,i.jsxs)(n.p,{children:["A more detailed explanation on how to generate a URL can be found in the\n",(0,i.jsx)(n.a,{href:"https://docs.sqlalchemy.org/en/13/core/engines.html?fbclid=IwZXh0bgNhZW0CMTEAAR2abpGB86CC2iA9ZgXltBODU-tHfP_cvlXay2opsGUI70GKv2I7q2UZPDY_aem_N5fQ7unkL8WLcsbaftrQuQ#database-urls",children:"SQLAlchemy docs"})]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:'client = Client()\n\nurl = "sqlite:///path/to/database.db"\nstorage_config = StorageConfig(url = url)\n\nclient.configure_experiment(...)\nclient.configure_optimization(...)\n'})}),"\n",(0,i.jsxs)(n.h4,{id:"2-view-the-state-of-the-attached-experiment-via-summarize",children:["2. View the state of the attached experiment via ",(0,i.jsx)(n.code,{children:"summarize()"})]}),"\n",(0,i.jsx)(n.p,{children:"You can inspect the state of the experiment by leveraging the summarize()\nmethod, which returns a DataFrame"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:"client.summarize()\n"})}),"\n",(0,i.jsx)(n.h3,{id:"load-an-experiment-from-a-sql-database",children:"Load an Experiment from a SQL database"}),"\n",(0,i.jsx)(n.h4,{id:"1-call-load_from_database-to-initialize-a-new-client",children:"1. Call load_from_database to initialize a new Client"}),"\n",(0,i.jsxs)(n.p,{children:["We will now load the previously saved Client snapshot into a 
1new one. We will do\nthis by calling ",(0,i.jsx)(n.code,{children:"load_from_database"}),", and passing it the experiment name and\nStorageConfig we created in the previous section"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:"new_client = Client.load_from_database(\n    experiment_name = experiment_name,\n    storage_config = storage_config\n)\n"})}),"\n",(0,i.jsxs)(n.h4,{id:"2-validate-the-state-of-the-attached-experiment-via-summarize",children:["2. Validate the state of the attached experiment via ",(0,i.jsx)(n.code,{children:"summarize()"})]}),"\n",(0,i.jsxs)(n.p,{children:["We can now view the state of the experiment by calling ",(0,i.jsx)(n.code,{children:"summarize()"}),", and\nvalidate that it is the same as the one we saved in the earlier section"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:"new_client.summarize()\n"})}),"\n",(0,i.jsx)(n.h3,{id:"customizing-the-serialization-process",children:"Customizing the Serialization Process"}),"\n",(0,i.jsxs)(n.p,{children:["If you have custom metrics or runners that you want to ensure are saved to your\nSQL database properly, you can initialize the ",(0,i.jsx)(n.code,{children:"Client"})," with a ",(0,i.jsx)(n.code,{children:"StorageConfig"}),"\nthat contains a ",(0,i.jsx)(n.code,{children:"RegistryBundle"}),", that bundles together encoding and decoding\nlogic for use in the save/load functions."]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:"storage_config = StorageConfig(\n    registry_bundle = RegistryBundle(\n        runner_clss={MyRunner: None},\n        metric_clss={MyMetric: None},\n    )\n)\n\nclient = Client(\n    url = url,\n    storage_config = storage_config)\n"})}),"\n",(0,i.jsx)(n.h2,{id:"learn-more",children:"Learn more"}),"\n",(0,i.jsx)(n.p,{children:"Take a look at these other recipes to continue your learning:"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"/docs/recipes/experiment-to-json",children:"Saving and Loading an Ax Experiment to JSON"})}),"\n"]})]})}function h(e={}){const{wrapper:n}={...(0,o.R)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(c,{...e})}):c(e)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.