|
49 | 49 | "## Change runtime to use a GPU\n", |
50 | 50 | "\n", |
51 | 51 | "This tutorial is much faster when a GPU is available to run the computations.\n", |
52 | | - "In Google Colab you can request access to a GPU by changing the runtime type. \n", |
53 | | - "To do so, click the following menu options in Google Colab: \n", |
| 52 | + "In Google Colab you can request access to a GPU by changing the runtime type.\n", |
| 53 | + "To do so, click the following menu options in Google Colab:\n", |
54 | 54 | "\n", |
55 | 55 | "(Menu) \"Runtime\" -> \"Change runtime type\" -> \"Hardware accelerator\" -> \"GPU\".\n", |
56 | 56 | "\n" |
|
63 | 63 | "## Download the data and install all required dependencies\n", |
64 | 64 | "\n", |
65 | 65 | "Uncomment and run the following cell to download the tutorial data and\n", |
66 | | - "install the required dependencies\n", |
| 66 | + "install the required dependencies.\n", |
67 | 67 | "\n" |
68 | 68 | ] |
69 | 69 | }, |
|
75 | 75 | }, |
76 | 76 | "outputs": [], |
77 | 77 | "source": [ |
78 | | - "# !gdown --id 1b0I0Ytj06m6GCmfxfNrZuyF97fDo3NZb && \\\n", |
79 | | - "# tar xzf vim-5-for-ccn.tar.gz && \\\n", |
80 | | - "# pip install -q voxelwise_tutorials && \\\n", |
81 | | - "# git clone https://github.com/gallantlab/pycortex" |
| 78 | + "# ![ -f \"vim-5-for-ccn.tar.gz\" ] || gdown --id 1b0I0Ytj06m6GCmfxfNrZuyF97fDo3NZb\n", |
| 79 | + "# ![ -d \"vim-5\" ] || tar xzf vim-5-for-ccn.tar.gz\n", |
| 80 | + "# ![ -d \"pycortex\" ] || git clone https://github.com/gallantlab/pycortex\n", |
| 81 | + "# !pip install -q voxelwise_tutorials" |
82 | 82 | ] |
83 | 83 | }, |
84 | 84 | { |
85 | 85 | "cell_type": "markdown", |
86 | 86 | "metadata": {}, |
87 | 87 | "source": [ |
88 | | - "Now run the following cell to set up the environment variables for the tutorials\n", |
89 | | - "and pycortex.\n", |
| 88 | + "Now run the following cell to set up the environment variables for the\n", |
| 89 | + "tutorials and pycortex.\n", |
90 | 90 | "\n" |
91 | 91 | ] |
92 | 92 | }, |
|
2781 | 2781 | "print(\"(n_repeats, n_samples_test, n_voxels) =\", Y_test.shape)" |
2782 | 2782 | ] |
2783 | 2783 | }, |
| 2784 | + { |
| 2785 | + "cell_type": "markdown", |
| 2786 | + "metadata": {}, |
| 2787 | + "source": [ |
| 2788 | + "We also compute the explainable variance, to exclude voxels with low\n", |
| 2789 | + "explainable variance from the fit, and speed up the model fitting.\n", |
| 2790 | + "\n" |
| 2791 | + ] |
| 2792 | + }, |
| 2793 | + { |
| 2794 | + "cell_type": "code", |
| 2795 | + "execution_count": null, |
| 2796 | + "metadata": { |
| 2797 | + "collapsed": false |
| 2798 | + }, |
| 2799 | + "outputs": [], |
| 2800 | + "source": [ |
| 2801 | + "from voxelwise_tutorials.utils import explainable_variance\n", |
| 2802 | + "ev = explainable_variance(Y_test)\n", |
| 2803 | + "print(\"(n_voxels,) =\", ev.shape)\n", |
| 2804 | + "\n", |
| 2805 | + "mask = ev > 0.1\n", |
| 2806 | + "print(\"(n_voxels_mask,) =\", ev[mask].shape)" |
| 2807 | + ] |
| 2808 | + }, |
2784 | 2809 | { |
2785 | 2810 | "cell_type": "markdown", |
2786 | 2811 | "metadata": {}, |
|
3203 | 3228 | "\n", |
3204 | 3229 | "We fit on the train set, and score on the test set.\n", |
3205 | 3230 | "\n", |
| 3231 | + "To speed up the fit and to limit the memory peak in Colab, we only fit on\n", |
| 3232 | + "voxels with explainable variance above 0.1.\n", |
| 3233 | + "\n", |
3206 | 3234 | "With a GPU backend, the fitting of this model takes around 6 minutes. With a\n", |
3207 | 3235 | "CPU backend, it can last 10 times more.\n", |
3208 | 3236 | "\n" |
|
3216 | 3244 | }, |
3217 | 3245 | "outputs": [], |
3218 | 3246 | "source": [ |
3219 | | - "pipeline.fit(X_train, Y_train)\n", |
| 3247 | + "pipeline.fit(X_train, Y_train[:, mask])\n", |
3220 | 3248 | "\n", |
3221 | | - "scores = pipeline.score(X_test, Y_test)\n", |
3222 | | - "scores = backend.to_numpy(scores)\n", |
| 3249 | + "scores_mask = pipeline.score(X_test, Y_test[:, mask])\n", |
| 3250 | + "scores_mask = backend.to_numpy(scores_mask)\n", |
| 3251 | + "print(\"(n_voxels_mask,) =\", scores_mask.shape)\n", |
3223 | 3252 | "\n", |
3224 | | - "print(\"(n_voxels,) =\", scores.shape)" |
| 3253 | + "# Then we extend the scores to all voxels, giving a score of zero to unfitted\n", |
| 3254 | + "# voxels.\n", |
| 3255 | + "n_voxels = Y_train.shape[1]\n", |
| 3256 | + "scores = np.zeros(n_voxels)\n", |
| 3257 | + "scores[mask] = scores_mask" |
3225 | 3258 | ] |
3226 | 3259 | }, |
3227 | 3260 | { |
|
3266 | 3299 | }, |
3267 | 3300 | "outputs": [], |
3268 | 3301 | "source": [ |
3269 | | - "pipeline_baseline.fit(X_train, Y_train)\n", |
3270 | | - "scores_baseline = pipeline_baseline.score(X_test, Y_test)\n", |
3271 | | - "scores_baseline = backend.to_numpy(scores_baseline)" |
| 3302 | + "pipeline_baseline.fit(X_train, Y_train[:, mask])\n", |
| 3303 | + "scores_baseline_mask = pipeline_baseline.score(X_test, Y_test[:, mask])\n", |
| 3304 | + "scores_baseline_mask = backend.to_numpy(scores_baseline_mask)\n", |
| 3305 | + "\n", |
| 3306 | + "# extend to unfitted voxels\n", |
| 3307 | + "n_voxels = Y_train.shape[1]\n", |
| 3308 | + "scores_baseline = np.zeros(n_voxels)\n", |
| 3309 | + "scores_baseline[mask] = scores_baseline_mask" |
3272 | 3310 | ] |
3273 | 3311 | }, |
3274 | 3312 | { |
3275 | 3313 | "cell_type": "markdown", |
3276 | 3314 | "metadata": {}, |
3277 | 3315 | "source": [ |
3278 | | - "Here we plot the comparison of model prediction accuracies with a 2D histogram.\n", |
3279 | | - "All 70k voxels are represented in this histogram, where the diagonal\n", |
3280 | | - "corresponds to identical model prediction accuracy for both models. A distibution deviating\n", |
3281 | | - "from the diagonal means that one model has better predictive performance\n", |
3282 | | - "than the other.\n", |
| 3316 | + "Here we plot the comparison of model prediction accuracies with a 2D\n", |
| 3317 | + "histogram. All 70k voxels are represented in this histogram, where the\n", |
| 3318 | + "diagonal corresponds to identical model prediction accuracy for both models.\n", |
| 3319 | + "A distibution deviating from the diagonal means that one model has better\n", |
| 3320 | + "predictive performance than the other.\n", |
3283 | 3321 | "\n" |
3284 | 3322 | ] |
3285 | 3323 | }, |
|
3344 | 3382 | "from himalaya.scoring import r2_score_split\n", |
3345 | 3383 | "\n", |
3346 | 3384 | "Y_test_pred_split = pipeline.predict(X_test, split=True)\n", |
3347 | | - "split_scores = r2_score_split(Y_test, Y_test_pred_split)\n", |
3348 | | - "split_scores.shape\n", |
| 3385 | + "split_scores_mask = r2_score_split(Y_test[:, mask], Y_test_pred_split)\n", |
| 3386 | + "\n", |
| 3387 | + "print(\"(n_kernels, n_samples_test, n_voxels_mask) =\", Y_test_pred_split.shape)\n", |
| 3388 | + "print(\"(n_kernels, n_voxels_mask) =\", split_scores_mask.shape)\n", |
3349 | 3389 | "\n", |
3350 | | - "print(\"(n_kernels, n_samples_test, n_voxels) =\", Y_test_pred_split.shape)\n", |
| 3390 | + "# extend to unfitted voxels\n", |
| 3391 | + "n_kernels = split_scores_mask.shape[0]\n", |
| 3392 | + "n_voxels = Y_train.shape[1]\n", |
| 3393 | + "split_scores = np.zeros((n_kernels, n_voxels))\n", |
| 3394 | + "split_scores[:, mask] = split_scores_mask\n", |
3351 | 3395 | "print(\"(n_kernels, n_voxels) =\", split_scores.shape)" |
3352 | 3396 | ] |
3353 | 3397 | }, |
|
3381 | 3425 | "cell_type": "markdown", |
3382 | 3426 | "metadata": {}, |
3383 | 3427 | "source": [ |
3384 | | - "The blue regions are better predicted by the motion-energy features, the orange\n", |
3385 | | - "regions are better predicted by the wordnet features, and the white regions are\n", |
3386 | | - "well predicted by both feature spaces.\n", |
| 3428 | + "The blue regions are better predicted by the motion-energy features, the\n", |
| 3429 | + "orange regions are better predicted by the wordnet features, and the white\n", |
| 3430 | + "regions are well predicted by both feature spaces.\n", |
3387 | 3431 | "\n", |
3388 | 3432 | "Compared to the last figure of the previous example, we see that most white\n", |
3389 | | - "regions have been replaced by either blue or orange regions. The\n", |
3390 | | - "banded ridge regression disentangled the two feature spaces in voxels where\n", |
3391 | | - "both feature spaces had good prediction accuracy (see previous example). For\n", |
3392 | | - "example, motion-energy features predict brain activity in early visual\n", |
3393 | | - "cortex, while wordnet features predict in semantic visual areas. For more\n", |
3394 | | - "discussions about these results, we refer the reader to the original\n", |
3395 | | - "publication [1]_.\n", |
| 3433 | + "regions have been replaced by either blue or orange regions. The banded ridge\n", |
| 3434 | + "regression disentangled the two feature spaces in voxels where both feature\n", |
| 3435 | + "spaces had good prediction accuracy (see previous example). For example,\n", |
| 3436 | + "motion-energy features predict brain activity in early visual cortex, while\n", |
| 3437 | + "wordnet features predict in semantic visual areas. For more discussions about\n", |
| 3438 | + "these results, we refer the reader to the original publication [1]_.\n", |
3396 | 3439 | "\n" |
3397 | 3440 | ] |
3398 | 3441 | }, |
|
0 commit comments