{ "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": [], "source": [ "data = pd.read_csv('./data/20240102/train_data.csv')" ] }, { "cell_type": "code", "execution_count": 4, "id": "04b177a7-2f02-4e23-8ea9-29f34cf3eafc", "metadata": {}, "outputs": [], "source": [ "out_cols = [x for x in data.columns if '碳材料' in x]" ] }, { "cell_type": "code", "execution_count": 5, "id": "31169fbf-d78e-42f7-87f3-71ba3dd0979d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['碳材料结构特征-比表面积', '碳材料结构特征-总孔体积', '碳材料结构特征-微孔体积', '碳材料结构特征-平均孔径']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out_cols" ] }, { "cell_type": "code", "execution_count": 6, "id": "a40bee0f-011a-4edb-80f8-4e2f40e755fd", "metadata": {}, "outputs": [], "source": [ "train_data = data.dropna(subset=out_cols).fillna(0)" ] }, { "cell_type": "code", "execution_count": 7, "id": "535d37b6-b9de-4025-ac8f-62f5bdbe2451", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-01-04 16:22:35.199530: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0\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": 8, "id": "c2318ce6-60d2-495c-91cd-67ca53609cf8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WARNING:tensorflow:From /tmp/ipykernel_44444/337460670.py:1: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.\n", "Instructions for updating:\n", "Use `tf.config.list_physical_devices('GPU')` instead.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "2024-01-04 16:22:36.097926: 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", "2024-01-04 16:22:36.142225: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcuda.so.1\n", "2024-01-04 16:22:36.232036: E tensorflow/stream_executor/cuda/cuda_driver.cc:328] failed call to cuInit: CUDA_ERROR_INVALID_DEVICE: invalid device ordinal\n", "2024-01-04 16:22:36.232061: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: zhaojh-yv621\n", "2024-01-04 16:22:36.232065: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: zhaojh-yv621\n", "2024-01-04 16:22:36.232185: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:200] libcuda reported version is: 520.61.5\n", "2024-01-04 16:22:36.232204: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:204] kernel reported version is: 520.61.5\n", "2024-01-04 16:22:36.232207: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:310] kernel version seems to match DSO: 520.61.5\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.test.is_gpu_available()" ] }, { "cell_type": "code", "execution_count": 9, "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", "execution_count": 10, "id": "790284a3-b9d3-4144-b481-38a7c3ecb4b9", "metadata": {}, "outputs": [], "source": [ "from tensorflow.keras import Model" ] }, { "cell_type": "code", "execution_count": 11, "id": "cd9a1ca1-d0ca-4cb5-9ef5-fd5d63576cd2", "metadata": {}, "outputs": [], "source": [ "from tensorflow.keras.initializers import Constant" ] }, { "cell_type": "code", "execution_count": 12, "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", "execution_count": 13, "id": "a190207e-5a59-4813-9660-758760cf1b73", "metadata": {}, "outputs": [], "source": [ "num_heads, ff_dim = 1, 12" ] }, { "cell_type": "code", "execution_count": 14, "id": "80f32155-e71f-4615-8d0c-01dfd04988fe", "metadata": {}, "outputs": [], "source": [ "def get_prediction_model():\n", " def build_output(out, out_name):\n", " self_block = TransformerBlock(64, num_heads, ff_dim, name=f'{out_name}_attn')\n", " out = self_block(out)\n", " out = layers.GlobalAveragePooling1D()(out)\n", " out = layers.Dropout(0.1)(out)\n", " out = layers.Dense(32, activation=\"relu\")(out)\n", " # out = layers.Dense(1, name=out_name, activation=\"sigmoid\")(out)\n", " return out\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", " out = K.expand_dims(out, axis=1)\n", "\n", " bet = build_output(out, 'bet')\n", " mesco = build_output(out, 'mesco')\n", " micro = build_output(out, 'micro')\n", " avg = build_output(out, 'avg')\n", "\n", " bet = layers.Dense(1, activation='sigmoid', name='bet')(bet)\n", " mesco = layers.Dense(1, activation='sigmoid', name='mesco')(mesco)\n", " micro = layers.Dense(1, activation='sigmoid', name='micro')(micro)\n", " avg = layers.Dense(1, activation='sigmoid', name='avg')(avg)\n", "\n", " model = Model(inputs=[inputs], outputs=[bet, mesco, micro, avg])\n", " return model\n" ] }, { "cell_type": "code", "execution_count": 15, "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, micro, avg = prediction_model(inputs)\n", " bet_real = layers.Input(shape=(1,), name='bet_real')\n", " mesco_real = layers.Input(shape=(1,), name='mesco_real')\n", " micro_real = layers.Input(shape=(1,), name='micro_real')\n", " avg_real = layers.Input(shape=(1,), name='avg_real')\n", " out = CustomMultiLossLayer(nb_outputs=4)([bet_real, mesco_real, micro_real, avg_real, bet, mesco, micro, avg])\n", " return Model([inputs, bet_real, mesco_real, micro_real, avg_real], out)" ] }, { "cell_type": "code", "execution_count": 16, "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": 17, "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", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "0.0 | \n", "0.0 | \n", "0.166667 | \n", "0.3 | \n", "0.5 | \n", "0.000000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "... | \n", "0 | \n", "0.0 | \n", "1.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "
1 | \n", "0.0 | \n", "0.0 | \n", "0.333333 | \n", "0.3 | \n", "0.5 | \n", "0.000000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "... | \n", "0 | \n", "0.0 | \n", "1.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "
2 | \n", "0.0 | \n", "0.0 | \n", "0.333333 | \n", "0.3 | \n", "0.5 | \n", "0.000000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "... | \n", "0 | \n", "0.0 | \n", "1.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "
3 | \n", "0.0 | \n", "0.0 | \n", "0.333333 | \n", "0.3 | \n", "0.5 | \n", "0.000000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "... | \n", "0 | \n", "0.0 | \n", "1.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "
4 | \n", "1.0 | \n", "0.0 | \n", "0.166667 | \n", "0.3 | \n", "0.5 | \n", "0.666667 | \n", "0.5 | \n", "0.666667 | \n", "0.0 | \n", "0.0 | \n", "... | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "1.0 | \n", "0.0 | \n", "0.0 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
144 | \n", "0.0 | \n", "0.0 | \n", "0.333333 | \n", "0.3 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "... | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "
145 | \n", "0.0 | \n", "0.0 | \n", "0.500000 | \n", "0.3 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "... | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "
146 | \n", "0.0 | \n", "0.0 | \n", "0.666667 | \n", "0.3 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "... | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "
147 | \n", "0.0 | \n", "0.0 | \n", "0.500000 | \n", "0.3 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "... | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "
148 | \n", "0.0 | \n", "0.0 | \n", "0.500000 | \n", "0.3 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "... | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "
123 rows × 42 columns
\n", "