|
653 | 653 | }, |
654 | 654 | { |
655 | 655 | "cell_type": "code", |
656 | | - "execution_count": 8, |
| 656 | + "execution_count": null, |
| 657 | + "metadata": {}, |
| 658 | + "outputs": [], |
| 659 | + "source": [ |
| 660 | + "from spotPython.light.netlightbase import NetLightBase\n", |
| 661 | + "from spotPython.utils.init import fun_control_init\n", |
| 662 | + "from spotPython.hyperdict.light_hyper_dict import LightHyperDict\n", |
| 663 | + "from spotPython.hyperparameters.values import add_core_model_to_fun_control\n", |
| 664 | + "fun_control = fun_control_init()\n", |
| 665 | + "add_core_model_to_fun_control(core_model=NetLightBase,\n", |
| 666 | + " fun_control=fun_control,\n", |
| 667 | + " hyper_dict=LightHyperDict)\n", |
| 668 | + "fun_control[\"core_model\"].__name__" |
| 669 | + ] |
| 670 | + }, |
| 671 | + { |
| 672 | + "cell_type": "code", |
| 673 | + "execution_count": null, |
| 674 | + "metadata": {}, |
| 675 | + "outputs": [], |
| 676 | + "source": [ |
| 677 | + "# check if the fun_control[\"core_model_hyper_dict\"] is a LightHyperDict\n", |
| 678 | + "isinstance(fun_control[\"core_model_hyper_dict\"], dict)" |
| 679 | + ] |
| 680 | + }, |
| 681 | + { |
| 682 | + "cell_type": "markdown", |
| 683 | + "metadata": {}, |
| 684 | + "source": [ |
| 685 | + "# Test netlightregression" |
| 686 | + ] |
| 687 | + }, |
| 688 | + { |
| 689 | + "cell_type": "code", |
| 690 | + "execution_count": 1, |
657 | 691 | "metadata": {}, |
658 | 692 | "outputs": [ |
659 | 693 | { |
660 | 694 | "name": "stderr", |
661 | 695 | "output_type": "stream", |
662 | 696 | "text": [ |
663 | | - "Seed set to 42\n" |
| 697 | + "Seed set to 42\n", |
| 698 | + "/Users/bartz/miniforge3/envs/spotCondaEnv/lib/python3.11/site-packages/lightning/pytorch/utilities/parsing.py:198: Attribute 'act_fn' is an instance of `nn.Module` and is already saved during checkpointing. It is recommended to ignore them using `self.save_hyperparameters(ignore=['act_fn'])`.\n", |
| 699 | + "GPU available: True (mps), used: True\n", |
| 700 | + "TPU available: False, using: 0 TPU cores\n", |
| 701 | + "IPU available: False, using: 0 IPUs\n", |
| 702 | + "HPU available: False, using: 0 HPUs\n", |
| 703 | + "\n", |
| 704 | + " | Name | Type | Params\n", |
| 705 | + "--------------------------------------\n", |
| 706 | + "0 | layers | Sequential | 92.0 K\n", |
| 707 | + "--------------------------------------\n", |
| 708 | + "92.0 K Trainable params\n", |
| 709 | + "0 Non-trainable params\n", |
| 710 | + "92.0 K Total params\n", |
| 711 | + "0.368 Total estimated model params size (MB)\n" |
| 712 | + ] |
| 713 | + }, |
| 714 | + { |
| 715 | + "name": "stdout", |
| 716 | + "output_type": "stream", |
| 717 | + "text": [ |
| 718 | + "| name | type | default | lower | upper | transform |\n", |
| 719 | + "|----------------|--------|-----------|---------|---------|-----------------------|\n", |
| 720 | + "| l1 | int | 3 | 5 | 8 | transform_power_2_int |\n", |
| 721 | + "| epochs | int | 4 | 6 | 13 | transform_power_2_int |\n", |
| 722 | + "| batch_size | int | 4 | 2 | 8 | transform_power_2_int |\n", |
| 723 | + "| act_fn | factor | ReLU | 0 | 5 | None |\n", |
| 724 | + "| optimizer | factor | SGD | 0 | 3 | None |\n", |
| 725 | + "| dropout_prob | float | 0.01 | 0 | 0.25 | None |\n", |
| 726 | + "| lr_mult | float | 1.0 | 0.1 | 10 | None |\n", |
| 727 | + "| patience | int | 2 | 2 | 6 | transform_power_2_int |\n", |
| 728 | + "| initialization | factor | Default | 0 | 2 | None |\n", |
| 729 | + "fun: Calling train_model\n", |
| 730 | + "torch.Size([2381, 133])\n", |
| 731 | + "torch.Size([2381])\n", |
| 732 | + "train_model(): Test set size: 1429\n" |
| 733 | + ] |
| 734 | + }, |
| 735 | + { |
| 736 | + "name": "stderr", |
| 737 | + "output_type": "stream", |
| 738 | + "text": [ |
| 739 | + "/Users/bartz/miniforge3/envs/spotCondaEnv/lib/python3.11/site-packages/lightning/pytorch/trainer/connectors/data_connector.py:436: Consider setting `persistent_workers=True` in 'val_dataloader' to speed up the dataloader worker initialization.\n", |
| 740 | + "/Users/bartz/miniforge3/envs/spotCondaEnv/lib/python3.11/site-packages/lightning/pytorch/trainer/connectors/data_connector.py:436: Consider setting `persistent_workers=True` in 'train_dataloader' to speed up the dataloader worker initialization.\n", |
| 741 | + "/Users/bartz/miniforge3/envs/spotCondaEnv/lib/python3.11/site-packages/lightning/pytorch/loops/fit_loop.py:293: The number of training batches (12) is smaller than the logging interval Trainer(log_every_n_steps=50). Set a lower value for log_every_n_steps if you want to see logs for the training epoch.\n" |
664 | 742 | ] |
665 | 743 | }, |
666 | 744 | { |
667 | | - "data": { |
668 | | - "text/plain": [ |
669 | | - "'NetLightBase'" |
670 | | - ] |
671 | | - }, |
672 | | - "execution_count": 8, |
673 | | - "metadata": {}, |
674 | | - "output_type": "execute_result" |
| 745 | + "name": "stdout", |
| 746 | + "output_type": "stream", |
| 747 | + "text": [ |
| 748 | + "────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────\n", |
| 749 | + " Validate metric DataLoader 0\n", |
| 750 | + "────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────\n", |
| 751 | + " hp_metric 166.65289306640625\n", |
| 752 | + " val_loss 166.65289306640625\n", |
| 753 | + "────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────\n" |
| 754 | + ] |
| 755 | + }, |
| 756 | + { |
| 757 | + "name": "stderr", |
| 758 | + "output_type": "stream", |
| 759 | + "text": [ |
| 760 | + "GPU available: True (mps), used: True\n", |
| 761 | + "TPU available: False, using: 0 TPU cores\n", |
| 762 | + "IPU available: False, using: 0 IPUs\n", |
| 763 | + "HPU available: False, using: 0 HPUs\n", |
| 764 | + "\n", |
| 765 | + " | Name | Type | Params\n", |
| 766 | + "--------------------------------------\n", |
| 767 | + "0 | layers | Sequential | 5.2 K \n", |
| 768 | + "--------------------------------------\n", |
| 769 | + "5.2 K Trainable params\n", |
| 770 | + "0 Non-trainable params\n", |
| 771 | + "5.2 K Total params\n", |
| 772 | + "0.021 Total estimated model params size (MB)\n" |
| 773 | + ] |
| 774 | + }, |
| 775 | + { |
| 776 | + "name": "stdout", |
| 777 | + "output_type": "stream", |
| 778 | + "text": [ |
| 779 | + "train_model result: {'val_loss': 166.65289306640625, 'hp_metric': 166.65289306640625}\n", |
| 780 | + "fun: train_model returned\n", |
| 781 | + "fun: Calling train_model\n", |
| 782 | + "torch.Size([2381, 133])\n", |
| 783 | + "torch.Size([2381])\n", |
| 784 | + "train_model(): Test set size: 1429\n" |
| 785 | + ] |
| 786 | + }, |
| 787 | + { |
| 788 | + "name": "stderr", |
| 789 | + "output_type": "stream", |
| 790 | + "text": [ |
| 791 | + "/Users/bartz/miniforge3/envs/spotCondaEnv/lib/python3.11/site-packages/lightning/pytorch/loops/fit_loop.py:293: The number of training batches (2) is smaller than the logging interval Trainer(log_every_n_steps=50). Set a lower value for log_every_n_steps if you want to see logs for the training epoch.\n" |
| 792 | + ] |
| 793 | + }, |
| 794 | + { |
| 795 | + "name": "stdout", |
| 796 | + "output_type": "stream", |
| 797 | + "text": [ |
| 798 | + "────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────\n", |
| 799 | + " Validate metric DataLoader 0\n", |
| 800 | + "────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────\n", |
| 801 | + " hp_metric 475733504.0\n", |
| 802 | + " val_loss 475733504.0\n", |
| 803 | + "────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────\n" |
| 804 | + ] |
| 805 | + }, |
| 806 | + { |
| 807 | + "name": "stderr", |
| 808 | + "output_type": "stream", |
| 809 | + "text": [ |
| 810 | + "GPU available: True (mps), used: True\n", |
| 811 | + "TPU available: False, using: 0 TPU cores\n", |
| 812 | + "IPU available: False, using: 0 IPUs\n", |
| 813 | + "HPU available: False, using: 0 HPUs\n", |
| 814 | + "\n", |
| 815 | + " | Name | Type | Params\n", |
| 816 | + "--------------------------------------\n", |
| 817 | + "0 | layers | Sequential | 31.7 K\n", |
| 818 | + "--------------------------------------\n", |
| 819 | + "31.7 K Trainable params\n", |
| 820 | + "0 Non-trainable params\n", |
| 821 | + "31.7 K Total params\n", |
| 822 | + "0.127 Total estimated model params size (MB)\n" |
| 823 | + ] |
| 824 | + }, |
| 825 | + { |
| 826 | + "name": "stdout", |
| 827 | + "output_type": "stream", |
| 828 | + "text": [ |
| 829 | + "train_model result: {'val_loss': 475733504.0, 'hp_metric': 475733504.0}\n", |
| 830 | + "fun: train_model returned\n", |
| 831 | + "fun: Calling train_model\n", |
| 832 | + "torch.Size([2381, 133])\n", |
| 833 | + "torch.Size([2381])\n", |
| 834 | + "train_model(): Test set size: 1429\n" |
| 835 | + ] |
| 836 | + }, |
| 837 | + { |
| 838 | + "name": "stderr", |
| 839 | + "output_type": "stream", |
| 840 | + "text": [ |
| 841 | + "/Users/bartz/miniforge3/envs/spotCondaEnv/lib/python3.11/site-packages/lightning/pytorch/loops/fit_loop.py:293: The number of training batches (48) is smaller than the logging interval Trainer(log_every_n_steps=50). Set a lower value for log_every_n_steps if you want to see logs for the training epoch.\n" |
| 842 | + ] |
675 | 843 | } |
676 | 844 | ], |
677 | 845 | "source": [ |
678 | | - "from spotPython.light.netlightbase import NetLightBase\n", |
| 846 | + "import torch\n", |
| 847 | + "import numpy as np\n", |
| 848 | + "from spotPython.spot import spot\n", |
| 849 | + "from math import inf\n", |
679 | 850 | "from spotPython.utils.init import fun_control_init\n", |
| 851 | + "from spotPython.utils.file import get_experiment_name, get_spot_tensorboard_path\n", |
| 852 | + "from spotPython.utils.device import getDevice\n", |
| 853 | + "from spotPython.hyperparameters.values import set_data_set\n", |
| 854 | + "from spotPython.data.pkldataset import PKLDataset\n", |
| 855 | + "from spotPython.light.netlightregression import NetLightRegression\n", |
680 | 856 | "from spotPython.hyperdict.light_hyper_dict import LightHyperDict\n", |
681 | 857 | "from spotPython.hyperparameters.values import add_core_model_to_fun_control\n", |
682 | | - "fun_control = fun_control_init()\n", |
683 | | - "add_core_model_to_fun_control(core_model=NetLightBase,\n", |
| 858 | + "from spotPython.hyperparameters.values import modify_hyper_parameter_bounds\n", |
| 859 | + "from spotPython.hyperparameters.values import modify_hyper_parameter_levels\n", |
| 860 | + "from spotPython.fun.hyperlight import HyperLight\n", |
| 861 | + "from spotPython.hyperparameters.values import (get_bound_values,\n", |
| 862 | + " get_var_name,\n", |
| 863 | + " get_var_type,)\n", |
| 864 | + "from spotPython.utils.eda import gen_design_table\n", |
| 865 | + "from spotPython.light.utils import get_tuned_architecture\n", |
| 866 | + "from spotPython.light.traintest import test_model\n", |
| 867 | + "from spotPython.light.traintest import load_light_from_checkpoint\n", |
| 868 | + "\n", |
| 869 | + "MAX_TIME = 10\n", |
| 870 | + "INIT_SIZE = 5\n", |
| 871 | + "WORKERS = 10\n", |
| 872 | + "PREFIX=\"031\"\n", |
| 873 | + "\n", |
| 874 | + "experiment_name = get_experiment_name(prefix=PREFIX)\n", |
| 875 | + "fun_control = fun_control_init(\n", |
| 876 | + " spot_tensorboard_path=get_spot_tensorboard_path(experiment_name),\n", |
| 877 | + " num_workers=WORKERS,\n", |
| 878 | + " device=getDevice(),\n", |
| 879 | + " _L_in=133,\n", |
| 880 | + " _L_out=1,\n", |
| 881 | + " TENSORBOARD_CLEAN=True)\n", |
| 882 | + "\n", |
| 883 | + "dataset = PKLDataset(directory=\"/Users/bartz/workspace/spotPython/notebooks/data/spotPython/\", filename=\"data_sensitive.pkl\", target_column='N', feature_type=torch.float32, target_type=torch.float32, rmNA=True)\n", |
| 884 | + "set_data_set(fun_control=fun_control,\n", |
| 885 | + " data_set=dataset)\n", |
| 886 | + "\n", |
| 887 | + "\n", |
| 888 | + "\n", |
| 889 | + "add_core_model_to_fun_control(core_model=NetLightRegression,\n", |
684 | 890 | " fun_control=fun_control,\n", |
685 | 891 | " hyper_dict=LightHyperDict)\n", |
686 | | - "fun_control[\"core_model\"].__name__" |
| 892 | + "modify_hyper_parameter_bounds(fun_control, \"l1\", bounds=[5,8])\n", |
| 893 | + "modify_hyper_parameter_bounds(fun_control, \"epochs\", bounds=[6,13])\n", |
| 894 | + "modify_hyper_parameter_bounds(fun_control, \"batch_size\", bounds=[2, 8])\n", |
| 895 | + "modify_hyper_parameter_levels(fun_control, \"optimizer\",[\"Adam\", \"AdamW\", \"Adamax\", \"NAdam\"])\n", |
| 896 | + "\n", |
| 897 | + "print(gen_design_table(fun_control))\n", |
| 898 | + "\n", |
| 899 | + "var_type = get_var_type(fun_control)\n", |
| 900 | + "var_name = get_var_name(fun_control)\n", |
| 901 | + "lower = get_bound_values(fun_control, \"lower\")\n", |
| 902 | + "upper = get_bound_values(fun_control, \"upper\")\n", |
| 903 | + "fun = HyperLight(log_level=10).fun\n", |
| 904 | + "spot_tuner = spot.Spot(fun=fun,\n", |
| 905 | + " log_level=10,\n", |
| 906 | + " lower = lower,\n", |
| 907 | + " upper = upper,\n", |
| 908 | + " fun_evals = inf,\n", |
| 909 | + " max_time = MAX_TIME,\n", |
| 910 | + " tolerance_x = np.sqrt(np.spacing(1)),\n", |
| 911 | + " var_type = var_type,\n", |
| 912 | + " var_name = var_name,\n", |
| 913 | + " show_progress= True,\n", |
| 914 | + " fun_control = fun_control,\n", |
| 915 | + " design_control={\"init_size\": INIT_SIZE},\n", |
| 916 | + " surrogate_control={\"noise\": True,\n", |
| 917 | + " \"min_theta\": -4,\n", |
| 918 | + " \"max_theta\": 3,\n", |
| 919 | + " \"n_theta\": len(var_name),\n", |
| 920 | + " \"model_fun_evals\": 10_000,\n", |
| 921 | + " })\n", |
| 922 | + "spot_tuner.run()" |
687 | 923 | ] |
688 | 924 | }, |
689 | 925 | { |
690 | 926 | "cell_type": "code", |
691 | | - "execution_count": 12, |
| 927 | + "execution_count": null, |
692 | 928 | "metadata": {}, |
693 | | - "outputs": [ |
694 | | - { |
695 | | - "data": { |
696 | | - "text/plain": [ |
697 | | - "True" |
698 | | - ] |
699 | | - }, |
700 | | - "execution_count": 12, |
701 | | - "metadata": {}, |
702 | | - "output_type": "execute_result" |
703 | | - } |
704 | | - ], |
| 929 | + "outputs": [], |
705 | 930 | "source": [ |
706 | | - "# check if the fun_control[\"core_model_hyper_dict\"] is a LightHyperDict\n", |
707 | | - "isinstance(fun_control[\"core_model_hyper_dict\"], dict)" |
| 931 | + "spot_tuner.plot_progress(log_y=False, filename=None)" |
| 932 | + ] |
| 933 | + }, |
| 934 | + { |
| 935 | + "cell_type": "code", |
| 936 | + "execution_count": null, |
| 937 | + "metadata": {}, |
| 938 | + "outputs": [], |
| 939 | + "source": [ |
| 940 | + "print(gen_design_table(fun_control=fun_control, spot=spot_tuner))" |
| 941 | + ] |
| 942 | + }, |
| 943 | + { |
| 944 | + "cell_type": "code", |
| 945 | + "execution_count": null, |
| 946 | + "metadata": {}, |
| 947 | + "outputs": [], |
| 948 | + "source": [ |
| 949 | + "spot_tuner.plot_importance(threshold=0.025, filename=None)" |
| 950 | + ] |
| 951 | + }, |
| 952 | + { |
| 953 | + "cell_type": "code", |
| 954 | + "execution_count": null, |
| 955 | + "metadata": {}, |
| 956 | + "outputs": [], |
| 957 | + "source": [ |
| 958 | + "config = get_tuned_architecture(spot_tuner, fun_control)" |
| 959 | + ] |
| 960 | + }, |
| 961 | + { |
| 962 | + "cell_type": "code", |
| 963 | + "execution_count": null, |
| 964 | + "metadata": {}, |
| 965 | + "outputs": [], |
| 966 | + "source": [ |
| 967 | + "test_model(config, fun_control)" |
| 968 | + ] |
| 969 | + }, |
| 970 | + { |
| 971 | + "cell_type": "code", |
| 972 | + "execution_count": null, |
| 973 | + "metadata": {}, |
| 974 | + "outputs": [], |
| 975 | + "source": [ |
| 976 | + "model_loaded = load_light_from_checkpoint(config, fun_control)" |
| 977 | + ] |
| 978 | + }, |
| 979 | + { |
| 980 | + "cell_type": "code", |
| 981 | + "execution_count": null, |
| 982 | + "metadata": {}, |
| 983 | + "outputs": [], |
| 984 | + "source": [ |
| 985 | + "model_loaded" |
| 986 | + ] |
| 987 | + }, |
| 988 | + { |
| 989 | + "cell_type": "code", |
| 990 | + "execution_count": null, |
| 991 | + "metadata": {}, |
| 992 | + "outputs": [], |
| 993 | + "source": [ |
| 994 | + "spot_tuner.plot_important_hyperparameter_contour(filename=None)" |
| 995 | + ] |
| 996 | + }, |
| 997 | + { |
| 998 | + "cell_type": "code", |
| 999 | + "execution_count": null, |
| 1000 | + "metadata": {}, |
| 1001 | + "outputs": [], |
| 1002 | + "source": [ |
| 1003 | + "spot_tuner.parallel_plot()" |
708 | 1004 | ] |
709 | 1005 | }, |
710 | 1006 | { |
|
0 commit comments