{ "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
碳源共碳化物质共碳化物/煤沥青加热次数是否有碳化过程模板剂种类模板剂比例KOH与煤沥青比例活化温度升温速率活化时间混合方式比表面积总孔体积微孔体积平均孔径
0煤沥青0.01自制氧化钙1.01.050052.0溶剂908.070.400.341.75
1煤沥青0.01自制氧化钙1.00.560052.0溶剂953.950.660.352.76
2煤沥青0.01自制氧化钙1.01.060052.0溶剂1388.620.610.531.77
3煤沥青0.01自制氧化钙1.02.060052.0溶剂1444.630.590.551.62
4煤沥青0.02自制碱式碳酸镁1.01.060052.0溶剂1020.990.450.351.77
\n", "
" ], "text/plain": [ " 碳源 共碳化物质 共碳化物/煤沥青 加热次数 是否有碳化过程 模板剂种类 模板剂比例 KOH与煤沥青比例 活化温度 升温速率 \\\n", "0 煤沥青 无 0.0 1 否 自制氧化钙 1.0 1.0 500 5 \n", "1 煤沥青 无 0.0 1 否 自制氧化钙 1.0 0.5 600 5 \n", "2 煤沥青 无 0.0 1 否 自制氧化钙 1.0 1.0 600 5 \n", "3 煤沥青 无 0.0 1 否 自制氧化钙 1.0 2.0 600 5 \n", "4 煤沥青 无 0.0 2 是 自制碱式碳酸镁 1.0 1.0 600 5 \n", "\n", " 活化时间 混合方式 比表面积 总孔体积 微孔体积 平均孔径 \n", "0 2.0 溶剂 908.07 0.40 0.34 1.75 \n", "1 2.0 溶剂 953.95 0.66 0.35 2.76 \n", "2 2.0 溶剂 1388.62 0.61 0.53 1.77 \n", "3 2.0 溶剂 1444.63 0.59 0.55 1.62 \n", "4 2.0 溶剂 1020.99 0.45 0.35 1.77 " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pd.read_excel('./data/20240123/煤沥青数据.xlsx')\n", "data.head()" ] }, { "cell_type": "code", "execution_count": 4, "id": "b016e6bd-b4de-4a6e-a3d5-2fc544042eb8", "metadata": {}, "outputs": [], "source": [ "data.drop(columns=['碳源'], inplace=True)" ] }, { "cell_type": "code", "execution_count": 5, "id": "d042e811-9548-480f-8a0b-27b72454fe43", "metadata": {}, "outputs": [], "source": [ "object_cols = ['共碳化物质', '是否有碳化过程', '模板剂种类', '混合方式']" ] }, { "cell_type": "code", "execution_count": 6, "id": "4ccf1708-e9cd-49d5-bdf0-27f6fdb60e1c", "metadata": {}, "outputs": [], "source": [ "data_0102 = pd.get_dummies(data, columns=object_cols)" ] }, { "cell_type": "code", "execution_count": 7, "id": "04b177a7-2f02-4e23-8ea9-29f34cf3eafc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['共碳化物/煤沥青',\n", " '加热次数',\n", " '模板剂比例',\n", " 'KOH与煤沥青比例',\n", " '活化温度',\n", " '升温速率',\n", " '活化时间',\n", " '共碳化物质_2-甲基咪唑',\n", " '共碳化物质_三聚氰胺',\n", " '共碳化物质_尿素',\n", " '共碳化物质_无',\n", " '共碳化物质_硫酸铵',\n", " '共碳化物质_聚磷酸铵',\n", " '是否有碳化过程_否',\n", " '是否有碳化过程_是',\n", " '模板剂种类_Al2O3',\n", " '模板剂种类_TiO2',\n", " '模板剂种类_α-Fe2O3',\n", " '模板剂种类_γ-Fe2O3',\n", " '模板剂种类_二氧化硅',\n", " '模板剂种类_无',\n", " '模板剂种类_氯化钾',\n", " '模板剂种类_纤维素',\n", " '模板剂种类_自制氢氧化镁',\n", " '模板剂种类_自制氧化钙',\n", " '模板剂种类_自制氧化锌',\n", " '模板剂种类_自制氧化镁',\n", " '模板剂种类_自制碱式碳酸镁',\n", " '模板剂种类_购买氢氧化镁',\n", " '模板剂种类_购买氧化钙',\n", " '模板剂种类_购买氧化锌',\n", " '模板剂种类_购买氧化镁',\n", " '模板剂种类_购买氯化钠',\n", " '模板剂种类_购买碳酸钙',\n", " '混合方式_溶剂',\n", " '混合方式_研磨']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out_cols = ['比表面积', '总孔体积', '微孔体积', '平均孔径']\n", "feature_cols = [x for x in data_0102.columns if x not in out_cols]\n", "feature_cols" ] }, { "cell_type": "code", "execution_count": 8, "id": "31169fbf-d78e-42f7-87f3-71ba3dd0979d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['比表面积', '总孔体积', '微孔体积', '平均孔径']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out_cols" ] }, { "cell_type": "code", "execution_count": 9, "id": "535d37b6-b9de-4025-ac8f-62f5bdbe2451", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-01-23 12:21:49.081644: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.\n", "2024-01-23 12:21:49.083823: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.\n", "2024-01-23 12:21:49.125771: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.\n", "2024-01-23 12:21:49.126872: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", "To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", "2024-01-23 12:21:50.338510: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n" ] } ], "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": 10, "id": "790284a3-b9d3-4144-b481-38a7c3ecb4b9", "metadata": {}, "outputs": [], "source": [ "from tensorflow.keras import Model" ] }, { "cell_type": "code", "execution_count": 12, "id": "cd9a1ca1-d0ca-4cb5-9ef5-fd5d63576cd2", "metadata": {}, "outputs": [], "source": [ "from tensorflow.keras.initializers import Constant" ] }, { "cell_type": "code", "execution_count": 13, "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)(lstm_out)\n", " out = layers.Dense(64, activation='relu')(out)\n", " bet = layers.Dense(1, activation='sigmoid', name='vad')(out)\n", " model = Model(inputs=[inputs], outputs=[bet])\n", " return model" ] }, { "cell_type": "code", "execution_count": 14, "id": "7a9915ee-0016-44e5-a6fb-5ee90532dc14", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-01-23 12:22:03.707721: E tensorflow/compiler/xla/stream_executor/cuda/cuda_driver.cc:268] failed call to cuInit: CUDA_ERROR_INVALID_DEVICE: invalid device ordinal\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Model: \"model\"\n", "_________________________________________________________________\n", " Layer (type) Output Shape Param # \n", "=================================================================\n", " input (InputLayer) [(None, 1, 36)] 0 \n", " \n", " conv1d (Conv1D) (None, 1, 64) 2368 \n", " \n", " bidirectional (Bidirection (None, 1, 128) 66048 \n", " al) \n", " \n", " dense (Dense) (None, 1, 128) 16512 \n", " \n", " dropout (Dropout) (None, 1, 128) 0 \n", " \n", " dense_1 (Dense) (None, 1, 64) 8256 \n", " \n", " vad (Dense) (None, 1, 1) 65 \n", " \n", "=================================================================\n", "Total params: 93249 (364.25 KB)\n", "Trainable params: 93249 (364.25 KB)\n", "Non-trainable params: 0 (0.00 Byte)\n", "_________________________________________________________________\n" ] } ], "source": [ "model = get_prediction_model()\n", "model.summary()" ] }, { "cell_type": "code", "execution_count": 15, "id": "372011ea-9876-41eb-a4e6-83ccd6c71559", "metadata": {}, "outputs": [], "source": [ "from tensorflow.python.keras.utils.vis_utils import plot_model" ] }, { "cell_type": "code", "execution_count": 16, "id": "965b1d6e-8b9f-4536-8205-06b314aeab51", "metadata": {}, "outputs": [], "source": [ "train_data = data_0102.copy()" ] }, { "cell_type": "code", "execution_count": 17, "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": 18, "id": "7f27bd56-4f6b-4242-9f79-c7d6b3ee2f13", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
共碳化物/煤沥青加热次数模板剂比例KOH与煤沥青比例活化温度升温速率活化时间比表面积总孔体积微孔体积...模板剂种类_自制氧化镁模板剂种类_自制碱式碳酸镁模板剂种类_购买氢氧化镁模板剂种类_购买氧化钙模板剂种类_购买氧化锌模板剂种类_购买氧化镁模板剂种类_购买氯化钠模板剂种类_购买碳酸钙混合方式_溶剂混合方式_研磨
00.00.00.10.0666670.0000000.30.3333330.2734370.1376280.270767...0.00.00.00.00.00.00.00.01.00.0
10.00.00.10.0333330.1666670.30.3333330.2873450.2291450.278754...0.00.00.00.00.00.00.00.01.00.0
20.00.00.10.0666670.1666670.30.3333330.4191030.2115450.422524...0.00.00.00.00.00.00.00.01.00.0
30.00.00.10.1333330.1666670.30.3333330.4360810.2045050.438498...0.00.00.00.00.00.00.00.01.00.0
40.01.00.10.0666670.1666670.30.3333330.3076660.1552270.278754...0.01.00.00.00.00.00.00.01.00.0
..................................................................
1440.00.00.00.2666670.1666670.30.0000000.5923010.3312210.638179...0.00.00.00.00.00.00.00.00.01.0
1450.00.00.00.2666670.3333330.30.0000000.8435890.4720170.941693...0.00.00.00.00.00.00.00.00.01.0
1460.00.00.00.2666670.5000000.30.0000000.6826310.3769800.781949...0.00.00.00.00.00.00.00.00.01.0
1470.00.00.00.2000000.3333330.30.0000000.5695670.2925030.614217...0.00.00.00.00.00.00.00.00.01.0
1480.00.00.00.3333330.3333330.30.0000000.7769020.3945790.885783...0.00.00.00.00.00.00.00.00.01.0
\n", "

149 rows × 40 columns

\n", "
" ], "text/plain": [ " 共碳化物/煤沥青 加热次数 模板剂比例 KOH与煤沥青比例 活化温度 升温速率 活化时间 比表面积 \\\n", "0 0.0 0.0 0.1 0.066667 0.000000 0.3 0.333333 0.273437 \n", "1 0.0 0.0 0.1 0.033333 0.166667 0.3 0.333333 0.287345 \n", "2 0.0 0.0 0.1 0.066667 0.166667 0.3 0.333333 0.419103 \n", "3 0.0 0.0 0.1 0.133333 0.166667 0.3 0.333333 0.436081 \n", "4 0.0 1.0 0.1 0.066667 0.166667 0.3 0.333333 0.307666 \n", ".. ... ... ... ... ... ... ... ... \n", "144 0.0 0.0 0.0 0.266667 0.166667 0.3 0.000000 0.592301 \n", "145 0.0 0.0 0.0 0.266667 0.333333 0.3 0.000000 0.843589 \n", "146 0.0 0.0 0.0 0.266667 0.500000 0.3 0.000000 0.682631 \n", "147 0.0 0.0 0.0 0.200000 0.333333 0.3 0.000000 0.569567 \n", "148 0.0 0.0 0.0 0.333333 0.333333 0.3 0.000000 0.776902 \n", "\n", " 总孔体积 微孔体积 ... 模板剂种类_自制氧化镁 模板剂种类_自制碱式碳酸镁 模板剂种类_购买氢氧化镁 \\\n", "0 0.137628 0.270767 ... 0.0 0.0 0.0 \n", "1 0.229145 0.278754 ... 0.0 0.0 0.0 \n", "2 0.211545 0.422524 ... 0.0 0.0 0.0 \n", "3 0.204505 0.438498 ... 0.0 0.0 0.0 \n", "4 0.155227 0.278754 ... 0.0 1.0 0.0 \n", ".. ... ... ... ... ... ... \n", "144 0.331221 0.638179 ... 0.0 0.0 0.0 \n", "145 0.472017 0.941693 ... 0.0 0.0 0.0 \n", "146 0.376980 0.781949 ... 0.0 0.0 0.0 \n", "147 0.292503 0.614217 ... 0.0 0.0 0.0 \n", "148 0.394579 0.885783 ... 0.0 0.0 0.0 \n", "\n", " 模板剂种类_购买氧化钙 模板剂种类_购买氧化锌 模板剂种类_购买氧化镁 模板剂种类_购买氯化钠 模板剂种类_购买碳酸钙 混合方式_溶剂 \\\n", "0 0.0 0.0 0.0 0.0 0.0 1.0 \n", "1 0.0 0.0 0.0 0.0 0.0 1.0 \n", "2 0.0 0.0 0.0 0.0 0.0 1.0 \n", "3 0.0 0.0 0.0 0.0 0.0 1.0 \n", "4 0.0 0.0 0.0 0.0 0.0 1.0 \n", ".. ... ... ... ... ... ... \n", "144 0.0 0.0 0.0 0.0 0.0 0.0 \n", "145 0.0 0.0 0.0 0.0 0.0 0.0 \n", "146 0.0 0.0 0.0 0.0 0.0 0.0 \n", "147 0.0 0.0 0.0 0.0 0.0 0.0 \n", "148 0.0 0.0 0.0 0.0 0.0 0.0 \n", "\n", " 混合方式_研磨 \n", "0 0.0 \n", "1 0.0 \n", "2 0.0 \n", "3 0.0 \n", "4 0.0 \n", ".. ... \n", "144 1.0 \n", "145 1.0 \n", "146 1.0 \n", "147 1.0 \n", "148 1.0 \n", "\n", "[149 rows x 40 columns]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "train_data" ] }, { "cell_type": "code", "execution_count": 19, "id": "baf45a3d-dc01-44fc-9f0b-456964ac2cdb", "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import KFold, train_test_split" ] }, { "cell_type": "code", "execution_count": 20, "id": "70414dca-d785-4f70-9521-6e58221486be", "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": 21, "id": "7f985922-75d4-45f2-9603-4a38ca84f696", "metadata": {}, "outputs": [], "source": [ "from keras.callbacks import ReduceLROnPlateau\n", "reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=10, mode='auto')" ] }, { "cell_type": "code", "execution_count": 22, "id": "266cd0ae-5681-402b-a4f7-ef705e3ac0cb", "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": 23, "id": "a1e4e900-b97d-4c52-88ad-439b80866f6b", "metadata": {}, "outputs": [], "source": [ "from keras.losses import mean_squared_error" ] }, { "cell_type": "code", "execution_count": 25, "id": "c47baf5e-8557-46d7-b67d-85530baf1af0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "比表面积\n", "1/1 [==============================] - 1s 588ms/step\n", "COL: 比表面积, MSE: 6.12E+05,RMSE: 782.4299926757812,MAPE: 409.191 %,MAE: 622.412,R_2: 0.034\n", "1/1 [==============================] - 1s 546ms/step\n", "COL: 比表面积, MSE: 4.94E+05,RMSE: 703.1320190429688,MAPE: 400.892 %,MAE: 560.341,R_2: 0.019\n", "1/1 [==============================] - 1s 576ms/step\n", "COL: 比表面积, MSE: 7.66E+05,RMSE: 875.4010009765625,MAPE: 807.376 %,MAE: 735.814,R_2: 0.043\n", "1/1 [==============================] - 1s 1s/step\n", "COL: 比表面积, MSE: 6.41E+05,RMSE: 800.6019897460938,MAPE: 1664.653 %,MAE: 621.767,R_2: -0.055\n", "WARNING:tensorflow:5 out of the last 5 calls to .predict_function at 0x7fdc106a8b80> 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 reduce_retracing=True option 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", "1/1 [==============================] - 1s 571ms/step\n", "COL: 比表面积, MSE: 6.27E+05,RMSE: 791.9520263671875,MAPE: 756.572 %,MAE: 627.752,R_2: -0.022\n", "总孔体积\n", "WARNING:tensorflow:6 out of the last 6 calls to .predict_function at 0x7fdb30784b80> 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 reduce_retracing=True option 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", "1/1 [==============================] - 1s 540ms/step\n", "COL: 总孔体积, MSE: 2.80E-01,RMSE: 0.5289999842643738,MAPE: 313.778 %,MAE: 0.428,R_2: 0.153\n", "1/1 [==============================] - 1s 574ms/step\n", "COL: 总孔体积, MSE: 1.50E-01,RMSE: 0.3880000114440918,MAPE: 381.536 %,MAE: 0.32,R_2: 0.016\n", "1/1 [==============================] - 1s 556ms/step\n", "COL: 总孔体积, MSE: 2.93E-01,RMSE: 0.5419999957084656,MAPE: 166.29 %,MAE: 0.384,R_2: 0.104\n", "1/1 [==============================] - 1s 540ms/step\n", "COL: 总孔体积, MSE: 3.27E-01,RMSE: 0.5720000267028809,MAPE: 428.563 %,MAE: 0.426,R_2: 0.124\n", "1/1 [==============================] - 1s 557ms/step\n", "COL: 总孔体积, MSE: 3.52E-01,RMSE: 0.5929999947547913,MAPE: 333.533 %,MAE: 0.405,R_2: 0.12\n", "微孔体积\n", "1/1 [==============================] - 1s 541ms/step\n", "COL: 微孔体积, MSE: 7.01E-02,RMSE: 0.26499998569488525,MAPE: 597.598 %,MAE: 0.212,R_2: 0.031\n", "1/1 [==============================] - 1s 544ms/step\n", "COL: 微孔体积, MSE: 1.05E-01,RMSE: 0.3240000009536743,MAPE: 1788.931 %,MAE: 0.273,R_2: 0.088\n", "1/1 [==============================] - 1s 553ms/step\n", "COL: 微孔体积, MSE: 9.10E-02,RMSE: 0.3019999861717224,MAPE: 3142.796 %,MAE: 0.238,R_2: 0.033\n", "1/1 [==============================] - 1s 559ms/step\n", "COL: 微孔体积, MSE: 1.26E-01,RMSE: 0.35600000619888306,MAPE: 1105.4 %,MAE: 0.298,R_2: 0.065\n", "1/1 [==============================] - 1s 681ms/step\n", "COL: 微孔体积, MSE: 9.03E-02,RMSE: 0.3009999990463257,MAPE: 466.473 %,MAE: 0.226,R_2: 0.026\n", "平均孔径\n", "1/1 [==============================] - 1s 612ms/step\n", "COL: 平均孔径, MSE: 1.35E+00,RMSE: 1.159999966621399,MAPE: 30.229 %,MAE: 0.837,R_2: 0.108\n", "1/1 [==============================] - 1s 560ms/step\n", "COL: 平均孔径, MSE: 2.38E+00,RMSE: 1.5420000553131104,MAPE: 37.616 %,MAE: 1.076,R_2: 0.095\n", "1/1 [==============================] - 1s 571ms/step\n", "COL: 平均孔径, MSE: 2.05E+00,RMSE: 1.4329999685287476,MAPE: 32.263 %,MAE: 0.996,R_2: 0.03\n", "1/1 [==============================] - 1s 530ms/step\n", "COL: 平均孔径, MSE: 1.42E+00,RMSE: 1.1920000314712524,MAPE: 33.395 %,MAE: 0.899,R_2: 0.221\n", "1/1 [==============================] - 1s 532ms/step\n", "COL: 平均孔径, MSE: 1.58E+00,RMSE: 1.2589999437332153,MAPE: 33.747 %,MAE: 0.931,R_2: 0.009\n" ] } ], "source": [ "for pred_col in out_cols:\n", " print(pred_col)\n", " use_cols = feature_cols + [pred_col]\n", " use_data = train_data[use_cols].dropna().reset_index(drop=True)\n", " kf = KFold(n_splits=5, shuffle=True, random_state=42)\n", " vad_eva_list = list()\n", " for (train_index, test_index) in kf.split(use_data):\n", " train = use_data.loc[train_index]\n", " valid = use_data.loc[test_index]\n", " X = np.expand_dims(train[feature_cols].values, axis=1)\n", " Y = train[pred_col].values.T\n", " X_valid = np.expand_dims(valid[feature_cols].values, axis=1)\n", " Y_valid = valid[pred_col].values.T\n", " prediction_model = get_prediction_model()\n", " prediction_model.compile(optimizer='adam', loss=mean_squared_error)\n", " hist = prediction_model.fit(X, Y, epochs=120, batch_size=8, verbose=0, \n", " validation_data=(X_valid, Y_valid),\n", " callbacks=[reduce_lr]\n", " )\n", " rst = prediction_model.predict(X_valid)\n", " pred_rst = rst * (maxs[pred_col] - mins[pred_col]) + mins[pred_col]\n", " real_rst = valid[pred_col] * (maxs[pred_col] - mins[pred_col]) + mins[pred_col]\n", " y_pred_vad = pred_rst.reshape(-1,)\n", " y_true_vad = real_rst.values.reshape(-1,)\n", " vad_eva = print_eva(y_true_vad, y_pred_vad, tp=pred_col)\n", " vad_eva_list.append(vad_eva)\n", " del prediction_model" ] }, { "cell_type": "code", "execution_count": null, "id": "27e0abf7-aa29-467f-bc5e-b66a1adf6165", "metadata": {}, "outputs": [], "source": [ "vad_df = pd.DataFrame.from_records(vad_eva_list, columns=['MSE', 'RMSE', 'MAE', 'MAPE', 'R_2'])\n", "vad_df.sort_values(by='R_2').mean()" ] }, { "cell_type": "code", "execution_count": 52, "id": "070cdb94-6e7b-4028-b6d5-ba8570c902ba", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MSE 0.317628\n", "RMSE 0.557178\n", "MAE 0.412263\n", "MAPE 0.007993\n", "R_2 0.986373\n", "dtype: float64" ] }, "execution_count": 52, "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').mean()" ] }, { "cell_type": "code", "execution_count": null, "id": "54c1df2c-c297-4b8d-be8a-3a99cff22545", "metadata": {}, "outputs": [], "source": [ "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)" ] }, { "cell_type": "code", "execution_count": 31, "id": "e7a914da-b9c2-40d9-96e0-459b0888adba", "metadata": {}, "outputs": [], "source": [ "prediction_model = get_prediction_model()\n", "trainable_model = get_trainable_model(prediction_model)" ] }, { "cell_type": "code", "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, "id": "cf869e4d-0fce-45a2-afff-46fd9b30fd1c", "metadata": {}, "outputs": [], "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", "execution_count": null, "id": "294813b8-90be-4007-9fd6-c26ee7bb9652", "metadata": {}, "outputs": [], "source": [ "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]" ] }, { "cell_type": "code", "execution_count": 47, "id": "21739f82-d82a-4bde-8537-9504b68a96d5", "metadata": {}, "outputs": [], "source": [ "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,)" ] }, { "cell_type": "code", "execution_count": 56, "id": "4ec4caa9-7c46-4fc8-a94b-cb659e924304", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "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" ] } ], "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.18" } }, "nbformat": 4, "nbformat_minor": 5 }