-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutimpnn.py
306 lines (263 loc) · 10.3 KB
/
mutimpnn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import os
import random
# Temporary suppress tf logs
# os.environ["TF_CPP_MIN_LOG_LEVEL"] = "0"
# os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
import tensorflow as tf
from tensorflow import keras
import numpy as np
import pandas as pd
import warnings
import argparse
import datasets
from tqdm import tqdm
import matplotlib.pyplot as plt
from rdkit import RDLogger
import camodels
from dataset import graphs_from_smiles, process_me_attributes
from tool import preparedataset, manual_iter, RSquared, ModelCheckpointWithCleanup, getdateset
from tool import RSquaredLoss
# tf.compat.v1.disable_eager_execution()
# Temporary suppress warnings and RDKit logs
warnings.filterwarnings("ignore")
RDLogger.DisableLog("rdApp.*")
np.random.seed(42)
tf.random.set_seed(42)
ap = argparse.ArgumentParser()
ap.add_argument(
"--dataset",
type=str,
default=".\\mejpg",
help="path to input dataset of house images",
)
ap.add_argument(
"--model", type=str, default="densenet", help="name to input dataset of model"
)
ap.add_argument(
"--batchsize", type=int, default=32, help="batchsize"
)
ap.add_argument("--trainsize", type=float, default=0.8, help="trainsize")
args = vars(ap.parse_args())
print("[INFO] loading attributes...")
batchsize = args["batchsize"]
train_size = args["trainsize"]
inputPath = os.path.sep.join([args["dataset"], "DESs.xlsx"])
df = datasets.load_me_attributes(inputPath)
train_index, valid_index, test_index = getdateset(df)
random.shuffle(train_index)
random.shuffle(valid_index)
df_hba = df["D1"]
df_hbd = df["D2"]
df = df[["HBA:HBD", "T", "Density"]]
AttrX = process_me_attributes(df,continuous=["HBA:HBD", "T"],label=False)
df = df["Density"]
df= np.log10(df)
# df = process_me_attributes(df, continuous=["Density"], label=False)
# Dataset_split
datacount = df.shape[0]
permuted_indices = np.random.permutation(np.arange(datacount))
# train_index = permuted_indices[: int(datacount * train_size)]
# valid_index = permuted_indices[int(datacount * train_size) : int(datacount * 0.99)]
# test_index = permuted_indices[int(datacount * 0.99) :]
Attrx_train = AttrX[train_index]
hbax_train = graphs_from_smiles(df_hba[train_index])
hbdx_train = graphs_from_smiles(df_hbd[train_index])
y_train = df[train_index]
Attrx_valid = AttrX[valid_index]
hbax_valid = graphs_from_smiles(df_hba[valid_index])
hbdx_valid = graphs_from_smiles(df_hbd[valid_index])
y_valid = df[valid_index]
Attrx_test = AttrX[test_index]
hbax_test = graphs_from_smiles(df_hba[test_index])
hbdx_test = graphs_from_smiles(df_hbd[test_index])
y_test = df[test_index]
tratensor = (
tf.data.Dataset.from_tensor_slices((Attrx_train, hbax_train, hbdx_train, y_train))
.batch(batchsize)
.map(preparedataset, -1)
)
valtensor = (
tf.data.Dataset.from_tensor_slices((Attrx_valid, hbax_valid, hbdx_valid, y_valid))
.batch(batchsize)
.map(preparedataset, -1)
)
traindatasets = manual_iter(tratensor)
validdatasets = manual_iter(valtensor)
tensorboard_callback = keras.callbacks.TensorBoard(log_dir="./logs", histogram_freq=1)
reduce_lr = keras.callbacks.ReduceLROnPlateau(
monitor="val_loss", factor=0.2, patience=3, min_lr=1e-4
)
# 构建模型
model = camodels.Finalmodel(batchsize=batchsize, num_heads=8, d_model=16)
# pretrained_model_path = "best_mpnn_model.h5"
# model = keras.models.load_model(pretrained_model_path)
# 检查每一层是否加载成功
# for layer in model.layers:
# try:
# # 获取层的权重
# weights = layer.get_weights()
# print(
# f"Layer '{layer.name}' loaded successfully with weights shape: {[w.shape for w in weights]}"
# )
# except Exception as e:
# print(f"Error loading layer '{layer.name}': {e}")
keras.utils.plot_model(model, show_dtype=True, show_shapes=True)
model.compile(
# loss=RSquaredLoss(),
loss="mse",
optimizer=keras.optimizers.Adam(learning_rate=1e-2),
metrics=[
keras.metrics.MeanAbsoluteError(name="mae"),
keras.metrics.RootMeanSquaredError(name="rmse"),
RSquared(name="r2"),
],
)
checkpoint_callback = ModelCheckpointWithCleanup(
filepath="model_epoch_{epoch:02d}.h5", # 保存路径
monitor="val_loss", # 监控的指标
save_best_only=True, # 仅保存最佳模型
mode="min", # 监控指标的变化方向
max_to_keep=3, # 仅保留最近的3个模型
)
for layer in model.layers:
if layer.name.startswith("message_passing"):
layer.trainable = False
if layer.name.startswith("transformer_encoder_readout"):
layer.trainable = False
# 确保回调知道模型
reduce_lr.set_model(model)
tensorboard_callback.set_model(model)
checkpoint_callback.set_model(model)
# 自定义训练循环参数
epochs = 30
train_loss_metric = tf.keras.metrics.Mean(name="train_loss")
val_loss_metric = tf.keras.metrics.Mean(name="val_loss")
history = {
"epoch": [],
"train_loss": [],
"val_loss": [],
"train_mae": [],
"val_mae": [],
"train_rmse": [],
"val_rmse": [],
"train_r2": [],
"val_r2": [],
}
# 训练循环
# for epoch in range(epochs):
# print(f"Epoch {epoch + 1}/{epochs}")
# # 使用 tqdm 显示训练进度
# train_dataset = tqdm(traindatasets, desc=f"Epoch {epoch + 1}/{epochs} - Training")
# for step, (x_batch_train, y_batch_train) in enumerate(train_dataset):
# with tf.GradientTape() as tape:
# logits = model(x_batch_train, training=True)
# loss_value = model.compiled_loss(y_batch_train, logits)
# grads = tape.gradient(loss_value, model.trainable_weights)
# model.optimizer.apply_gradients(zip(grads, model.trainable_weights))
# # 更新训练指标
# train_loss_metric.update_state(loss_value)
# model.compiled_metrics.update_state(y_batch_train, logits)
# train_dataset.set_postfix({"Training loss": train_loss_metric.result().numpy()})
# if epoch > 20:
# print("**************")
# print(y_batch_train)
# print("**************")
# print(logits)
# train_loss = train_loss_metric.result()
# train_metrics = {metric.name: metric.result().numpy() for metric in model.metrics}
# train_loss_metric.reset_states()
# for metric in model.metrics:
# metric.reset_states()
# print(f"Training loss over epoch: {train_loss:.4f}")
# for name, result in train_metrics.items():
# print(f"Training {name} over epoch: {result:.4f}")
# val_dataset = tqdm(validdatasets, desc=f"Epoch {epoch + 1}/{epochs} - Validation")
# for step, (x_batch_val, y_batch_val) in enumerate(val_dataset):
# val_logits = model(x_batch_val, training=False)
# val_loss_value = model.compiled_loss(y_batch_val, val_logits)
# val_loss_metric.update_state(val_loss_value)
# model.compiled_metrics.update_state(y_batch_val, val_logits)
# val_dataset.set_postfix({"Validation loss": val_loss_metric.result().numpy()})
# val_loss = val_loss_metric.result()
# val_metrics = {metric.name: metric.result().numpy() for metric in model.metrics}
# val_loss_metric.reset_states()
# for metric in model.metrics:
# metric.reset_states()
# print(f"Validation loss: {val_loss:.4f}")
# for name, result in val_metrics.items():
# print(f"Validation {name}: {result:.4f}")
# # 调用回调函数
# logs = {
# "val_loss": val_loss.numpy(),
# **{f"val_{name}": result for name, result in val_metrics.items()},
# "lr": model.optimizer.lr.numpy(),
# }
# reduce_lr.on_epoch_end(epoch, logs)
# tensorboard_callback.on_epoch_end(epoch, logs)
# checkpoint_callback.on_epoch_end(epoch, logs)
# # 记录每个epoch的损失值
# history["epoch"].append(epoch + 1)
# history["train_loss"].append(train_loss.numpy())
# history["val_loss"].append(val_loss.numpy())
# history["train_mae"].append(train_metrics["mae"])
# history["val_mae"].append(val_metrics["mae"])
# history["train_rmse"].append(train_metrics["rmse"])
# history["val_rmse"].append(val_metrics["rmse"])
# history["train_r2"].append(train_metrics["r2"])
# history["val_r2"].append(val_metrics["r2"])
# model.save("./savemodel/model_epoch_{}".format(epoch), save_format="tf")
# if epoch + 1 == 8:
# for layer in model.layers:
# layer.trainable = True
# model.compile(
# # loss=RSquaredLoss(),
# loss="mse",
# optimizer=keras.optimizers.Adam(learning_rate=1.5e-4),
# metrics=[
# keras.metrics.MeanAbsoluteError(name="mae"),
# keras.metrics.RootMeanSquaredError(name="rmse"),
# RSquared(name="r2"),
# ],
# )
# # 使用 TensorFlow Summary API 保存指标日志到 CSV 文件
# history_df = pd.DataFrame(history)
# history_df.to_csv("training_history.csv", index=False)
# print(f"Training history saved to training_history.csv")
import numpy as np
import matplotlib.pyplot as plt
from keras.models import load_model
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
# Load the trained model
model.load_weights("beat.h5", by_name=True)
val_dataset = tqdm(validdatasets)
y_test = []
y_pred = []
for step, (x_batch_val, y_batch_val) in enumerate(val_dataset):
val_logits = model(x_batch_val)
y_test.append(y_batch_val.numpy())
y_pred.append(val_logits.numpy())
y_test = np.concatenate(y_test, axis=0)
y_pred = np.concatenate(y_pred, axis=0)
# Calculate metrics
mae = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
r2 = r2_score(y_test, y_pred)
# Create the plot
plt.figure(figsize=(8, 6))
plt.scatter(y_test, y_pred, color="#EEC186", alpha=0.8, label="Predicted")
plt.scatter(y_pred, y_test, color="#B8E5FA", alpha=0.8, label="Experimental")
plt.plot(
[min(y_test), max(y_test)], [min(y_test), max(y_test)], color="black", linewidth=2
)
plt.xlabel("Experimental value")
plt.ylabel("Predicted value")
plt.title("Experimental vs. Predicted values")
plt.text(
1.05,
2.8,
f"$R^2$={r2:.4f}\nMAE={mae:.4f}\nRMSE={rmse:.4f}",
fontsize=10,
bbox=dict(facecolor="white", alpha=0.5),
)
plt.legend()
plt.show()