MilesCranmer commited on
Commit
d3c6385
1 Parent(s): d70ae0c

Update colab notebook

Browse files
Files changed (1) hide show
  1. examples/pysr_demo.ipynb +13 -102
examples/pysr_demo.ipynb CHANGED
@@ -15,68 +15,9 @@
15
  "id": "tQ1r1bbb0yBv"
16
  },
17
  "source": [
18
- "\n",
19
  "## Instructions\n",
20
  "1. Work on a copy of this notebook: _File_ > _Save a copy in Drive_ (you will need a Google account).\n",
21
- "2. (Optional) If you would like to do the deep learning component of this tutorial, turn on the GPU with Edit->Notebook settings->Hardware accelerator->GPU\n",
22
- "3. Execute the following cell (click on it and press Ctrl+Enter) to install Julia. This may take a minute or so.\n",
23
- "4. Continue to the next section.\n",
24
- "\n",
25
- "_Notes_:\n",
26
- "* If your Colab Runtime gets reset (e.g., due to inactivity), repeat steps 3, 4.\n",
27
- "* After installation, if you want to change the Julia version or activate/deactivate the GPU, you will need to reset the Runtime: _Runtime_ > _Delete and disconnect runtime_ and repeat steps 2-4."
28
- ]
29
- },
30
- {
31
- "cell_type": "markdown",
32
- "metadata": {
33
- "id": "COndi88gbDgO"
34
- },
35
- "source": [
36
- "**Run the following code to install Julia**"
37
- ]
38
- },
39
- {
40
- "cell_type": "code",
41
- "execution_count": null,
42
- "metadata": {
43
- "colab": {
44
- "base_uri": "https://localhost:8080/"
45
- },
46
- "id": "GIeFXS0F0zww",
47
- "outputId": "5399ed75-f77f-47c5-e53b-4b2f231f2839"
48
- },
49
- "outputs": [],
50
- "source": [
51
- "!curl -fsSL https://install.julialang.org | sh -s -- -y --default-channel 1.10"
52
- ]
53
- },
54
- {
55
- "cell_type": "code",
56
- "execution_count": null,
57
- "metadata": {
58
- "colab": {
59
- "base_uri": "https://localhost:8080/"
60
- },
61
- "id": "Iu9X-Y-YNmwM",
62
- "outputId": "ee14af65-043a-4ad6-efa0-3cdcc48a4eb8"
63
- },
64
- "outputs": [],
65
- "source": [
66
- "# Make julia available on PATH:\n",
67
- "!ln -s $HOME/.juliaup/bin/julia /usr/local/bin/julia\n",
68
- "\n",
69
- "# Test it works:\n",
70
- "!julia --version"
71
- ]
72
- },
73
- {
74
- "cell_type": "markdown",
75
- "metadata": {
76
- "id": "ORv1c6xvbDgV"
77
- },
78
- "source": [
79
- "Install PySR"
80
  ]
81
  },
82
  {
@@ -91,36 +32,23 @@
91
  },
92
  "outputs": [],
93
  "source": [
94
- "!pip install pysr && python -m pysr install"
95
  ]
96
  },
97
  {
98
  "cell_type": "markdown",
99
- "metadata": {
100
- "id": "etTMEV0wDqld"
101
- },
102
  "source": [
103
- "Colab's printing is non-standard, so we need to manually initialize Julia and redirect its printing. Normally, however, this is not required, and PySR will automatically start Julia during the first call to `.fit`:"
104
  ]
105
  },
106
  {
107
  "cell_type": "code",
108
  "execution_count": null,
109
- "metadata": {
110
- "id": "j666aOI8xWF_"
111
- },
112
  "outputs": [],
113
  "source": [
114
- "def init_colab_printing():\n",
115
- " from pysr.julia_helpers import init_julia\n",
116
- " from julia.tools import redirect_output_streams\n",
117
- "\n",
118
- " julia_kwargs = dict(optimize=3, threads=\"auto\", compiled_modules=False)\n",
119
- " init_julia(julia_kwargs=julia_kwargs)\n",
120
- " redirect_output_streams()\n",
121
- "\n",
122
- "\n",
123
- "init_colab_printing()"
124
  ]
125
  },
