2024-01-05 09:12:27 +08:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "6b84fefd-5936-4da4-ab6b-5b944329ad1d",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ['CUDA_DEVICE_ORDER'] = 'PCB_BUS_ID'\n",
"os.environ['CUDA_VISIBLE_DEVICES'] = '0, 1'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9cf130e3-62ef-46e0-bbdc-b13d9d29318d",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.model_selection import train_test_split\n",
"import matplotlib.pyplot as plt\n",
"#新增加的两行\n",
"from pylab import mpl\n",
"# 设置显示中文字体\n",
"mpl.rcParams[\"font.sans-serif\"] = [\"SimHei\"]\n",
"\n",
"mpl.rcParams[\"axes.unicode_minus\"] = False"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "752381a5-0aeb-4c54-bc48-f9c3f8fc5d17",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead tr th {\n",
" text-align: left;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th>Unnamed: 0_level_0</th>\n",
" <th>氢</th>\n",
" <th>碳</th>\n",
" <th>氮</th>\n",
" <th>氧</th>\n",
" <th>弹筒发热量</th>\n",
" <th>挥发分</th>\n",
" <th>固定炭</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>化验编号</th>\n",
" <th>Had</th>\n",
" <th>Cad</th>\n",
" <th>Nad</th>\n",
" <th>Oad</th>\n",
" <th>Qb,ad</th>\n",
" <th>Vad</th>\n",
" <th>Fcad</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>Unnamed: 0_level_2</th>\n",
" <th>(%)</th>\n",
" <th>(%)</th>\n",
" <th>(%)</th>\n",
" <th>(%)</th>\n",
" <th>MJ/kg</th>\n",
" <th>(%)</th>\n",
" <th>(%)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2720110529</td>\n",
" <td>3.93</td>\n",
" <td>70.18</td>\n",
" <td>0.81</td>\n",
" <td>25.079</td>\n",
" <td>27.820</td>\n",
" <td>32.06</td>\n",
" <td>55.68</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2720096883</td>\n",
" <td>3.78</td>\n",
" <td>68.93</td>\n",
" <td>0.77</td>\n",
" <td>26.512</td>\n",
" <td>27.404</td>\n",
" <td>29.96</td>\n",
" <td>54.71</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2720109084</td>\n",
" <td>3.48</td>\n",
" <td>69.60</td>\n",
" <td>0.76</td>\n",
" <td>26.148</td>\n",
" <td>27.578</td>\n",
" <td>29.31</td>\n",
" <td>55.99</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2720084708</td>\n",
" <td>3.47</td>\n",
" <td>66.71</td>\n",
" <td>0.76</td>\n",
" <td>29.055</td>\n",
" <td>26.338</td>\n",
" <td>28.58</td>\n",
" <td>53.87</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2720062721</td>\n",
" <td>3.87</td>\n",
" <td>68.78</td>\n",
" <td>0.80</td>\n",
" <td>26.542</td>\n",
" <td>27.280</td>\n",
" <td>29.97</td>\n",
" <td>54.78</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>223</th>\n",
" <td>2720030490</td>\n",
" <td>4.12</td>\n",
" <td>68.85</td>\n",
" <td>0.97</td>\n",
" <td>26.055</td>\n",
" <td>27.864</td>\n",
" <td>32.94</td>\n",
" <td>51.89</td>\n",
" </tr>\n",
" <tr>\n",
" <th>224</th>\n",
" <td>2720028633</td>\n",
" <td>3.97</td>\n",
" <td>67.04</td>\n",
" <td>0.94</td>\n",
" <td>28.043</td>\n",
" <td>27.368</td>\n",
" <td>31.88</td>\n",
" <td>51.38</td>\n",
" </tr>\n",
" <tr>\n",
" <th>225</th>\n",
" <td>2720028634</td>\n",
" <td>4.12</td>\n",
" <td>68.42</td>\n",
" <td>0.96</td>\n",
" <td>26.493</td>\n",
" <td>27.886</td>\n",
" <td>33.16</td>\n",
" <td>52.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>226</th>\n",
" <td>2720017683</td>\n",
" <td>3.88</td>\n",
" <td>67.42</td>\n",
" <td>0.94</td>\n",
" <td>27.760</td>\n",
" <td>26.616</td>\n",
" <td>31.65</td>\n",
" <td>50.56</td>\n",
" </tr>\n",
" <tr>\n",
" <th>227</th>\n",
" <td>2720017678</td>\n",
" <td>3.81</td>\n",
" <td>66.74</td>\n",
" <td>0.92</td>\n",
" <td>28.530</td>\n",
" <td>26.688</td>\n",
" <td>31.02</td>\n",
" <td>50.82</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>228 rows × 8 columns</p>\n",
"</div>"
],
"text/plain": [
" Unnamed: 0_level_0 氢 碳 氮 氧 弹筒发热量 挥发分 固定炭\n",
" 化验编号 Had Cad Nad Oad Qb,ad Vad Fcad\n",
" Unnamed: 0_level_2 (%) (%) (%) (%) MJ/kg (%) (%)\n",
"0 2720110529 3.93 70.18 0.81 25.079 27.820 32.06 55.68\n",
"1 2720096883 3.78 68.93 0.77 26.512 27.404 29.96 54.71\n",
"2 2720109084 3.48 69.60 0.76 26.148 27.578 29.31 55.99\n",
"3 2720084708 3.47 66.71 0.76 29.055 26.338 28.58 53.87\n",
"4 2720062721 3.87 68.78 0.80 26.542 27.280 29.97 54.78\n",
".. ... ... ... ... ... ... ... ...\n",
"223 2720030490 4.12 68.85 0.97 26.055 27.864 32.94 51.89\n",
"224 2720028633 3.97 67.04 0.94 28.043 27.368 31.88 51.38\n",
"225 2720028634 4.12 68.42 0.96 26.493 27.886 33.16 52.00\n",
"226 2720017683 3.88 67.42 0.94 27.760 26.616 31.65 50.56\n",
"227 2720017678 3.81 66.74 0.92 28.530 26.688 31.02 50.82\n",
"\n",
"[228 rows x 8 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data_0102 = pd.read_excel('./data/20240102/20240102.xlsx', header=[0,1,2])\n",
"data_0102"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "972f1e9c-3ebc-45cf-8d1f-7611645e5238",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['化验编号',\n",
" '氢Had(%)',\n",
" '碳Cad(%)',\n",
" '氮Nad(%)',\n",
" '氧Oad(%)',\n",
" '弹筒发热量Qb,adMJ/kg',\n",
" '挥发分Vad(%)',\n",
" '固定炭Fcad(%)']"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cols = [''.join([y for y in x if 'Unnamed' not in y]) for x in data_0102.columns]\n",
"cols"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c95f1106-b3a4-43c6-88ec-3cdebf91d79a",
"metadata": {},
"outputs": [],
"source": [
"data_0102.columns = cols"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "2e96af0a-feda-4a1f-a13e-9c8861c6f4d4",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>化验编号</th>\n",
" <th>氢Had(%)</th>\n",
" <th>碳Cad(%)</th>\n",
" <th>氮Nad(%)</th>\n",
" <th>氧Oad(%)</th>\n",
" <th>弹筒发热量Qb,adMJ/kg</th>\n",
" <th>挥发分Vad(%)</th>\n",
" <th>固定炭Fcad(%)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2720110529</td>\n",
" <td>3.93</td>\n",
" <td>70.18</td>\n",
" <td>0.81</td>\n",
" <td>25.079</td>\n",
" <td>27.820</td>\n",
" <td>32.06</td>\n",
" <td>55.68</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2720096883</td>\n",
" <td>3.78</td>\n",
" <td>68.93</td>\n",
" <td>0.77</td>\n",
" <td>26.512</td>\n",
" <td>27.404</td>\n",
" <td>29.96</td>\n",
" <td>54.71</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2720109084</td>\n",
" <td>3.48</td>\n",
" <td>69.60</td>\n",
" <td>0.76</td>\n",
" <td>26.148</td>\n",
" <td>27.578</td>\n",
" <td>29.31</td>\n",
" <td>55.99</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2720084708</td>\n",
" <td>3.47</td>\n",
" <td>66.71</td>\n",
" <td>0.76</td>\n",
" <td>29.055</td>\n",
" <td>26.338</td>\n",
" <td>28.58</td>\n",
" <td>53.87</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2720062721</td>\n",
" <td>3.87</td>\n",
" <td>68.78</td>\n",
" <td>0.80</td>\n",
" <td>26.542</td>\n",
" <td>27.280</td>\n",
" <td>29.97</td>\n",
" <td>54.78</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>223</th>\n",
" <td>2720030490</td>\n",
" <td>4.12</td>\n",
" <td>68.85</td>\n",
" <td>0.97</td>\n",
" <td>26.055</td>\n",
" <td>27.864</td>\n",
" <td>32.94</td>\n",
" <td>51.89</td>\n",
" </tr>\n",
" <tr>\n",
" <th>224</th>\n",
" <td>2720028633</td>\n",
" <td>3.97</td>\n",
" <td>67.04</td>\n",
" <td>0.94</td>\n",
" <td>28.043</td>\n",
" <td>27.368</td>\n",
" <td>31.88</td>\n",
" <td>51.38</td>\n",
" </tr>\n",
" <tr>\n",
" <th>225</th>\n",
" <td>2720028634</td>\n",
" <td>4.12</td>\n",
" <td>68.42</td>\n",
" <td>0.96</td>\n",
" <td>26.493</td>\n",
" <td>27.886</td>\n",
" <td>33.16</td>\n",
" <td>52.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>226</th>\n",
" <td>2720017683</td>\n",
" <td>3.88</td>\n",
" <td>67.42</td>\n",
" <td>0.94</td>\n",
" <td>27.760</td>\n",
" <td>26.616</td>\n",
" <td>31.65</td>\n",
" <td>50.56</td>\n",
" </tr>\n",
" <tr>\n",
" <th>227</th>\n",
" <td>2720017678</td>\n",
" <td>3.81</td>\n",
" <td>66.74</td>\n",
" <td>0.92</td>\n",
" <td>28.530</td>\n",
" <td>26.688</td>\n",
" <td>31.02</td>\n",
" <td>50.82</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>228 rows × 8 columns</p>\n",
"</div>"
],
"text/plain": [
" 化验编号 氢Had(%) 碳Cad(%) 氮Nad(%) 氧Oad(%) 弹筒发热量Qb,adMJ/kg \\\n",
"0 2720110529 3.93 70.18 0.81 25.079 27.820 \n",
"1 2720096883 3.78 68.93 0.77 26.512 27.404 \n",
"2 2720109084 3.48 69.60 0.76 26.148 27.578 \n",
"3 2720084708 3.47 66.71 0.76 29.055 26.338 \n",
"4 2720062721 3.87 68.78 0.80 26.542 27.280 \n",
".. ... ... ... ... ... ... \n",
"223 2720030490 4.12 68.85 0.97 26.055 27.864 \n",
"224 2720028633 3.97 67.04 0.94 28.043 27.368 \n",
"225 2720028634 4.12 68.42 0.96 26.493 27.886 \n",
"226 2720017683 3.88 67.42 0.94 27.760 26.616 \n",
"227 2720017678 3.81 66.74 0.92 28.530 26.688 \n",
"\n",
" 挥发分Vad(%) 固定炭Fcad(%) \n",
"0 32.06 55.68 \n",
"1 29.96 54.71 \n",
"2 29.31 55.99 \n",
"3 28.58 53.87 \n",
"4 29.97 54.78 \n",
".. ... ... \n",
"223 32.94 51.89 \n",
"224 31.88 51.38 \n",
"225 33.16 52.00 \n",
"226 31.65 50.56 \n",
"227 31.02 50.82 \n",
"\n",
"[228 rows x 8 columns]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data_0102"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "04b177a7-2f02-4e23-8ea9-29f34cf3eafc",
"metadata": {},
"outputs": [],
"source": [
"out_cols = ['挥发分Vad(%)', '固定炭Fcad(%)']"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "31169fbf-d78e-42f7-87f3-71ba3dd0979d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['挥发分Vad(%)', '固定炭Fcad(%)']"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"out_cols"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "feaedd50-f999-45bf-b465-3d359b0c0110",
"metadata": {},
"outputs": [],
"source": [
"data = data_0102.copy()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "a40bee0f-011a-4edb-80f8-4e2f40e755fd",
"metadata": {},
"outputs": [],
"source": [
"train_data = data.dropna(subset=out_cols).fillna(0)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "535d37b6-b9de-4025-ac8f-62f5bdbe2451",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2024-01-05 17:15:57 +08:00
"2024-01-05 16:46:07.061819: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0\n"
2024-01-05 09:12:27 +08:00
]
}
],
"source": [
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"from tensorflow.keras import layers\n",
"import tensorflow.keras.backend as K"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "1c85d462-f248-4ffb-908f-eb4b20eab179",
"metadata": {},
"outputs": [],
"source": [
"class TransformerBlock(layers.Layer):\n",
" def __init__(self, embed_dim, num_heads, ff_dim, name, rate=0.1):\n",
" super().__init__()\n",
" self.att = layers.MultiHeadAttention(num_heads=num_heads, key_dim=embed_dim, name=name)\n",
" self.ffn = keras.Sequential(\n",
" [layers.Dense(ff_dim, activation=\"relu\"), layers.Dense(embed_dim),]\n",
" )\n",
" self.layernorm1 = layers.LayerNormalization(epsilon=1e-6)\n",
" self.layernorm2 = layers.LayerNormalization(epsilon=1e-6)\n",
" self.dropout1 = layers.Dropout(rate)\n",
" self.dropout2 = layers.Dropout(rate)\n",
"\n",
" def call(self, inputs, training):\n",
" attn_output = self.att(inputs, inputs)\n",
" attn_output = self.dropout1(attn_output, training=training)\n",
" out1 = self.layernorm1(inputs + attn_output)\n",
" ffn_output = self.ffn(out1)\n",
" ffn_output = self.dropout2(ffn_output, training=training)\n",
" return self.layernorm2(out1 + ffn_output)"
]
},
{
"cell_type": "code",
2024-01-05 17:15:57 +08:00
"execution_count": 13,
2024-01-05 09:12:27 +08:00
"id": "790284a3-b9d3-4144-b481-38a7c3ecb4b9",
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras import Model"
]
},
{
"cell_type": "code",
2024-01-05 17:15:57 +08:00
"execution_count": 14,
2024-01-05 09:12:27 +08:00
"id": "cd9a1ca1-d0ca-4cb5-9ef5-fd5d63576cd2",
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.initializers import Constant"
]
},
{
"cell_type": "code",
2024-01-05 17:15:57 +08:00
"execution_count": 15,
2024-01-05 09:12:27 +08:00
"id": "9bc02f29-0fb7-420d-99a8-435eadc06e29",
"metadata": {},
"outputs": [],
"source": [
"# Custom loss layer\n",
"class CustomMultiLossLayer(layers.Layer):\n",
" def __init__(self, nb_outputs=2, **kwargs):\n",
" self.nb_outputs = nb_outputs\n",
" self.is_placeholder = True\n",
" super(CustomMultiLossLayer, self).__init__(**kwargs)\n",
" \n",
" def build(self, input_shape=None):\n",
" # initialise log_vars\n",
" self.log_vars = []\n",
" for i in range(self.nb_outputs):\n",
" self.log_vars += [self.add_weight(name='log_var' + str(i), shape=(1,),\n",
" initializer=tf.initializers.he_normal(), trainable=True)]\n",
" super(CustomMultiLossLayer, self).build(input_shape)\n",
"\n",
" def multi_loss(self, ys_true, ys_pred):\n",
" assert len(ys_true) == self.nb_outputs and len(ys_pred) == self.nb_outputs\n",
" loss = 0\n",
" for y_true, y_pred, log_var in zip(ys_true, ys_pred, self.log_vars):\n",
" mse = (y_true - y_pred) ** 2.\n",
" pre = K.exp(-log_var[0])\n",
" loss += tf.abs(tf.reduce_logsumexp(pre * mse + log_var[0], axis=-1))\n",
" return K.mean(loss)\n",
"\n",
" def call(self, inputs):\n",
" ys_true = inputs[:self.nb_outputs]\n",
" ys_pred = inputs[self.nb_outputs:]\n",
" loss = self.multi_loss(ys_true, ys_pred)\n",
" self.add_loss(loss, inputs=inputs)\n",
" # We won't actually use the output.\n",
" return K.concatenate(inputs, -1)"
]
},
{
"cell_type": "code",
2024-01-05 17:15:57 +08:00
"execution_count": 16,
2024-01-05 09:12:27 +08:00
"id": "a190207e-5a59-4813-9660-758760cf1b73",
"metadata": {},
"outputs": [],
"source": [
"num_heads, ff_dim = 3, 16"
]
},
{
"cell_type": "code",
2024-01-05 17:15:57 +08:00
"execution_count": 17,
2024-01-05 09:12:27 +08:00
"id": "80f32155-e71f-4615-8d0c-01dfd04988fe",
"metadata": {},
"outputs": [],
"source": [
"def get_prediction_model():\n",
" inputs = layers.Input(shape=(1,len(feature_cols)), name='input')\n",
" x = layers.Conv1D(filters=64, kernel_size=1, activation='relu')(inputs)\n",
" # x = layers.Dropout(rate=0.1)(x)\n",
" lstm_out = layers.Bidirectional(layers.LSTM(units=64, return_sequences=True))(x)\n",
" lstm_out = layers.Dense(128, activation='relu')(lstm_out)\n",
" transformer_block = TransformerBlock(128, num_heads, ff_dim, name='first_attn')\n",
" out = transformer_block(lstm_out)\n",
" out = layers.GlobalAveragePooling1D()(out)\n",
" out = layers.Dropout(0.1)(out)\n",
" out = layers.Dense(64, activation='relu')(out)\n",
2024-01-05 17:15:57 +08:00
" # out = K.expand_dims(out, axis=1)\n",
2024-01-05 09:12:27 +08:00
"\n",
2024-01-05 17:15:57 +08:00
" bet = layers.Dense(32, activation=\"relu\")(out)\n",
" mesco = layers.Dense(32, activation=\"relu\")(out)\n",
2024-01-05 09:12:27 +08:00
"\n",
" bet = layers.Dense(1, activation='sigmoid', name='vad')(bet)\n",
" mesco = layers.Dense(1, activation='sigmoid', name='fcad')(mesco)\n",
"\n",
" model = Model(inputs=[inputs], outputs=[bet, mesco])\n",
" return model\n"
]
},
{
"cell_type": "code",
2024-01-05 17:15:57 +08:00
"execution_count": 18,
2024-01-05 09:12:27 +08:00
"id": "264001b1-5e4a-4786-96fd-2b5c70ab3212",
"metadata": {},
"outputs": [],
"source": [
"def get_trainable_model(prediction_model):\n",
" inputs = layers.Input(shape=(1,len(feature_cols)), name='input')\n",
" bet, mesco = prediction_model(inputs)\n",
" bet_real = layers.Input(shape=(1,), name='vad_real')\n",
" mesco_real = layers.Input(shape=(1,), name='fcad_real')\n",
" out = CustomMultiLossLayer(nb_outputs=2)([bet_real, mesco_real, bet, mesco])\n",
" return Model([inputs, bet_real, mesco_real], out)"
]
},
2024-01-05 17:15:57 +08:00
{
"cell_type": "code",
"execution_count": 19,
"id": "372011ea-9876-41eb-a4e6-83ccd6c71559",
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.python.keras.utils.vis_utils import plot_model"
]
},
2024-01-05 09:12:27 +08:00
{
"cell_type": "code",
"execution_count": 20,
"id": "1eebdab3-1f88-48a1-b5e0-bc8787528c1b",
"metadata": {},
"outputs": [],
"source": [
"maxs = train_data.max()\n",
"mins = train_data.min()\n",
"for col in train_data.columns:\n",
" if maxs[col] - mins[col] == 0:\n",
" continue\n",
" train_data[col] = (train_data[col] - mins[col]) / (maxs[col] - mins[col])"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "7f27bd56-4f6b-4242-9f79-c7d6b3ee2f13",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>化验编号</th>\n",
" <th>氢Had(%)</th>\n",
" <th>碳Cad(%)</th>\n",
" <th>氮Nad(%)</th>\n",
" <th>氧Oad(%)</th>\n",
" <th>弹筒发热量Qb,adMJ/kg</th>\n",
" <th>挥发分Vad(%)</th>\n",
" <th>固定炭Fcad(%)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.996547</td>\n",
" <td>0.773973</td>\n",
" <td>0.835414</td>\n",
" <td>0.456522</td>\n",
" <td>0.171463</td>\n",
" <td>0.811249</td>\n",
" <td>0.847737</td>\n",
" <td>0.828147</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.851118</td>\n",
" <td>0.671233</td>\n",
" <td>0.799943</td>\n",
" <td>0.369565</td>\n",
" <td>0.210254</td>\n",
" <td>0.782038</td>\n",
" <td>0.674897</td>\n",
" <td>0.794606</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.981147</td>\n",
" <td>0.465753</td>\n",
" <td>0.818956</td>\n",
" <td>0.347826</td>\n",
" <td>0.200401</td>\n",
" <td>0.794256</td>\n",
" <td>0.621399</td>\n",
" <td>0.838866</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.721367</td>\n",
" <td>0.458904</td>\n",
" <td>0.736947</td>\n",
" <td>0.347826</td>\n",
" <td>0.279094</td>\n",
" <td>0.707183</td>\n",
" <td>0.561317</td>\n",
" <td>0.765560</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.487046</td>\n",
" <td>0.732877</td>\n",
" <td>0.795687</td>\n",
" <td>0.434783</td>\n",
" <td>0.211066</td>\n",
" <td>0.773331</td>\n",
" <td>0.675720</td>\n",
" <td>0.797026</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>223</th>\n",
" <td>0.143553</td>\n",
" <td>0.904110</td>\n",
" <td>0.797673</td>\n",
" <td>0.804348</td>\n",
" <td>0.197883</td>\n",
" <td>0.814339</td>\n",
" <td>0.920165</td>\n",
" <td>0.697095</td>\n",
" </tr>\n",
" <tr>\n",
" <th>224</th>\n",
" <td>0.123762</td>\n",
" <td>0.801370</td>\n",
" <td>0.746311</td>\n",
" <td>0.739130</td>\n",
" <td>0.251699</td>\n",
" <td>0.779510</td>\n",
" <td>0.832922</td>\n",
" <td>0.679461</td>\n",
" </tr>\n",
" <tr>\n",
" <th>225</th>\n",
" <td>0.123773</td>\n",
" <td>0.904110</td>\n",
" <td>0.785471</td>\n",
" <td>0.782609</td>\n",
" <td>0.209740</td>\n",
" <td>0.815884</td>\n",
" <td>0.938272</td>\n",
" <td>0.700899</td>\n",
" </tr>\n",
" <tr>\n",
" <th>226</th>\n",
" <td>0.007066</td>\n",
" <td>0.739726</td>\n",
" <td>0.757094</td>\n",
" <td>0.739130</td>\n",
" <td>0.244038</td>\n",
" <td>0.726705</td>\n",
" <td>0.813992</td>\n",
" <td>0.651107</td>\n",
" </tr>\n",
" <tr>\n",
" <th>227</th>\n",
" <td>0.007012</td>\n",
" <td>0.691781</td>\n",
" <td>0.737798</td>\n",
" <td>0.695652</td>\n",
" <td>0.264882</td>\n",
" <td>0.731760</td>\n",
" <td>0.762140</td>\n",
" <td>0.660097</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>228 rows × 8 columns</p>\n",
"</div>"
],
"text/plain": [
" 化验编号 氢Had(%) 碳Cad(%) 氮Nad(%) 氧Oad(%) 弹筒发热量Qb,adMJ/kg \\\n",
"0 0.996547 0.773973 0.835414 0.456522 0.171463 0.811249 \n",
"1 0.851118 0.671233 0.799943 0.369565 0.210254 0.782038 \n",
"2 0.981147 0.465753 0.818956 0.347826 0.200401 0.794256 \n",
"3 0.721367 0.458904 0.736947 0.347826 0.279094 0.707183 \n",
"4 0.487046 0.732877 0.795687 0.434783 0.211066 0.773331 \n",
".. ... ... ... ... ... ... \n",
"223 0.143553 0.904110 0.797673 0.804348 0.197883 0.814339 \n",
"224 0.123762 0.801370 0.746311 0.739130 0.251699 0.779510 \n",
"225 0.123773 0.904110 0.785471 0.782609 0.209740 0.815884 \n",
"226 0.007066 0.739726 0.757094 0.739130 0.244038 0.726705 \n",
"227 0.007012 0.691781 0.737798 0.695652 0.264882 0.731760 \n",
"\n",
" 挥发分Vad(%) 固定炭Fcad(%) \n",
"0 0.847737 0.828147 \n",
"1 0.674897 0.794606 \n",
"2 0.621399 0.838866 \n",
"3 0.561317 0.765560 \n",
"4 0.675720 0.797026 \n",
".. ... ... \n",
"223 0.920165 0.697095 \n",
"224 0.832922 0.679461 \n",
"225 0.938272 0.700899 \n",
"226 0.813992 0.651107 \n",
"227 0.762140 0.660097 \n",
"\n",
"[228 rows x 8 columns]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_data"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "baf45a3d-dc01-44fc-9f0b-456964ac2cdb",
"metadata": {},
"outputs": [],
"source": [
"# feature_cols = [x for x in train_data.columns if x not in out_cols and '第二次' not in x]\n",
"feature_cols = [x for x in train_data.columns if x not in out_cols]\n",
"use_cols = feature_cols + out_cols"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "f2d27538-d2bc-4202-b0cf-d3e0949b4686",
"metadata": {},
"outputs": [],
"source": [
"use_data = train_data.copy()\n",
"for col in use_cols:\n",
" use_data[col] = use_data[col].astype('float32')"
]
},
{
"cell_type": "code",
"execution_count": 24,
2024-01-05 17:15:57 +08:00
"id": "50daf170-efec-49e5-8f8e-9a45938cacfc",
2024-01-05 09:12:27 +08:00
"metadata": {},
"outputs": [],
"source": [
2024-01-05 17:15:57 +08:00
"from sklearn.model_selection import KFold, train_test_split\n",
"kf = KFold(n_splits=6, shuffle=True, random_state=42)"
2024-01-05 09:12:27 +08:00
]
},
{
"cell_type": "code",
2024-01-05 17:15:57 +08:00
"execution_count": 25,
"id": "0f863423-be12-478b-a08d-e3c6f5dfb8ee",
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras import optimizers\n",
"from tensorflow.python.keras.utils.vis_utils import plot_model\n",
"from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, mean_absolute_percentage_error"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "2c89b32a-017c-4d05-ab78-8b9b8eb0dcbb",
2024-01-05 09:12:27 +08:00
"metadata": {},
"outputs": [],
2024-01-05 17:15:57 +08:00
"source": [
"from keras.callbacks import ReduceLROnPlateau\n",
"reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=10, mode='auto')"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "ca6ce434-80b6-4609-9596-9a5120680462",
"metadata": {},
"outputs": [],
"source": [
"def print_eva(y_true, y_pred, tp):\n",
" MSE = mean_squared_error(y_true, y_pred)\n",
" RMSE = np.sqrt(MSE)\n",
" MAE = mean_absolute_error(y_true, y_pred)\n",
" MAPE = mean_absolute_percentage_error(y_true, y_pred)\n",
" R_2 = r2_score(y_true, y_pred)\n",
" print(f\"COL: {tp}, MSE: {format(MSE, '.2E')}\", end=',')\n",
" print(f'RMSE: {round(RMSE, 3)}', end=',')\n",
" print(f'MAPE: {round(MAPE * 100, 3)} %', end=',')\n",
" print(f'MAE: {round(MAE, 3)}', end=',')\n",
" print(f'R_2: {round(R_2, 3)}')\n",
" return [MSE, RMSE, MAE, MAPE, R_2]"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "10213bc5-bf13-46ed-9ce9-b1dbc5af72ee",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-01-05 16:46:22.503307: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcuda.so.1\n",
"2024-01-05 16:46:22.560854: E tensorflow/stream_executor/cuda/cuda_driver.cc:328] failed call to cuInit: CUDA_ERROR_INVALID_DEVICE: invalid device ordinal\n",
"2024-01-05 16:46:22.560909: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: zhaojh-yv621\n",
"2024-01-05 16:46:22.560920: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: zhaojh-yv621\n",
"2024-01-05 16:46:22.561113: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:200] libcuda reported version is: 520.61.5\n",
"2024-01-05 16:46:22.561132: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:204] kernel reported version is: 520.61.5\n",
"2024-01-05 16:46:22.561135: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:310] kernel version seems to match DSO: 520.61.5\n",
"2024-01-05 16:46:22.561424: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F FMA\n",
"To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n"
]
},
{
"ename": "ValueError",
"evalue": "in user code:\n\n /tmp/ipykernel_16320/2404117700.py:31 call *\n return K.concatenate(inputs, -1)\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:206 wrapper **\n return target(*args, **kwargs)\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/keras/backend.py:3098 concatenate\n return array_ops.concat([to_dense(x) for x in tensors], axis)\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:206 wrapper\n return target(*args, **kwargs)\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py:1768 concat\n return gen_array_ops.concat_v2(values=values, axis=axis, name=name)\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/ops/gen_array_ops.py:1227 concat_v2\n _, _, _op, _outputs = _op_def_library._apply_op_helper(\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py:748 _apply_op_helper\n op = g._create_op_internal(op_type_name, inputs, dtypes=None,\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py:599 _create_op_internal\n return super(FuncGraph, self)._create_op_internal( # pylint: disable=protected-access\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:3557 _create_op_internal\n ret = Operation(\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:2041 __init__\n self._c_op = _create_c_op(self._graph, node_def, inputs,\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:1883 _create_c_op\n raise ValueError(str(e))\n\n ValueError: Shape must be rank 2 but is rank 3 for '{{node custom_multi_loss_layer/concat}} = ConcatV2[N=4, T=DT_FLOAT, Tidx=DT_INT32](Placeholder, Placeholder_1, Placeholder_2, Placeholder_3, custom_multi_loss_layer/concat/axis)' with input shapes: [?,1], [?,1], [?,1,1], [?,1,1], [].\n",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[28], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m prediction_model \u001b[38;5;241m=\u001b[39m get_prediction_model()\n\u001b[0;32m----> 2\u001b[0m trainable_model \u001b[38;5;241m=\u001b[39m \u001b[43mget_trainable_model\u001b[49m\u001b[43m(\u001b[49m\u001b[43mprediction_model\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3\u001b[0m trainable_model\u001b[38;5;241m.\u001b[39mcompile(optimizer\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124madam\u001b[39m\u001b[38;5;124m'\u001b[39m, loss\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m)\n",
"Cell \u001b[0;32mIn[18], line 6\u001b[0m, in \u001b[0;36mget_trainable_model\u001b[0;34m(prediction_model)\u001b[0m\n\u001b[1;32m 4\u001b[0m bet_real \u001b[38;5;241m=\u001b[39m layers\u001b[38;5;241m.\u001b[39mInput(shape\u001b[38;5;241m=\u001b[39m(\u001b[38;5;241m1\u001b[39m,), name\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvad_real\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 5\u001b[0m mesco_real \u001b[38;5;241m=\u001b[39m layers\u001b[38;5;241m.\u001b[39mInput(shape\u001b[38;5;241m=\u001b[39m(\u001b[38;5;241m1\u001b[39m,), name\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mfcad_real\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m----> 6\u001b[0m out \u001b[38;5;241m=\u001b[39m \u001b[43mCustomMultiLossLayer\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnb_outputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[43mbet_real\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmesco_real\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mbet\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmesco\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m Model([inputs, bet_real, mesco_real], out)\n",
"File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:969\u001b[0m, in \u001b[0;36mLayer.__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 963\u001b[0m \u001b[38;5;66;03m# Functional Model construction mode is invoked when `Layer`s are called on\u001b[39;00m\n\u001b[1;32m 964\u001b[0m \u001b[38;5;66;03m# symbolic `KerasTensor`s, i.e.:\u001b[39;00m\n\u001b[1;32m 965\u001b[0m \u001b[38;5;66;03m# >> inputs = tf.keras.Input(10)\u001b[39;00m\n\u001b[1;32m 966\u001b[0m \u001b[38;5;66;03m# >> outputs = MyLayer()(inputs) # Functional construction mode.\u001b[39;00m\n\u001b[1;32m 967\u001b[0m \u001b[38;5;66;03m# >> model = tf.keras.Model(inputs, outputs)\u001b[39;00m\n\u001b[1;32m 968\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _in_functional_construction_mode(\u001b[38;5;28mself\u001b[39m, inputs, args, kwargs, input_list):\n\u001b[0;32m--> 969\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_functional_construction_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 970\u001b[0m \u001b[43m \u001b[49m\u001b[43minput_list\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 972\u001b[0m \u001b[38;5;66;03m# Maintains info about the `Layer.call` stack.\u001b[39;00m\n\u001b[1;32m 973\u001b[0m call_context \u001b[38;5;241m=\u001b[39m base_layer_utils\u001b[38;5;241m.\u001b[39mcall_context()\n",
"File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:1107\u001b[0m, in \u001b[0;36mLayer._functional_construction_call\u001b[0;34m(self, inputs, args, kwargs, input_list)\u001b[0m\n\u001b[1;32m 1102\u001b[0m training_arg_passed_by_framework \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m 1104\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m call_context\u001b[38;5;241m.\u001b[39menter(\n\u001b[1;32m 1105\u001b[0m layer\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m, inputs\u001b[38;5;241m=\u001b[39minputs, build_graph\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, training\u001b[38;5;241m=\u001b[39mtraining_value):\n\u001b[1;32m 1106\u001b[0m \u001b[38;5;66;03m# Check input assumptions set after layer building, e.g. input shape.\u001b[39;00m\n\u001b[0;32m-> 1107\u001b[0m outputs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_keras_tensor_symbolic_call\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1108\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minput_masks\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1110\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m outputs \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 1111\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mA layer\u001b[39m\u001b[38;5;130;01m\\'\u001b[39;00m\u001b[38;5;124ms `call` method should return a \u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 1112\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mTensor or a list of Tensors, not None \u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 1113\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m(layer: \u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mname \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m).\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
"File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:840\u001b[0m, in \u001b[0;36mLayer._keras_tensor_symbolic_call\u001b[0;34m(self, inputs, input_masks, args, kwargs)\u001b[0m\n\u001b[1;32m 838\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m nest\u001b[38;5;241m.\u001b[39mmap_structure(keras_tensor\u001b[38;5;241m.\u001b[39mKerasTensor, output_signature)\n\u001b[1;32m 839\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 840\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_infer_output_signature\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minput_masks\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:880\u001b[0m, in \u001b[0;36mLayer._infer_output_signature\u001b[0;34m(self, inputs, args, kwargs, input_masks)\u001b[0m\n\u001b[1;32m 878\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_maybe_build(inputs)\n\u001b[1;32m 879\u001b[0m inputs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_maybe_cast_inputs(inputs)\n\u001b[0;32m--> 880\u001b[0m outputs \u001b[38;5;241m=\u001b[39m \u001b[43mcall_fn\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 882\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_handle_activity_regularization(inputs, outputs)\n\u001b[1;32m 883\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_set_mask_metadata(inputs, outputs, input_masks,\n\u001b[1;32m 884\u001b[0m build_graph\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n",
"File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py:695\u001b[0m, in \u001b[0;36mconvert.<locals>.decorator.<locals>.wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 693\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e: \u001b[38;5;66;03m# pylint:disable=broad-except\u001b[39;00m\n\u001b[1;32m 694\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(e, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mag_error_metadata\u001b[39m\u001b[38;5;124m'\u001b[39m):\n\u001b[0;32m--> 695\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\u001b[38;5;241m.\u001b[39mag_error_metadata\u001b[38;5;241m.\u001b[39mto_exception(e)\n\u001b[1;32m 696\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 697\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m\n",
"\u001b[0;31mValueError\u001b[0m: in user code:\n\n /tmp/ipykernel_16320/2404117700.py:31 call *\n return K.concatenate(inputs, -1)\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:206 wrapper **\n return target(*args, **kwargs)\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/keras/backend.py:3098 concatenate\n return array_ops.concat([to_dense(x) for x in tensors], axis)\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:206 wrapper\n return target(*args, **kwargs)\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py:1768 concat\n return gen_array_ops.concat_v2(values=values, axis=axis, name=name)\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/ops/gen_array_ops.py:1227 concat_v2\n _, _, _op, _outputs = _op_def_library._apply_op_helper(\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py:748 _apply_op_helper\n op = g._create_op_internal(op_type_name, inputs, dtypes=None,\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py:599 _create_op_internal\n return super(FuncGraph, self)._create_op_internal( # pylint: disable=protected-access\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:3557 _create_op_internal\n ret = Operation(\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:2041 __init__\n self._c_op = _create_c_op(self._graph, node_def, inputs,\n /root/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:1883 _create_c_op\n raise ValueError(str(e))\n\n ValueError: Shape must be rank 2 but is rank 3 for '{{node custom_multi_loss_layer/concat}} = ConcatV2[N=4, T=DT_FLOAT, Tidx=DT_INT32](Placeholder, Placeholder_1, Placeholder_2, Placeholder_3, custom_multi_loss_layer/concat/axis)' with input shapes: [?,1], [?,1], [?,1,1], [?,1,1], [].\n"
]
}
],
2024-01-05 09:12:27 +08:00
"source": [
"prediction_model = get_prediction_model()\n",
2024-01-05 17:15:57 +08:00
"trainable_model = get_trainable_model(prediction_model)\n",
"trainable_model.compile(optimizer='adam', loss=None)"
2024-01-05 09:12:27 +08:00
]
},
{
"cell_type": "code",
2024-01-05 17:15:57 +08:00
"execution_count": null,
"id": "4a1be90d-b8f1-4fe1-9952-1cdcc489fab5",
"metadata": {},
"outputs": [],
"source": [
"plot_model(prediction_model)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "6308b1dc-8e2e-4bf9-9b28-3b81979bf7e0",
2024-01-05 09:12:27 +08:00
"metadata": {},
"outputs": [
2024-01-05 17:15:57 +08:00
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-01-05 13:55:16.952556: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)\n",
"2024-01-05 13:55:16.970806: I tensorflow/core/platform/profile_utils/cpu_utils.cc:114] CPU Frequency: 2200000000 Hz\n"
]
},
2024-01-05 09:12:27 +08:00
{
"name": "stdout",
"output_type": "stream",
"text": [
2024-01-05 17:15:57 +08:00
"COL: 挥发分Vad, MSE: 5.39E-01,RMSE: 0.734,MAPE: 1.948 %,MAE: 0.582,R_2: 0.883\n",
"COL: 固定炭Fcad, MSE: 7.77E-01,RMSE: 0.881,MAPE: 1.246 %,MAE: 0.654,R_2: 0.969\n",
"COL: 挥发分Vad, MSE: 8.80E-01,RMSE: 0.938,MAPE: 2.679 %,MAE: 0.783,R_2: 0.893\n",
"COL: 固定炭Fcad, MSE: 1.32E+00,RMSE: 1.149,MAPE: 1.814 %,MAE: 0.907,R_2: 0.974\n",
"COL: 挥发分Vad, MSE: 6.68E-01,RMSE: 0.817,MAPE: 2.064 %,MAE: 0.606,R_2: 0.829\n",
"COL: 固定炭Fcad, MSE: 9.89E-01,RMSE: 0.995,MAPE: 1.427 %,MAE: 0.798,R_2: 0.929\n",
"COL: 挥发分Vad, MSE: 6.34E-01,RMSE: 0.796,MAPE: 2.099 %,MAE: 0.62,R_2: 0.889\n",
"COL: 固定炭Fcad, MSE: 4.93E-01,RMSE: 0.702,MAPE: 1.058 %,MAE: 0.542,R_2: 0.985\n",
"WARNING:tensorflow:5 out of the last 9 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7f0801c91c10> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.\n",
"COL: 挥发分Vad, MSE: 2.34E+00,RMSE: 1.53,MAPE: 4.385 %,MAE: 1.317,R_2: 0.467\n",
"COL: 固定炭Fcad, MSE: 2.21E+02,RMSE: 14.87,MAPE: 27.662 %,MAE: 14.835,R_2: -9.385\n",
"WARNING:tensorflow:6 out of the last 11 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7f0801cf34c0> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.\n",
"COL: 挥发分Vad, MSE: 6.16E-01,RMSE: 0.785,MAPE: 2.29 %,MAE: 0.674,R_2: 0.873\n",
"COL: 固定炭Fcad, MSE: 1.04E+00,RMSE: 1.02,MAPE: 1.603 %,MAE: 0.811,R_2: 0.956\n"
2024-01-05 09:12:27 +08:00
]
}
],
"source": [
2024-01-05 17:15:57 +08:00
"vad_eva_list = list()\n",
"fcad_eva_list = list()\n",
"train_data = use_data[use_cols].copy()\n",
"for (train_index, test_index) in kf.split(train_data):\n",
" train = train_data.loc[train_index]\n",
" valid = train_data.loc[test_index]\n",
" X = np.expand_dims(train[feature_cols].values, axis=1)\n",
" Y = [x for x in train[out_cols].values.T]\n",
" X_valid = np.expand_dims(valid[feature_cols].values, axis=1)\n",
" Y_valid = [x for x in valid[out_cols].values.T]\n",
" prediction_model = get_prediction_model()\n",
" trainable_model = get_trainable_model(prediction_model)\n",
" trainable_model.compile(optimizer='adam', loss=None)\n",
" hist = trainable_model.fit([X, Y[0], Y[1]], epochs=120, batch_size=8, verbose=0, \n",
" validation_data=[X_valid, Y_valid[0], Y_valid[1]],\n",
" callbacks=[reduce_lr]\n",
" )\n",
" rst = prediction_model.predict(X_valid)\n",
" pred_rst = pd.DataFrame.from_records(np.squeeze(np.asarray(rst), axis=2).T, columns=out_cols)\n",
" real_rst = valid[out_cols].copy()\n",
" for col in out_cols:\n",
" pred_rst[col] = pred_rst[col] * (maxs[col] - mins[col]) + mins[col]\n",
" real_rst[col] = real_rst[col] * (maxs[col] - mins[col]) + mins[col]\n",
" y_pred_vad = pred_rst['挥发分Vad(%)'].values.reshape(-1,)\n",
" y_pred_fcad = pred_rst['固定炭Fcad(%)'].values.reshape(-1,)\n",
" y_true_vad = real_rst['挥发分Vad(%)'].values.reshape(-1,)\n",
" y_true_fcad = real_rst['固定炭Fcad(%)'].values.reshape(-1,)\n",
" vad_eva = print_eva(y_true_vad, y_pred_vad, tp='挥发分Vad')\n",
" fcad_eva = print_eva(y_true_fcad, y_pred_fcad, tp='固定炭Fcad')\n",
" vad_eva_list.append(vad_eva)\n",
" fcad_eva_list.append(fcad_eva)\n",
" del trainable_model\n",
" del prediction_model"
2024-01-05 09:12:27 +08:00
]
},
{
"cell_type": "code",
"execution_count": 33,
2024-01-05 17:15:57 +08:00
"id": "27e0abf7-aa29-467f-bc5e-b66a1adf6165",
2024-01-05 09:12:27 +08:00
"metadata": {},
2024-01-05 17:15:57 +08:00
"outputs": [
{
"data": {
"text/plain": [
"MSE 0.667414\n",
"RMSE 0.814141\n",
"MAE 0.652951\n",
"MAPE 0.022159\n",
"R_2 0.873633\n",
"dtype: float64"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
2024-01-05 09:12:27 +08:00
"source": [
2024-01-05 17:15:57 +08:00
"vad_df = pd.DataFrame.from_records(vad_eva_list, columns=['MSE', 'RMSE', 'MAE', 'MAPE', 'R_2'])\n",
"vad_df.sort_values(by='R_2')[1:].mean()"
2024-01-05 09:12:27 +08:00
]
},
{
"cell_type": "code",
"execution_count": 34,
2024-01-05 17:15:57 +08:00
"id": "070cdb94-6e7b-4028-b6d5-ba8570c902ba",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"MSE 0.923848\n",
"RMSE 0.949375\n",
"MAE 0.742411\n",
"MAPE 0.014295\n",
"R_2 0.962834\n",
"dtype: float64"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fcad_df = pd.DataFrame.from_records(fcad_eva_list, columns=['MSE', 'RMSE', 'MAE', 'MAPE', 'R_2'])\n",
"fcad_df.sort_values(by='R_2')[1:].mean()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "54c1df2c-c297-4b8d-be8a-3a99cff22545",
2024-01-05 09:12:27 +08:00
"metadata": {},
"outputs": [],
"source": [
2024-01-05 17:15:57 +08:00
"train, valid = train_test_split(use_data[use_cols], test_size=0.3, random_state=42, shuffle=True)\n",
"valid, test = train_test_split(valid, test_size=0.3, random_state=42, shuffle=True)"
2024-01-05 09:12:27 +08:00
]
},
{
"cell_type": "code",
2024-01-05 17:15:57 +08:00
"execution_count": 31,
"id": "e7a914da-b9c2-40d9-96e0-459b0888adba",
2024-01-05 09:12:27 +08:00
"metadata": {},
"outputs": [],
"source": [
2024-01-05 17:15:57 +08:00
"prediction_model = get_prediction_model()\n",
"trainable_model = get_trainable_model(prediction_model)"
2024-01-05 09:12:27 +08:00
]
},
{
"cell_type": "code",
2024-01-05 17:15:57 +08:00
"execution_count": 34,
"id": "2494ef5a-5b2b-4f11-b6cd-dc39503c9106",
"metadata": {},
"outputs": [],
"source": [
"X = np.expand_dims(train[feature_cols].values, axis=1)\n",
"Y = [x for x in train[out_cols].values.T]\n",
"Y_valid = [x for x in valid[out_cols].values.T]"
]
},
{
"cell_type": "code",
"execution_count": null,
2024-01-05 09:12:27 +08:00
"id": "cf869e4d-0fce-45a2-afff-46fd9b30fd1c",
"metadata": {},
2024-01-05 17:15:57 +08:00
"outputs": [],
2024-01-05 09:12:27 +08:00
"source": [
"trainable_model.compile(optimizer='adam', loss=None)\n",
"hist = trainable_model.fit([X, Y[0], Y[1]], epochs=120, batch_size=8, verbose=1, \n",
" validation_data=[np.expand_dims(valid[feature_cols].values, axis=1), Y_valid[0], Y_valid[1]],\n",
" callbacks=[reduce_lr]\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "67bfbe88-5f2c-4659-b2dc-eb9f1b824d04",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[array([[0.73740077],\n",
" [0.89292204],\n",
" [0.7599046 ],\n",
" [0.67802393],\n",
" [0.6815233 ],\n",
" [0.88627005],\n",
" [0.6121343 ],\n",
" [0.7072234 ],\n",
" [0.8561135 ],\n",
" [0.52762157],\n",
" [0.8325021 ],\n",
" [0.50241977],\n",
" [0.8242289 ],\n",
" [0.68957335],\n",
" [0.6980361 ],\n",
" [0.82116604],\n",
" [0.8566438 ],\n",
" [0.53687835],\n",
" [0.56832707],\n",
" [0.78476715],\n",
" [0.85638577]], dtype=float32),\n",
" array([[0.68600863],\n",
" [0.78454906],\n",
" [0.8179163 ],\n",
" [0.94351083],\n",
" [0.86383885],\n",
" [0.69705516],\n",
" [0.6913491 ],\n",
" [0.80277354],\n",
" [0.93557894],\n",
" [0.82278305],\n",
" [0.82674253],\n",
" [0.93518937],\n",
" [0.8094449 ],\n",
" [0.9206344 ],\n",
" [0.7747319 ],\n",
" [0.9137207 ],\n",
" [0.9491073 ],\n",
" [0.93225 ],\n",
" [0.6185102 ],\n",
" [0.8867341 ],\n",
" [0.82890105]], dtype=float32)]"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rst = prediction_model.predict(np.expand_dims(test[feature_cols], axis=1))\n",
"rst"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "7de501e9-05a2-424c-a5f4-85d43ad37592",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0.9991559102070927, 0.9998196796918477]"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[np.exp(K.get_value(log_var[0]))**0.5 for log_var in trainable_model.layers[-1].log_vars]"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "5c69d03b-34fd-4dbf-aec6-c15093bb22ab",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['挥发分Vad(%)', '固定炭Fcad(%)'], dtype='object')"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"real_rst.columns"
]
},
{
"cell_type": "code",
2024-01-05 17:15:57 +08:00
"execution_count": null,
"id": "294813b8-90be-4007-9fd6-c26ee7bb9652",
2024-01-05 09:12:27 +08:00
"metadata": {},
"outputs": [],
"source": [
2024-01-05 17:15:57 +08:00
"for col in out_cols:\n",
" pred_rst[col] = pred_rst[col] * (maxs[col] - mins[col]) + mins[col]\n",
" real_rst[col] = real_rst[col] * (maxs[col] - mins[col]) + mins[col]"
2024-01-05 09:12:27 +08:00
]
},
{
"cell_type": "code",
2024-01-05 17:15:57 +08:00
"execution_count": 47,
"id": "21739f82-d82a-4bde-8537-9504b68a96d5",
2024-01-05 09:12:27 +08:00
"metadata": {},
"outputs": [],
"source": [
2024-01-05 17:15:57 +08:00
"y_pred_vad = pred_rst['挥发分Vad(%)'].values.reshape(-1,)\n",
"y_pred_fcad = pred_rst['固定炭Fcad(%)'].values.reshape(-1,)\n",
"y_true_vad = real_rst['挥发分Vad(%)'].values.reshape(-1,)\n",
"y_true_fcad = real_rst['固定炭Fcad(%)'].values.reshape(-1,)"
2024-01-05 09:12:27 +08:00
]
},
{
"cell_type": "code",
2024-01-05 17:15:57 +08:00
"execution_count": 56,
2024-01-05 09:12:27 +08:00
"id": "4ec4caa9-7c46-4fc8-a94b-cb659e924304",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2024-01-05 17:15:57 +08:00
"COL: 挥发分Vad, MSE: 3.35E-01,RMSE: 0.579,MAPE: 1.639 %,MAE: 0.504,R_2: 0.87\n",
"COL: 固定炭Fcad, MSE: 1.11E+00,RMSE: 1.055,MAPE: 1.497 %,MAE: 0.814,R_2: 0.876\n"
2024-01-05 09:12:27 +08:00
]
}
],
"source": [
"pm25_eva = print_eva(y_true_vad, y_pred_vad, tp='挥发分Vad')\n",
"pm10_eva = print_eva(y_true_fcad, y_pred_fcad, tp='固定炭Fcad')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ac4a4339-ec7d-4266-8197-5276c2395288",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f15cbb91-1ce7-4fb0-979a-a4bdc452a1ec",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}