{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":53666,"databundleVersionId":6589269,"sourceType":"competition"},{"sourceId":151625393,"sourceType":"kernelVersion"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### editing in progress","metadata":{}},{"cell_type":"markdown","source":"# 対象ID","metadata":{}},{"cell_type":"code","source":"# 学習用\n# series_id: c7b2155a4a47\nTRAIN_ID = 206\n\n# テスト用\n# series_id: b750c8c1556c\nTEST_ID = 185","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:25:44.313059Z","iopub.execute_input":"2023-11-26T03:25:44.313315Z","iopub.status.idle":"2023-11-26T03:25:44.323289Z","shell.execute_reply.started":"2023-11-26T03:25:44.313291Z","shell.execute_reply":"2023-11-26T03:25:44.322358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 前処理","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport pandas.tseries.offsets as offsets\n\nimport numpy as np\nimport pyarrow as pa\nimport pyarrow.parquet as pq\nimport matplotlib.pyplot as plt\nimport os\nimport gc\nimport time\nimport IPython.display\nimport collections\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import LSTM, Dense, Conv1D, Conv2D, Conv3D, Dropout\nfrom tensorflow.keras.utils import to_categorical, timeseries_dataset_from_array\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.losses import BinaryCrossentropy\n\nfrom sklearn.preprocessing import PolynomialFeatures\nfrom sklearn.linear_model import LinearRegression\n\nfrom datetime import timedelta\nfrom scipy.stats import norm\nfrom pathlib import Path\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-26T03:25:44.325763Z","iopub.execute_input":"2023-11-26T03:25:44.326088Z","iopub.status.idle":"2023-11-26T03:25:57.948732Z","shell.execute_reply.started":"2023-11-26T03:25:44.32605Z","shell.execute_reply":"2023-11-26T03:25:57.947915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def graphs(df, id_map, start=0, end=0):\n    df = df.copy()\n    if not end:\n        df = df[df['id_map']==id_map].reset_index(drop=True)\n    else:\n        df = df[df['id_map']==id_map].reset_index(drop=True).iloc[start:end]\n    steps = df[df['time'].apply(lambda x: str(x)[-8:]=='00:00:00')]['step']\n    flag = len(df) != df['step'].iat[-1]\n\n    _, axes = plt.subplots(2, 1, figsize=(20,6))\n    axes[0].set_title(id_map)\n    for ax, col in zip(axes.ravel(), ['anglez', 'enmo']):\n        if 'state' in df.columns:\n            if col == 'anglez':\n                df_state = df['state'].apply(lambda x: 50 if x==1 else -50 if x==0 else 0)\n            else:\n                df_state = df['state'].apply(lambda x: 2 if x==1 else 0 if x==0 else 1)\n        X = df.index if flag else df['step']\n        ax.plot(X, df[col], linewidth=0.5, marker='', color='0.5', label=col)\n        if 'state' in df.columns:\n            ax.plot(X, df_state, linewidth=1, marker='', color='b', label='state')\n        for step in steps:\n            X = df[df['step']==step].index[0] if flag else step\n            ax.axvline(x=X, linewidth=1, color='r', alpha=0.5)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:25:57.950454Z","iopub.execute_input":"2023-11-26T03:25:57.951039Z","iopub.status.idle":"2023-11-26T03:25:57.963769Z","shell.execute_reply.started":"2023-11-26T03:25:57.95101Z","shell.execute_reply":"2023-11-26T03:25:57.962821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def time_to_sec(x):\n    return x.timestamp() % SEC_OF_DAY\n\ndef time_to_step(x):\n    return (x.timestamp() % SEC_OF_DAY) / 5","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:25:57.964865Z","iopub.execute_input":"2023-11-26T03:25:57.965137Z","iopub.status.idle":"2023-11-26T03:25:57.998373Z","shell.execute_reply.started":"2023-11-26T03:25:57.965113Z","shell.execute_reply":"2023-11-26T03:25:57.997478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HOME = Path('/kaggle')\nORIGIN = HOME / 'input/child-mind-institute-detect-sleep-states'\nCUSTOM = HOME / 'input/zzz-eda-dataset'\nWORKING = HOME / 'working'\n\nEVENT_MAP = {'onset':0, 'wakeup':1}\n\nEXP = ['anglez', 'enmo', 'a_var', 'e_var', 'pd']\nOBJ = ['state']\n\nSTEP_OF_WARMUP = 12 * 60 * 2\nSTEP_OF_RATE = 3\nSTEP_OF_STRIDE = 12\n\nSEC_OF_DAY  = 60 * 60 * 24\nSEC_OF_HOUR = 60 * 60\n\nSTEP_OF_DAY   = 12 * 60 * 24\nSTEP_OF_HOUR  = 12 * 60\nSTEP_OF_30MIN = 12 * 30","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:25:58.000953Z","iopub.execute_input":"2023-11-26T03:25:58.001651Z","iopub.status.idle":"2023-11-26T03:25:58.009312Z","shell.execute_reply.started":"2023-11-26T03:25:58.001588Z","shell.execute_reply":"2023-11-26T03:25:58.00855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\ndf = pq.read_table(CUSTOM / 'custom_train_series.parquet').to_pandas()\ndf_test = pq.read_table(ORIGIN / 'test_series.parquet').to_pandas()\ndf_event = pd.read_csv(CUSTOM / 'custom_train_events.csv', index_col=0)\ndf_sample = pd.read_csv(ORIGIN / 'sample_submission.csv', index_col=0)\ndf_id = pd.read_csv(CUSTOM / 'custom_train_stat_groupby_id.csv', index_col=0)\ndf_mean = pd.read_csv(CUSTOM / 'custom_train_series_mean.csv', index_col=0)\ndf_metric = pd.read_csv(CUSTOM / 'metric.csv', index_col=0)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:25:58.010407Z","iopub.execute_input":"2023-11-26T03:25:58.011166Z","iopub.status.idle":"2023-11-26T03:26:23.781839Z","shell.execute_reply.started":"2023-11-26T03:25:58.01113Z","shell.execute_reply":"2023-11-26T03:26:23.780876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 本番テストデータのIDリストが事前のIDリストに含まれていない場合\nfor i in df_test['series_id'].unique():\n    if not i in df_id['series_id'].values:\n        id_map = len(df_id)\n        df_id.loc[id_map] = df_map.iloc[17] # id_map=17 はeventデータが欠損値\n        df_id.loc[df_id.index==id_map, 'series_id'] = i\n        df_id.loc[df_id.index==id_map, 'id_map'] = id_map","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:26:23.783216Z","iopub.execute_input":"2023-11-26T03:26:23.783566Z","iopub.status.idle":"2023-11-26T03:26:23.791612Z","shell.execute_reply.started":"2023-11-26T03:26:23.783533Z","shell.execute_reply":"2023-11-26T03:26:23.790676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def data_pre(df):\n    df['time'] = df['timestamp'].apply(lambda x: x[:-5]).astype('datetime64[ns]')\n    df = df.merge(df_id[['series_id', 'id_map']], on='series_id', how=\"left\").drop(['series_id', 'timestamp'], axis=1)\n    df.info()\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:26:23.792666Z","iopub.execute_input":"2023-11-26T03:26:23.792946Z","iopub.status.idle":"2023-11-26T03:26:23.803656Z","shell.execute_reply.started":"2023-11-26T03:26:23.792923Z","shell.execute_reply":"2023-11-26T03:26:23.802902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = data_pre(df_test)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:26:23.805851Z","iopub.execute_input":"2023-11-26T03:26:23.806654Z","iopub.status.idle":"2023-11-26T03:26:23.837003Z","shell.execute_reply.started":"2023-11-26T03:26:23.806622Z","shell.execute_reply":"2023-11-26T03:26:23.83618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# テストデータのIDが事前のIDリストに含まれていない場合\n\n# 24時間周期で anglez と enmo が全く同じ値を取る区間を特定する。\n# 正しく記録できていないケースが多い。\n\ndef null_range(id_map, df=df_test):\n    r = []\n    df = df[df['id_map']==id_map].reset_index(drop=True).copy()\n    if len(df) < STEP_OF_DAY:\n        return r\n    df['a_s'] = df['anglez'].shift(STEP_OF_DAY)\n    df['e_s'] = df['enmo'].shift(STEP_OF_DAY)\n    df['flag'] = (df['anglez']==df['a_s']) & (df['enmo']==df['e_s'])\n    running = df['flag'].groupby(df['flag'].diff().ne(0).cumsum()).cumsum()\n    i = len(df)-1\n    if running[i] >= STEP_OF_DAY:\n        s = running[-running[i]:].index[0]\n        e = i + 1\n        r.append(range(s,e))\n        i = s - 1\n    while i >= 0:\n        if running[i] < STEP_OF_DAY:\n            i -= 1\n        else:\n            e = i + 1\n            s = e - STEP_OF_DAY + 1\n            r.append(range(s,e))\n            i = s - 1\n    return r\n\n# ID別の24時間周期の anglez と enmo の平均値を求めて、df_mean に集約\nwith tqdm(df_id['id_map'][277:]) as t: # 追加分のみ処理\n    for i in t:\n#         r_list = null_range(i)\n#         df_tmp = df_test[df_test['id_map']==i]\n#         for r in r_list:\n#             df_tmp = df_tmp[~df_tmp['step'].isin(r)]\n#         df_tmp['time'] = df_tmp['time'].apply(time_to_step).astype('int16')\n#         IPython.display.clear_output()\n#         df_tmp = df_tmp[['time','id_map','anglez','enmo']].groupby('time').mean()\n#         if len(df_tmp) != STEP_OF_DAY:\n#             raise ValueError(\"24時間分のデータがない\")\n        df_tmp = df_mean[df_mean['id_map']==17]\n        df_tmp.loc[:, 'id_map'] = i\n        df_mean = pd.concat([df_mean, df_tmp])\n    df_mean['id_map'] = df_mean['id_map'].astype('int16')\n    df_mean = df_mean.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:26:23.838016Z","iopub.execute_input":"2023-11-26T03:26:23.838272Z","iopub.status.idle":"2023-11-26T03:26:24.067247Z","shell.execute_reply.started":"2023-11-26T03:26:23.838249Z","shell.execute_reply":"2023-11-26T03:26:24.066238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 学習データの加工","metadata":{}},{"cell_type":"code","source":"def add_pd(df, id_map, df_mean=df_mean):\n    df_tmp = df_mean[df_mean['id_map']==id_map][['time', 'w_pd', 'o_pd']].rename(columns={'time':'t'})\n    df['t'] = df['time'].apply(time_to_step).astype('int64')\n    df = df.merge(df_tmp, on='t').sort_values('step').drop('t', axis=1)\n    df['pd'] = df['w_pd'] - df['o_pd']\n    df = df.drop(['o_pd', 'w_pd'], axis=1)\n    return df\n\ndef add_warmup(df, id_map, warmup, col, df_mean=df_mean):\n    ts = df['time'].iat[0]\n    ts = ts.timestamp() % SEC_OF_DAY / 5\n    \n    df_tmp = df_mean[df_mean['id_map']==id_map]\n    df_tmp = pd.concat([df_tmp[df_tmp['time']>=ts], df_tmp[df_tmp['time']<ts]])    \n    df_tmp = df_tmp.iloc[-warmup:,:].reset_index(drop=True)\n    df_tmp['step'] = df_tmp.index - warmup\n    df_tmp['pd'] = df_tmp['w_pd'] - df_tmp['o_pd']\n    df_tmp = df_tmp.drop(['o_pd', 'w_pd'], axis=1)\n\n    ts = df['time'].iat[0]\n    ts = [ts + timedelta(seconds=-5*(i+1)) for i in reversed(range(warmup))]\n    df_tmp['time'] = ts\n    df = pd.concat([df_tmp[col], df[col]])\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:26:24.071452Z","iopub.execute_input":"2023-11-26T03:26:24.071786Z","iopub.status.idle":"2023-11-26T03:26:24.083604Z","shell.execute_reply.started":"2023-11-26T03:26:24.071757Z","shell.execute_reply":"2023-11-26T03:26:24.082731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_test_extract(id_map, warmup, n_val=2, n_test=2, df=df, df_id=df_id):\n    df = df[(df['id_map']==id_map) & (df['state']!=-1)].copy() if 'state' in df.columns else df.copy()\n    df = add_pd(df, id_map)\n    col = ['id_map', 'step', 'time'] + EXP + (OBJ if 'state' in df.columns else [])\n    df = df[col]\n    IPython.display.clear_output()\n    \n    night   = df_id[df_id['id_map']==id_map]['night'].iat[0]\n    n_train = night - n_test - n_val\n    \n    steps = df_event[(df_event['id_map']==id_map) & (df_event['step']!=-1)]['step']\n\n    if n_test:\n        end    = steps.iat[(n_train+n_val)*2]\n        start  = steps.iat[(n_train+n_val)*2+1]\n        s_test = (end + start) // 2\n    else:\n        s_test = df['step'].iat[-1] + 1\n        \n    if n_val:\n        end   = steps.iat[n_train*2]\n        start = steps.iat[n_train*2+1]\n        s_val = (end + start) // 2\n    else:\n        s_val = s_test\n        \n    df_tmp = df[df['step']<s_val].reset_index(drop=True)\n    train = add_warmup(df_tmp, id_map, warmup, col)\n    df_tmp = df[(df['step']>=s_val) & (df['step']<s_test)].reset_index(drop=True)\n    val = add_warmup(df_tmp, id_map, warmup, col) if n_val else df.head(0)\n    df_tmp = df[df['step']>=s_test].reset_index(drop=True)\n    test  = add_warmup(df_tmp, id_map, warmup, col) if n_test else df.head(0)\n    \n    print(f'night: {night}')\n    print(f'train: {n_train}')\n    print(f'val  : {n_val}')\n    print(f'test : {n_test}')\n    \n    return train, val, test","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:26:24.084758Z","iopub.execute_input":"2023-11-26T03:26:24.085048Z","iopub.status.idle":"2023-11-26T03:26:24.101151Z","shell.execute_reply.started":"2023-11-26T03:26:24.085023Z","shell.execute_reply":"2023-11-26T03:26:24.100125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w1 = 12 * 5       # 移動分散の幅\nw2 = 12 * 60 * 12 # 移動分散の移動平均の幅\n\nwarmup = STEP_OF_WARMUP * STEP_OF_RATE + w1 + w2\nprint(f'warmup: {warmup}\\nw1: {w1}\\nw2: {w2}')","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:48:55.869758Z","iopub.execute_input":"2023-11-26T03:48:55.870659Z","iopub.status.idle":"2023-11-26T03:48:55.875995Z","shell.execute_reply.started":"2023-11-26T03:48:55.870623Z","shell.execute_reply":"2023-11-26T03:48:55.874907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_map = TRAIN_ID\ntrain, val, test = train_test_extract(id_map, warmup, 5, 5)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:36:05.216065Z","iopub.execute_input":"2023-11-26T03:36:05.21678Z","iopub.status.idle":"2023-11-26T03:36:07.919532Z","shell.execute_reply.started":"2023-11-26T03:36:05.216748Z","shell.execute_reply":"2023-11-26T03:36:07.918615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('original\\n')\ngraphs(df, id_map)\n\nprint('\\ncustom\\n')\ngraphs(df[df['state']!=-1], id_map)\n\nprint('\\ntrain\\n')\ngraphs(train, id_map)\n\nprint('\\nval\\n')\ngraphs(val, id_map)\n\nprint('\\ntest\\n')\ngraphs(test, id_map)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:36:07.920945Z","iopub.execute_input":"2023-11-26T03:36:07.921204Z","iopub.status.idle":"2023-11-26T03:36:27.720954Z","shell.execute_reply.started":"2023-11-26T03:36:07.921181Z","shell.execute_reply":"2023-11-26T03:36:27.720049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_map = TEST_ID\ntest, _, _ = train_test_extract(id_map, warmup, 0, 0)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:26:45.841083Z","iopub.execute_input":"2023-11-26T03:26:45.841438Z","iopub.status.idle":"2023-11-26T03:26:47.276219Z","shell.execute_reply.started":"2023-11-26T03:26:45.841398Z","shell.execute_reply":"2023-11-26T03:26:47.27525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('original\\n')\ngraphs(df, id_map)\n\nprint('\\ncustom\\n')\ngraphs(df[df['state']!=-1], id_map)\n\nprint('\\ntest\\n')\ngraphs(test, id_map)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:26:47.27758Z","iopub.execute_input":"2023-11-26T03:26:47.277987Z","iopub.status.idle":"2023-11-26T03:27:03.998662Z","shell.execute_reply.started":"2023-11-26T03:26:47.277937Z","shell.execute_reply":"2023-11-26T03:27:03.997697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def std_arr(arr):\n    return (arr - arr.mean()) / arr.std()\n\ndef std_df(df, cols=EXP):\n    df = df.copy()\n    for col in cols:\n        mean = df[col].mean()\n        std = df[col].std()\n        df[col] = (df[col] - mean) / std\n    return df\n\ndef make_dataset(df, stride=STEP_OF_STRIDE, rate=STEP_OF_RATE, warmup=STEP_OF_WARMUP):\n    ds = timeseries_dataset_from_array(\n        data=df[EXP],\n        targets=df[OBJ][warmup:] if OBJ[0] in df.columns else None,\n        sequence_length=warmup,\n        sampling_rate=rate,\n        sequence_stride=stride\n    )\n    return ds\n\ndef class_weight(df):\n    weight = len(df[df['state']==1]) / len(df[df['state']==0])\n    return {0: weight, 1: 1}\n\ndef prediction(model, df, stride=STEP_OF_STRIDE):\n    ds = make_dataset(std_df(df), stride)\n    pred = model.predict(ds).flatten()\n    pred = pd.Series(pred, index=range(0, len(pred)*stride, stride))\n    plt.figure(figsize=(20,6))\n    plt.plot(pred)\n    plt.plot(df.iloc[STEP_OF_WARMUP:,:]['state'].reset_index(drop=True))\n    plt.show()\n    return pred","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:30:13.413316Z","iopub.execute_input":"2023-11-26T03:30:13.413651Z","iopub.status.idle":"2023-11-26T03:30:13.424998Z","shell.execute_reply.started":"2023-11-26T03:30:13.413624Z","shell.execute_reply":"2023-11-26T03:30:13.42394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LSTMモデル（単層）","metadata":{}},{"cell_type":"code","source":"id_map = TRAIN_ID\ntrain, val, test = train_test_extract(id_map, warmup, 5, 5)\n\nds_train = make_dataset(std_df(train))\nds_val   = make_dataset(std_df(val))","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:36:27.722681Z","iopub.execute_input":"2023-11-26T03:36:27.723019Z","iopub.status.idle":"2023-11-26T03:36:30.476253Z","shell.execute_reply.started":"2023-11-26T03:36:27.722991Z","shell.execute_reply":"2023-11-26T03:36:30.475157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model_path = f'/kaggle/working/id_{id_map:03d}_single32.tf'\n\nmodel = Sequential([\n    LSTM(64, return_sequences=False),\n    Dropout(0.7),\n    Dense(units=1, activation='sigmoid')\n])\n\ncheckpoint_callback = ModelCheckpoint(\n    filepath=best_model_path,\n    monitor='val_loss',\n    mode='min',\n    save_best_only=True,\n    save_weights_only=False,\n    verbose=1\n)\n\nearly_stopping = EarlyStopping(monitor='val_loss', patience=4, mode='min')\n\nmodel.compile(optimizer=Adam(learning_rate=0.0005),\n            loss=BinaryCrossentropy(from_logits=False),\n            metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:36:30.477887Z","iopub.execute_input":"2023-11-26T03:36:30.478272Z","iopub.status.idle":"2023-11-26T03:36:30.501622Z","shell.execute_reply.started":"2023-11-26T03:36:30.478236Z","shell.execute_reply":"2023-11-26T03:36:30.500771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nhistory = model.fit(ds_train, epochs=20,\n                  validation_data=ds_val,\n                  callbacks=[early_stopping, checkpoint_callback],\n                  class_weight=class_weight(df[df['id_map']==id_map]))\ngc.collect()\nprint()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:36:30.503727Z","iopub.execute_input":"2023-11-26T03:36:30.504026Z","iopub.status.idle":"2023-11-26T03:39:52.21925Z","shell.execute_reply.started":"2023-11-26T03:36:30.503999Z","shell.execute_reply":"2023-11-26T03:39:52.218323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model = tf.keras.models.load_model(best_model_path)\npred = prediction(best_model, test)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:39:52.220449Z","iopub.execute_input":"2023-11-26T03:39:52.220829Z","iopub.status.idle":"2023-11-26T03:39:55.77793Z","shell.execute_reply.started":"2023-11-26T03:39:52.2208Z","shell.execute_reply":"2023-11-26T03:39:55.776984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test, _, _ = train_test_extract(TEST_ID, warmup, 0, 0)\npred = prediction(best_model, test)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:43:31.513735Z","iopub.execute_input":"2023-11-26T03:43:31.514579Z","iopub.status.idle":"2023-11-26T03:43:37.958458Z","shell.execute_reply.started":"2023-11-26T03:43:31.514543Z","shell.execute_reply":"2023-11-26T03:43:37.957519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LSTMモデル（ID=0で同条件）","metadata":{}},{"cell_type":"code","source":"id_map = 0\ntrain, val, test = train_test_extract(id_map, warmup, 5, 5)\n\nds_train = make_dataset(std_df(train))\nds_val   = make_dataset(std_df(val))","metadata":{"execution":{"iopub.status.busy":"2023-11-26T02:32:51.737096Z","iopub.execute_input":"2023-11-26T02:32:51.737548Z","iopub.status.idle":"2023-11-26T02:32:54.289047Z","shell.execute_reply.started":"2023-11-26T02:32:51.737516Z","shell.execute_reply":"2023-11-26T02:32:54.287995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model_path = f'/kaggle/working/id_{id_map:03d}_single32.tf'\n\nmodel = Sequential([\n    LSTM(64, return_sequences=False),\n    Dropout(0.7),\n    Dense(units=1, activation='sigmoid')\n])\n\ncheckpoint_callback = ModelCheckpoint(\n    filepath=best_model_path,\n    monitor='val_loss',\n    mode='min',\n    save_best_only=True,\n    save_weights_only=False,\n    verbose=1\n)\n\nearly_stopping = EarlyStopping(monitor='val_loss', patience=4, mode='min')\n\nmodel.compile(optimizer=Adam(learning_rate=0.0005),\n            loss=BinaryCrossentropy(from_logits=False),\n            metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-11-26T02:32:54.291275Z","iopub.execute_input":"2023-11-26T02:32:54.29223Z","iopub.status.idle":"2023-11-26T02:32:54.316722Z","shell.execute_reply.started":"2023-11-26T02:32:54.292185Z","shell.execute_reply":"2023-11-26T02:32:54.315931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nhistory = model.fit(ds_train, epochs=20,\n                  validation_data=ds_val,\n                  callbacks=[early_stopping, checkpoint_callback],\n                  class_weight=class_weight(df[df['id_map']==id_map]))\ngc.collect()\nprint()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T02:32:54.317794Z","iopub.execute_input":"2023-11-26T02:32:54.318112Z","iopub.status.idle":"2023-11-26T02:34:52.571241Z","shell.execute_reply.started":"2023-11-26T02:32:54.318087Z","shell.execute_reply":"2023-11-26T02:34:52.57021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model = tf.keras.models.load_model(best_model_path)\nprediction(best_model, test)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T02:34:52.573827Z","iopub.execute_input":"2023-11-26T02:34:52.574251Z","iopub.status.idle":"2023-11-26T02:34:56.00298Z","shell.execute_reply.started":"2023-11-26T02:34:52.574214Z","shell.execute_reply":"2023-11-26T02:34:56.001995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test, _, _ = train_test_extract(TEST_ID, warmup, 0, 0)\nprediction(best_model, test)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T02:35:25.162705Z","iopub.execute_input":"2023-11-26T02:35:25.163108Z","iopub.status.idle":"2023-11-26T02:35:31.722171Z","shell.execute_reply.started":"2023-11-26T02:35:25.163078Z","shell.execute_reply":"2023-11-26T02:35:31.721225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LSTMモデル（２層）","metadata":{}},{"cell_type":"code","source":"id_map = TRAIN_ID\ntrain, val, test = train_test_extract(id_map, warmup, 5, 5)\n\nds_train = make_dataset(std_df(train))\nds_val   = make_dataset(std_df(val))","metadata":{"execution":{"iopub.status.busy":"2023-11-13T06:11:44.475904Z","iopub.execute_input":"2023-11-13T06:11:44.476279Z","iopub.status.idle":"2023-11-13T06:11:47.807868Z","shell.execute_reply.started":"2023-11-13T06:11:44.476252Z","shell.execute_reply":"2023-11-13T06:11:47.806882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model_path = f'/kaggle/working/id_{id_map:03d}_double0.tf'\n\nmodel = Sequential([\n    LSTM(32, return_sequences=True),\n    Dropout(0.7),\n    LSTM(32, return_sequences=False),\n    Dropout(0.7),\n    Dense(units=1, activation='sigmoid')\n])\n\ncheckpoint_callback = ModelCheckpoint(\n    filepath=best_model_path,\n    monitor='val_loss',\n    mode='min',\n    save_best_only=True,\n    save_weights_only=False,\n    verbose=1\n)\n\nearly_stopping = EarlyStopping(monitor='val_loss', patience=4, mode='min')\n\nmodel.compile(optimizer=Adam(learning_rate=0.0005),\n            loss=tf.keras.losses.BinaryCrossentropy(from_logits=False),\n            metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-11-13T06:11:48.095246Z","iopub.execute_input":"2023-11-13T06:11:48.095587Z","iopub.status.idle":"2023-11-13T06:11:48.122069Z","shell.execute_reply.started":"2023-11-13T06:11:48.095559Z","shell.execute_reply":"2023-11-13T06:11:48.121331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nhistory = model.fit(ds_train, epochs=20,\n                  validation_data=ds_val,\n                  callbacks=[early_stopping, checkpoint_callback],\n                  class_weight=class_weight(df[df['id_map']==id_map]))\ngc.collect()\nprint()","metadata":{"execution":{"iopub.status.busy":"2023-11-13T06:11:48.123996Z","iopub.execute_input":"2023-11-13T06:11:48.124279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model = tf.keras.models.load_model(best_model_path)\nprediction(best_model, test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test, _, _ = train_test_extract(TEST_ID, warmup, 0, 0)\nprediction(best_model, test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ウォームアップを３時間に短縮","metadata":{}},{"cell_type":"code","source":"STEP_OF_WARMUP = 12 * 60 * 3","metadata":{"execution":{"iopub.status.busy":"2023-11-23T22:58:07.365882Z","iopub.execute_input":"2023-11-23T22:58:07.366761Z","iopub.status.idle":"2023-11-23T22:58:07.371198Z","shell.execute_reply.started":"2023-11-23T22:58:07.366728Z","shell.execute_reply":"2023-11-23T22:58:07.370295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_map = TRAIN_ID\ntrain, val, test = train_test_extract(id_map, warmup, 5, 5)\n\nds_train = make_dataset(std_df(train))\nds_val   = make_dataset(std_df(val))","metadata":{"execution":{"iopub.status.busy":"2023-11-23T22:58:07.746152Z","iopub.execute_input":"2023-11-23T22:58:07.746506Z","iopub.status.idle":"2023-11-23T22:58:16.224634Z","shell.execute_reply.started":"2023-11-23T22:58:07.746478Z","shell.execute_reply":"2023-11-23T22:58:16.223773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model_path = f'/kaggle/working/id_{id_map:03d}_double1.tf'\n\nmodel = Sequential([\n    LSTM(32, return_sequences=True),\n    Dropout(0.7),\n    LSTM(32, return_sequences=False),\n    Dropout(0.7),\n    Dense(units=1, activation='sigmoid')\n])\n\ncheckpoint_callback = ModelCheckpoint(\n    filepath=best_model_path,\n    monitor='val_loss',\n    mode='min',\n    save_best_only=True,\n    save_weights_only=False,\n    verbose=1\n)\n\nearly_stopping = EarlyStopping(monitor='val_loss', patience=4, mode='min')\n\nmodel.compile(optimizer=Adam(learning_rate=0.0005),\n            loss=tf.keras.losses.BinaryCrossentropy(from_logits=False),\n            metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-11-23T22:58:16.226804Z","iopub.execute_input":"2023-11-23T22:58:16.227538Z","iopub.status.idle":"2023-11-23T22:58:16.304288Z","shell.execute_reply.started":"2023-11-23T22:58:16.227499Z","shell.execute_reply":"2023-11-23T22:58:16.303276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nhistory = model.fit(ds_train, epochs=20,\n                  validation_data=ds_val,\n                  callbacks=[early_stopping, checkpoint_callback],\n                  class_weight=class_weight(df[df['id_map']==id_map]))\ngc.collect()\nprint()","metadata":{"execution":{"iopub.status.busy":"2023-11-23T22:58:16.305491Z","iopub.execute_input":"2023-11-23T22:58:16.305795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model = tf.keras.models.load_model(best_model_path)\nprediction(best_model, test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test, _, _ = train_test_extract(TEST_ID, warmup, 0, 0)\nprediction(best_model, test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ユニット数を倍に","metadata":{}},{"cell_type":"code","source":"id_map = TRAIN_ID\ntrain, val, test = train_test_extract(id_map, warmup, 5, 5)\n\nds_train = make_dataset(std_df(train))\nds_val   = make_dataset(std_df(val))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model_path = f'/kaggle/working/id_{id_map:03d}_double2.tf'\n\nmodel = Sequential([\n    LSTM(64, return_sequences=True),\n    Dropout(0.7),\n    LSTM(64, return_sequences=False),\n    Dropout(0.7),\n    Dense(units=1, activation='sigmoid')\n])\n\ncheckpoint_callback = ModelCheckpoint(\n    filepath=best_model_path,\n    monitor='val_loss',\n    mode='min',\n    save_best_only=True,\n    save_weights_only=False,\n    verbose=1\n)\n\nearly_stopping = EarlyStopping(monitor='val_loss', patience=4, mode='min')\n\nmodel.compile(optimizer=Adam(learning_rate=0.0005),\n            loss=tf.keras.losses.BinaryCrossentropy(from_logits=False),\n            metrics=[\"accuracy\"])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nhistory = model.fit(ds_train, epochs=20,\n                  validation_data=ds_val,\n                  callbacks=[early_stopping, checkpoint_callback],\n                  class_weight=class_weight(df[df['id_map']==id_map]))\ngc.collect()\nprint()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model = tf.keras.models.load_model(best_model_path)\nprediction(best_model, test)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test, _, _ = train_test_extract(TEST_ID, warmup, 0, 0)\nprediction(best_model, test)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 連結して学習","metadata":{}},{"cell_type":"code","source":"# with tqdm(df_id[df_id['night']>=4]['id_map']) as t:\n#     for id_map in t:\n#         night = df_id['night'].iat[id_map]\n#         n_val = 2\n#         n_test = night - 4\n#         tmp1, tmp2, _ = train_test_extract(id_map, warmup, n_val, n_test)\n#         train1 = pd.concat([train1, tmp1]) if id_map else tmp1\n#         train2 = pd.concat([train2, tmp2]) if id_map else tmp2","metadata":{"execution":{"iopub.status.busy":"2023-11-20T08:58:41.826828Z","iopub.execute_input":"2023-11-20T08:58:41.827222Z","iopub.status.idle":"2023-11-20T08:58:49.162664Z","shell.execute_reply.started":"2023-11-20T08:58:41.827192Z","shell.execute_reply":"2023-11-20T08:58:49.161695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ds_train1 = make_dataset(std_df(train1))\n# ds_train2 = make_dataset(std_df(train2))","metadata":{"execution":{"iopub.status.busy":"2023-11-20T09:16:59.474408Z","iopub.execute_input":"2023-11-20T09:16:59.475306Z","iopub.status.idle":"2023-11-20T09:17:17.785722Z","shell.execute_reply.started":"2023-11-20T09:16:59.475234Z","shell.execute_reply":"2023-11-20T09:17:17.784651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# best_model_path = f'/kaggle/working/comb_0.tf'\n\n# model = Sequential([\n#     LSTM(32, return_sequences=False),\n#     Dense(units=1, activation='sigmoid')\n# ])\n\n# checkpoint_callback = ModelCheckpoint(\n#     filepath=best_model_path,\n#     monitor='val_loss',\n#     mode='min',\n#     save_best_only=True,\n#     save_weights_only=False,\n#     verbose=1\n# )\n\n# early_stopping = EarlyStopping(monitor='val_loss', patience=2, mode='min')\n\n# model.compile(optimizer=tf.keras.optimizers.Adam(),\n#             loss=tf.keras.losses.BinaryCrossentropy(from_logits=False),\n#             metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-11-20T09:17:27.791101Z","iopub.execute_input":"2023-11-20T09:17:27.791507Z","iopub.status.idle":"2023-11-20T09:17:27.875047Z","shell.execute_reply.started":"2023-11-20T09:17:27.791473Z","shell.execute_reply":"2023-11-20T09:17:27.874128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n\n# history = model.fit(ds_train1, epochs=5,\n#                   validation_data=ds_train2,\n#                   callbacks=[early_stopping, checkpoint_callback],\n#                   class_weight=class_weight(train1))\n# gc.collect()\n# print()","metadata":{"execution":{"iopub.status.busy":"2023-11-20T09:17:32.555757Z","iopub.execute_input":"2023-11-20T09:17:32.556415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test, _, _ = train_test_extract(TEST_ID, warmup, 0, 0)\n# prediction(best_model, test)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# best_model_path = f'/kaggle/working/comb_1.tf'\n\n# model = Sequential([\n#     LSTM(32, return_sequences=False),\n#     Dense(units=1, activation='sigmoid')\n# ])\n\n# checkpoint_callback = ModelCheckpoint(\n#     filepath=best_model_path,\n#     monitor='val_loss',\n#     mode='min',\n#     save_best_only=True,\n#     save_weights_only=False,\n#     verbose=1\n# )\n\n# early_stopping = EarlyStopping(monitor='val_loss', patience=2, mode='min')\n\n# model.compile(optimizer=tf.keras.optimizers.Adam(),\n#             loss=tf.keras.losses.BinaryCrossentropy(from_logits=False),\n#             metrics=[\"accuracy\"])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n\n# history = model.fit(ds_train2, epochs=5,\n#                   validation_data=ds_train1,\n#                   callbacks=[early_stopping, checkpoint_callback],\n#                   class_weight=class_weight(train2))\n# gc.collect()\n# print()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test, _, _ = train_test_extract(TEST_ID, warmup, 0, 0)\n# prediction(best_model, test)","metadata":{},"execution_count":null,"outputs":[]}]}