126
  {
@@ -129,7 +57,7 @@
129
  "id": "qeCPKd9wldEK"
130
  },
131
  "source": [
132
- "Now, let's import all of our libraries:"
133
  ]
134
  },
135
  {
@@ -815,26 +743,7 @@
815
  "where $p_i$ is the $i$th prime number, and $x$ is the input feature.\n",
816
  "\n",
817
  "Let's see if we can discover this using\n",
818
- "the [Primes.jl](https://github.com/JuliaMath/Primes.jl) package.\n",
819
- "\n",
820
- "First, let's get the Julia backend\n",
821
- "Here, we might choose to manually specify unlimited threads, `-O3`,\n",
822
- "and `compile_modules=False`, although this will only propagate if Julia has not yet started:"
823
- ]
824
- },
825
- {
826
- "cell_type": "code",
827
- "execution_count": null,
828
- "metadata": {
829
- "id": "yUC4BMuHG-KN"
830
- },
831
- "outputs": [],
832
- "source": [
833
- "import pysr\n",
834
- "\n",
835
- "jl = pysr.julia_helpers.init_julia(\n",
836
- " julia_kwargs=dict(optimize=3, threads=\"auto\", compiled_modules=False)\n",
837
- ")"
838
  ]
839
  },
840
  {
@@ -859,7 +768,9 @@
859
  },
860
  "outputs": [],
861
  "source": [
862
- "jl.eval(\n",
 
 
863
  " \"\"\"\n",
864
  "import Pkg\n",
865
  "Pkg.add(\"Primes\")\n",
@@ -885,7 +796,7 @@
885
  },
886
  "outputs": [],
887
  "source": [
888
- "jl.eval(\"import Primes\")"
889
  ]
890
  },
891
  {
@@ -906,7 +817,7 @@
906
  },
907
  "outputs": [],
908
  "source": [
909
- "jl.eval(\n",
910
  " \"\"\"\n",
911
  "function p(i::T) where T\n",
912
  " if 0.5 < i < 1000\n",
 
15
  "id": "tQ1r1bbb0yBv"
16
  },
17
  "source": [
 
18
  "## Instructions\n",
19
  "1. Work on a copy of this notebook: _File_ > _Save a copy in Drive_ (you will need a Google account).\n",
20
+ "2. (Optional) If you would like to do the deep learning component of this tutorial, turn on the GPU with Edit->Notebook settings->Hardware accelerator->GPU\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  ]
22
  },
23
  {
 
32
  },
33
  "outputs": [],
34
  "source": [
35
+ "!pip install -U pysr"
36
  ]
37
  },
38
  {
39
  "cell_type": "markdown",
40
+ "metadata": {},
 
 
41
  "source": [
42
+ "Julia and Julia dependencies are installed at first import:"
43
  ]
44
  },
45
  {
46
  "cell_type": "code",
47
  "execution_count": null,
48
+ "metadata": {},
 
 
49
  "outputs": [],
50
  "source": [
51
+ "import pysr"
 
 
 
 
 
 
 
 
 
52
  ]
53
  },
54
  {
 
57
  "id": "qeCPKd9wldEK"
58
  },
59
  "source": [
60
+ "Now, let's import everything else as well as the PySRRegressor:\n"
61
  ]
62
  },
63
  {
 
743
  "where $p_i$ is the $i$th prime number, and $x$ is the input feature.\n",
744
  "\n",
745
  "Let's see if we can discover this using\n",
746
+ "the [Primes.jl](https://github.com/JuliaMath/Primes.jl) package."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
747
  ]
748
  },
749
  {
 
768
  },
769
  "outputs": [],
770
  "source": [
771
+ "from pysr import jl\n",
772
+ "\n",
773
+ "jl.seval(\n",
774
  " \"\"\"\n",
775
  "import Pkg\n",
776
  "Pkg.add(\"Primes\")\n",
 
796
  },
797
  "outputs": [],
798
  "source": [
799
+ "jl.seval(\"import Primes\")"
800
  ]
801
  },
802
  {
 
817
  },
818
  "outputs": [],
819
  "source": [
820
+ "jl.seval(\n",
821
  " \"\"\"\n",
822
  "function p(i::T) where T\n",
823
  " if 0.5 < i < 1000\n",