|
563 | 563 | "1 + L(2,3,4)"
|
564 | 564 | ]
|
565 | 565 | },
|
566 |
| - { |
567 |
| - "cell_type": "markdown", |
568 |
| - "metadata": {}, |
569 |
| - "source": [ |
570 |
| - "### Transforms" |
571 |
| - ] |
572 |
| - }, |
573 |
| - { |
574 |
| - "cell_type": "markdown", |
575 |
| - "metadata": {}, |
576 |
| - "source": [ |
577 |
| - "A `Transform` is the main building block of the fastai data pipelines. In the most general terms a transform can be any function you want to apply to your data, however the `Transform` class provides several mechanisms that make the process of building them easy and flexible (see the docs for information about each of these):\n", |
578 |
| - "\n", |
579 |
| - "- Type dispatch\n", |
580 |
| - "- Dispatch over tuples\n", |
581 |
| - "- Reversability\n", |
582 |
| - "- Type propagation\n", |
583 |
| - "- Preprocessing\n", |
584 |
| - "- Filtering based on the dataset type\n", |
585 |
| - "- Ordering\n", |
586 |
| - "- Appending new behavior with decorators\n", |
587 |
| - "\n", |
588 |
| - "`Transform` looks for three special methods, <code>encodes</code>, <code>decodes</code>, and <code>setups</code>, which provide the implementation for [`__call__`](https://www.python-course.eu/python3_magic_methods.php), `decode`, and `setup` respectively. For instance:" |
589 |
| - ] |
590 |
| - }, |
591 |
| - { |
592 |
| - "cell_type": "code", |
593 |
| - "execution_count": null, |
594 |
| - "metadata": {}, |
595 |
| - "outputs": [ |
596 |
| - { |
597 |
| - "data": { |
598 |
| - "text/plain": [ |
599 |
| - "2" |
600 |
| - ] |
601 |
| - }, |
602 |
| - "execution_count": null, |
603 |
| - "metadata": {}, |
604 |
| - "output_type": "execute_result" |
605 |
| - } |
606 |
| - ], |
607 |
| - "source": [ |
608 |
| - "class A(Transform):\n", |
609 |
| - " def encodes(self, x): return x+1\n", |
610 |
| - "\n", |
611 |
| - "A()(1)" |
612 |
| - ] |
613 |
| - }, |
614 |
| - { |
615 |
| - "cell_type": "markdown", |
616 |
| - "metadata": {}, |
617 |
| - "source": [ |
618 |
| - "For simple transforms like this, you can also use `Transform` as a decorator:" |
619 |
| - ] |
620 |
| - }, |
621 |
| - { |
622 |
| - "cell_type": "code", |
623 |
| - "execution_count": null, |
624 |
| - "metadata": {}, |
625 |
| - "outputs": [ |
626 |
| - { |
627 |
| - "data": { |
628 |
| - "text/plain": [ |
629 |
| - "2" |
630 |
| - ] |
631 |
| - }, |
632 |
| - "execution_count": null, |
633 |
| - "metadata": {}, |
634 |
| - "output_type": "execute_result" |
635 |
| - } |
636 |
| - ], |
637 |
| - "source": [ |
638 |
| - "@Transform\n", |
639 |
| - "def f(x): return x+1\n", |
640 |
| - "\n", |
641 |
| - "f(1)" |
642 |
| - ] |
643 |
| - }, |
644 |
| - { |
645 |
| - "cell_type": "markdown", |
646 |
| - "metadata": {}, |
647 |
| - "source": [ |
648 |
| - "Transforms can be composed into a `Pipeline`:" |
649 |
| - ] |
650 |
| - }, |
651 |
| - { |
652 |
| - "cell_type": "code", |
653 |
| - "execution_count": null, |
654 |
| - "metadata": {}, |
655 |
| - "outputs": [ |
656 |
| - { |
657 |
| - "data": { |
658 |
| - "text/plain": [ |
659 |
| - "2.0" |
660 |
| - ] |
661 |
| - }, |
662 |
| - "execution_count": null, |
663 |
| - "metadata": {}, |
664 |
| - "output_type": "execute_result" |
665 |
| - } |
666 |
| - ], |
667 |
| - "source": [ |
668 |
| - "@Transform\n", |
669 |
| - "def g(x): return x/2\n", |
670 |
| - "\n", |
671 |
| - "pipe = Pipeline([f,g])\n", |
672 |
| - "pipe(3)" |
673 |
| - ] |
674 |
| - }, |
675 |
| - { |
676 |
| - "cell_type": "markdown", |
677 |
| - "metadata": {}, |
678 |
| - "source": [ |
679 |
| - "The power of `Transform` and `Pipeline` is best understood by seeing how they're used to create a complete data processing pipeline. This is explained in [chapter 11](https://github.com/fastai/fastbook/blob/master/11_midlevel_data.ipynb) of the [fastai book](https://www.amazon.com/Deep-Learning-Coders-fastai-PyTorch/dp/1492045527), which is [available for free](https://github.com/fastai/fastbook) in Jupyter Notebook format." |
680 |
| - ] |
681 |
| - }, |
682 | 566 | {
|
683 | 567 | "cell_type": "code",
|
684 | 568 | "execution_count": null,
|
|
0 commit comments