{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# 先知晓一下当前的目录地址\n!pwd","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:01:54.075374Z","iopub.execute_input":"2023-08-21T16:01:54.075749Z","iopub.status.idle":"2023-08-21T16:01:55.025512Z","shell.execute_reply.started":"2023-08-21T16:01:54.075719Z","shell.execute_reply":"2023-08-21T16:01:55.024359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 安装必要的程序包\n!pip install lalsuite","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:01:55.028144Z","iopub.execute_input":"2023-08-21T16:01:55.028534Z","iopub.status.idle":"2023-08-21T16:02:28.899355Z","shell.execute_reply.started":"2023-08-21T16:01:55.028499Z","shell.execute_reply":"2023-08-21T16:02:28.898250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- 下面的代码块是Kaggle notebook默认的","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-21T16:02:28.901402Z","iopub.execute_input":"2023-08-21T16:02:28.901799Z","iopub.status.idle":"2023-08-21T16:02:28.913303Z","shell.execute_reply.started":"2023-08-21T16:02:28.901763Z","shell.execute_reply":"2023-08-21T16:02:28.911730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 加载我给同志们准备好的程序脚本\nimport sys\nsys.path.append('/kaggle/input/can-you-find-the-gw-signals')","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:02:28.916283Z","iopub.execute_input":"2023-08-21T16:02:28.917311Z","iopub.status.idle":"2023-08-21T16:02:28.921996Z","shell.execute_reply.started":"2023-08-21T16:02:28.917264Z","shell.execute_reply":"2023-08-21T16:02:28.920900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train","metadata":{}},{"cell_type":"code","source":"# 把脚本里的所有关联的程序包、类、函数都 load 一下\nfrom main import *\nfrom utils import *","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:02:28.923617Z","iopub.execute_input":"2023-08-21T16:02:28.924047Z","iopub.status.idle":"2023-08-21T16:02:32.712744Z","shell.execute_reply.started":"2023-08-21T16:02:28.923909Z","shell.execute_reply":"2023-08-21T16:02:32.711547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 初始化数据生成 class\nnsample_perepoch = 100\ndataset_train = DatasetGenerator(snr=20, nsample_perepoch=nsample_perepoch)\ndataset_test = DatasetGenerator(snr=20, nsample_perepoch=nsample_perepoch)\n\n# Create a DataLoader\ndata_loader = DataLoader(dataset_train, batch_size=32, shuffle=True,)\ntest_iter = DataLoader(dataset_test, batch_size=32, shuffle=True,)\n\ndevice = torch.device('cuda')\n\n# Where we output our model and loss history\ncheckpoint_dir = './checkpoints_cnn/'\n\n# Creat model    \nnet, epoch, train_loss_history = load_model(checkpoint_dir)\nnet.to(device);\n\n# Optim. params\nlr = 0.003\ntotal_epochs = 100\ntotal_epochs += epoch\noutput_freq = 1\n\ntrain(net, lr, nsample_perepoch, epoch, total_epochs,\n      dataset_train, data_loader, test_iter,\n      train_loss_history, checkpoint_dir, device, notebook=True)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:02:32.714126Z","iopub.execute_input":"2023-08-21T16:02:32.714797Z","iopub.status.idle":"2023-08-21T16:09:44.421313Z","shell.execute_reply.started":"2023-08-21T16:02:32.714759Z","shell.execute_reply":"2023-08-21T16:09:44.419808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate","metadata":{}},{"cell_type":"code","source":"# 加载必要的库\nfrom main import *\nfrom utils import *\nfrom tqdm import tqdm\nfrom sklearn.metrics import roc_auc_score, roc_curve\n\ndef evaluate_gpu(net, data_iter, device=None): #@save\n    \"\"\"使用GPU计算模型在数据集上的预测结果\"\"\"\n    if isinstance(net, nn.Module):\n        net.eval()  # 设置为评估模式\n        if not device:\n            device = next(iter(net.parameters())).device\n    softmax = nn.Softmax(dim=-1)\n    y_hat_list = []\n    y_list = []\n    with torch.no_grad():\n        for X, y in data_iter:\n            if isinstance(X, list):\n                # BERT微调所需的（之后将介绍）\n                X = [x.to(device) for x in X]\n            else:\n                X = X.to(device).to(torch.float)\n            y = y.to(device).to(torch.long)\n            y_hat = net(X)\n\n            preds = softmax(y_hat).cpu().numpy()[:,1].tolist()\n            labels = y.cpu().numpy().tolist()\n\n            y_hat_list.extend(preds)\n            y_list.extend(labels)\n    return np.asarray(y_hat_list), np.asarray(y_list)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:09:44.422942Z","iopub.execute_input":"2023-08-21T16:09:44.423366Z","iopub.status.idle":"2023-08-21T16:09:44.598537Z","shell.execute_reply.started":"2023-08-21T16:09:44.423329Z","shell.execute_reply":"2023-08-21T16:09:44.597552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nsample_perepoch = 1000 # num of injections\nsnr = 20  # 信噪比\n\ndataset_test = DatasetGenerator(snr=snr, nsample_perepoch=nsample_perepoch)\ndata_iter = DataLoader(dataset_test, batch_size=32, shuffle=True,)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:09:44.600121Z","iopub.execute_input":"2023-08-21T16:09:44.600483Z","iopub.status.idle":"2023-08-21T16:10:16.336608Z","shell.execute_reply.started":"2023-08-21T16:09:44.600449Z","shell.execute_reply":"2023-08-21T16:10:16.335321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load model\ndevice = torch.device('cuda')\n\n# Where we output our model and loss history\ncheckpoint_dir = './checkpoints_cnn/'\n\n# Creat model\nnet, epoch, train_loss_history = load_model(checkpoint_dir)\nnet.to(device);","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:10:16.337950Z","iopub.execute_input":"2023-08-21T16:10:16.338336Z","iopub.status.idle":"2023-08-21T16:10:16.395584Z","shell.execute_reply.started":"2023-08-21T16:10:16.338282Z","shell.execute_reply":"2023-08-21T16:10:16.394464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_hat_list, y_list = evaluate_gpu(net, data_iter, device)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:10:16.400218Z","iopub.execute_input":"2023-08-21T16:10:16.400549Z","iopub.status.idle":"2023-08-21T16:10:16.717725Z","shell.execute_reply.started":"2023-08-21T16:10:16.400521Z","shell.execute_reply":"2023-08-21T16:10:16.716664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fpr, tpr, thresholds = roc_curve(y_list, y_hat_list)\nauc = roc_auc_score(y_list, y_hat_list)\nauc","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:10:16.719008Z","iopub.execute_input":"2023-08-21T16:10:16.719385Z","iopub.status.idle":"2023-08-21T16:10:16.737195Z","shell.execute_reply.started":"2023-08-21T16:10:16.719351Z","shell.execute_reply":"2023-08-21T16:10:16.736286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = np.argmax(thresholds < 0.5)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:10:16.738645Z","iopub.execute_input":"2023-08-21T16:10:16.739020Z","iopub.status.idle":"2023-08-21T16:10:16.744694Z","shell.execute_reply.started":"2023-08-21T16:10:16.738984Z","shell.execute_reply":"2023-08-21T16:10:16.743479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(fpr, tpr, color=\"deeppink\")\nplt.plot(*(np.linspace(0,1,100),)*2, 'k--', label='Luck (AUC=0.50)')\nplt.scatter(fpr[index], tpr[index], label=f'threshold={thresholds[index]:.2f}')\nplt.axis(\"square\")\n# plt.xscale('log')\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"ROC curves\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:10:16.746612Z","iopub.execute_input":"2023-08-21T16:10:16.747027Z","iopub.status.idle":"2023-08-21T16:10:17.137105Z","shell.execute_reply.started":"2023-08-21T16:10:16.746993Z","shell.execute_reply":"2023-08-21T16:10:17.136371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from itertools import cycle\ncolors = cycle([\"deeppink\", \"aqua\", \"darkorange\", \"cornflowerblue\"])\n\nnsample_perepoch = 1000\nsnr_list = [5, 10, 15, 20]\nfor snr in tqdm(snr_list):\n    dataset_test = DatasetGenerator(snr=snr, nsample_perepoch=nsample_perepoch, verbose=False)\n    data_iter = DataLoader(dataset_test, batch_size=32, shuffle=True)\n    y_hat_list, y_list = evaluate_gpu(net, data_iter, device)\n\n    fpr, tpr, thresholds = roc_curve(y_list, y_hat_list)\n    auc = roc_auc_score(y_list, y_hat_list)\n    \n    plt.plot(fpr, tpr, color=next(colors), label=f'SNR={snr} (AUC={auc:.2f})')\nplt.plot(*(np.linspace(0,1,100),)*2, 'k--', label='Luck (AUC=0.50)')\n# plt.axis(\"square\")\nplt.xscale('log')\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"ROC curves\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:10:17.138546Z","iopub.execute_input":"2023-08-21T16:10:17.139670Z","iopub.status.idle":"2023-08-21T16:12:25.878199Z","shell.execute_reply.started":"2023-08-21T16:10:17.139634Z","shell.execute_reply":"2023-08-21T16:12:25.877230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from itertools import cycle\ncolors = cycle([\"deeppink\", \"aqua\", \"darkorange\", \"cornflowerblue\"])\n\ndef sigma(n, tp):\n    \"\"\"\n\n    :param tp:\n    :param n:\n    :return:\n    \"\"\"\n    return np.sqrt(tp*(1.-tp)/n)\n\nnsample_perepoch = 1000\nsnr_list = [2, 4, 6]\nfor snr in tqdm(snr_list):\n    dataset_test = DatasetGenerator(snr=snr, nsample_perepoch=nsample_perepoch, verbose=False)\n    data_iter = DataLoader(dataset_test, batch_size=32, shuffle=True)\n    y_hat_list, y_list = evaluate_gpu(net, data_iter, device)\n\n    fpr, tpr, _ = roc_curve(y_list, y_hat_list)\n    auc = roc_auc_score(y_list, y_hat_list)\n    \n    fpr_error = [sigma(len(y_list)/2., t) for t in fpr]\n    tpr_error = [sigma(len(y_list)/2., t) for t in tpr]\n    \n    color = next(colors)\n    plt.plot(fpr, tpr, color=color, label=f'SNR={snr} (AUC={auc:.2f})')\n    plt.fill_between(fpr, tpr+tpr_error, tpr-tpr_error, alpha=0.2, facecolor=color, zorder=0)\nplt.plot(*(np.linspace(0,1,100),)*2, 'k--', label='Luck (AUC=0.50)')\n# plt.axis(\"square\")\nplt.xscale('log')\nplt.yscale('log')\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"ROC curves\")\nplt.xlim(1e-4,1)\nplt.ylim(1e-4,1)\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:12:25.879256Z","iopub.execute_input":"2023-08-21T16:12:25.879606Z","iopub.status.idle":"2023-08-21T16:14:01.923660Z","shell.execute_reply.started":"2023-08-21T16:12:25.879567Z","shell.execute_reply":"2023-08-21T16:14:01.922734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"import _pickle as pickle\nfrom tqdm import tqdm\nfrom main import *\nfrom utils import *\n\ndef evaluate_submissoin_gpu(net, data_iter, device=None): #@save\n    \"\"\"使用GPU计算模型在数据集上的预测结果\"\"\"\n    if isinstance(net, nn.Module):\n        net.eval()  # 设置为评估模式\n        if not device:\n            device = next(iter(net.parameters())).device\n    softmax = nn.Softmax(dim=-1)\n    y_hat_list = []\n\n    with torch.no_grad():\n        for X in tqdm(data_iter):\n            if isinstance(X, list):\n                # BERT微调所需的（之后将介绍）\n                X = [x.to(device) for x in X]\n            else:\n                X = X.to(device).to(torch.float)\n            y_hat = net(X)\n\n            preds = softmax(y_hat).cpu().numpy()[:,1].tolist()\n            y_hat_list.extend(preds)\n    return y_hat_list","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:14:01.924976Z","iopub.execute_input":"2023-08-21T16:14:01.926565Z","iopub.status.idle":"2023-08-21T16:14:01.936647Z","shell.execute_reply.started":"2023-08-21T16:14:01.926527Z","shell.execute_reply":"2023-08-21T16:14:01.935590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = np.load('/kaggle/input/can-you-find-the-gw-signals/test.npy')","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:14:01.938495Z","iopub.execute_input":"2023-08-21T16:14:01.938890Z","iopub.status.idle":"2023-08-21T16:14:04.174169Z","shell.execute_reply.started":"2023-08-21T16:14:01.938857Z","shell.execute_reply":"2023-08-21T16:14:04.173178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_loader = DataLoader(test_dataset, batch_size=32, shuffle=False,)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:14:04.175579Z","iopub.execute_input":"2023-08-21T16:14:04.175942Z","iopub.status.idle":"2023-08-21T16:14:04.182547Z","shell.execute_reply.started":"2023-08-21T16:14:04.175909Z","shell.execute_reply":"2023-08-21T16:14:04.181343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load model\ndevice = torch.device('cuda')\n\n# Where we output our model and loss history\ncheckpoint_dir = './checkpoints_cnn/'\n\n# Creat model\nnet, epoch, train_loss_history = load_model(checkpoint_dir)\nnet.to(device);","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:14:04.184908Z","iopub.execute_input":"2023-08-21T16:14:04.185555Z","iopub.status.idle":"2023-08-21T16:14:04.241326Z","shell.execute_reply.started":"2023-08-21T16:14:04.185520Z","shell.execute_reply":"2023-08-21T16:14:04.240339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = evaluate_submissoin_gpu(net, data_loader, device)\nlen(results)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:14:04.242967Z","iopub.execute_input":"2023-08-21T16:14:04.243322Z","iopub.status.idle":"2023-08-21T16:14:04.489027Z","shell.execute_reply.started":"2023-08-21T16:14:04.243273Z","shell.execute_reply":"2023-08-21T16:14:04.488083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:14:04.490786Z","iopub.execute_input":"2023-08-21T16:14:04.491483Z","iopub.status.idle":"2023-08-21T16:14:04.507919Z","shell.execute_reply.started":"2023-08-21T16:14:04.491447Z","shell.execute_reply":"2023-08-21T16:14:04.506864Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\npd.DataFrame({'id': range(len(results)),\n              'target': results}, index=None).to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T16:14:04.509605Z","iopub.execute_input":"2023-08-21T16:14:04.510119Z","iopub.status.idle":"2023-08-21T16:14:04.529079Z","shell.execute_reply.started":"2023-08-21T16:14:04.510070Z","shell.execute_reply":"2023-08-21T16:14:04.528002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}