pip install scikit-learn keras_tuner numpy==1.19.5
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/, https://mirrors.aliyun.com/pypi/simple/
Collecting scikit-learn
Downloading https://mirrors.aliyun.com/pypi/packages/57/aa/483fbe6b5314bce2d49801e6cec1f2139a9c220d0d51494788fff47233b3/scikit_learn-1.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (26.4 MB)
[K |████████████████████████████████| 26.4 MB 275 kB/s eta 0:00:01 |██████████████████████████████▌ | 25.2 MB 27.5 MB/s eta 0:00:01
[?25hCollecting keras_tuner
Downloading https://mirrors.aliyun.com/pypi/packages/c7/d3/506771d6114853c270103b3348f992086517c380dbefad60c8a42ae165c3/keras_tuner-1.1.2-py3-none-any.whl (133 kB)
[K |████████████████████████████████| 133 kB 36.8 MB/s eta 0:00:01
[?25hCollecting numpy==1.19.5
Downloading https://mirrors.aliyun.com/pypi/packages/f7/50/02c0fb5d913901ad643934713a31567df9b5065c28efc9672b707f80dfb1/numpy-1.19.5-cp39-cp39-manylinux2010_x86_64.whl (14.9 MB)
[K |████████████████████████████████| 14.9 MB 39.8 MB/s eta 0:00:01
[?25hCollecting joblib>=0.11
Downloading https://mirrors.aliyun.com/pypi/packages/3e/d5/0163eb0cfa0b673aa4fe1cd3ea9d8a81ea0f32e50807b0c295871e4aab2e/joblib-1.1.0-py2.py3-none-any.whl (306 kB)
[K |████████████████████████████████| 306 kB 75.8 MB/s eta 0:00:01
[?25hCollecting threadpoolctl>=2.0.0
Downloading https://mirrors.aliyun.com/pypi/packages/61/cf/6e354304bcb9c6413c4e02a747b600061c21d38ba51e7e544ac7bc66aecc/threadpoolctl-3.1.0-py3-none-any.whl (14 kB)
Requirement already satisfied: scipy>=1.1.0 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from scikit-learn) (1.7.1)
Requirement already satisfied: tensorboard in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from keras_tuner) (2.4.0)
Collecting kt-legacy
Downloading https://mirrors.aliyun.com/pypi/packages/09/83/7c3001c7826cd7194f36dc971c57a25c7a55373e7087c62a6d1d1193c022/kt_legacy-1.0.4-py3-none-any.whl (9.6 kB)
Requirement already satisfied: ipython in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from keras_tuner) (7.27.0)
Requirement already satisfied: requests in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from keras_tuner) (2.26.0)
Collecting packaging
Downloading https://mirrors.aliyun.com/pypi/packages/05/8e/8de486cbd03baba4deef4142bd643a3e7bbe954a784dc1bb17142572d127/packaging-21.3-py3-none-any.whl (40 kB)
[K |████████████████████████████████| 40 kB 1.2 MB/s eta 0:00:01
[?25hRequirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from ipython->keras_tuner) (3.0.17)
Requirement already satisfied: matplotlib-inline in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from ipython->keras_tuner) (0.1.2)
Requirement already satisfied: pygments in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from ipython->keras_tuner) (2.10.0)
Requirement already satisfied: traitlets>=4.2 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from ipython->keras_tuner) (5.0.5)
Requirement already satisfied: jedi>=0.16 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from ipython->keras_tuner) (0.18.0)
Requirement already satisfied: pickleshare in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from ipython->keras_tuner) (0.7.5)
Requirement already satisfied: pexpect>4.3 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from ipython->keras_tuner) (4.8.0)
Requirement already satisfied: setuptools>=18.5 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from ipython->keras_tuner) (58.0.4)
Requirement already satisfied: decorator in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from ipython->keras_tuner) (5.0.9)
Requirement already satisfied: backcall in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from ipython->keras_tuner) (0.2.0)
Requirement already satisfied: parso<0.9.0,>=0.8.0 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from jedi>=0.16->ipython->keras_tuner) (0.8.2)
Requirement already satisfied: ptyprocess>=0.5 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from pexpect>4.3->ipython->keras_tuner) (0.7.0)
Requirement already satisfied: wcwidth in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython->keras_tuner) (0.2.5)
Requirement already satisfied: ipython-genutils in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from traitlets>=4.2->ipython->keras_tuner) (0.2.0)
Collecting pyparsing!=3.0.5,>=2.0.2
Downloading https://mirrors.aliyun.com/pypi/packages/d9/41/d9cfb4410589805cd787f8a82cddd13142d9bf7449d12adf2d05a4a7d633/pyparsing-3.0.8-py3-none-any.whl (98 kB)
[K |████████████████████████████████| 98 kB 1.2 MB/s eta 0:00:01
[?25hRequirement already satisfied: charset-normalizer~=2.0.0 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from requests->keras_tuner) (2.0.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from requests->keras_tuner) (1.26.6)
Requirement already satisfied: idna<4,>=2.5 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from requests->keras_tuner) (3.2)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from requests->keras_tuner) (2021.5.30)
Requirement already satisfied: protobuf>=3.6.0 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from tensorboard->keras_tuner) (3.17.2)
Requirement already satisfied: grpcio>=1.24.3 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from tensorboard->keras_tuner) (1.36.1)
Requirement already satisfied: google-auth<2,>=1.6.3 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from tensorboard->keras_tuner) (1.33.0)
Requirement already satisfied: absl-py>=0.4 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from tensorboard->keras_tuner) (0.13.0)
Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from tensorboard->keras_tuner) (1.6.0)
Requirement already satisfied: wheel>=0.26 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from tensorboard->keras_tuner) (0.37.0)
Requirement already satisfied: six>=1.10.0 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from tensorboard->keras_tuner) (1.16.0)
Requirement already satisfied: werkzeug>=0.11.15 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from tensorboard->keras_tuner) (2.0.1)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from tensorboard->keras_tuner) (0.4.4)
Requirement already satisfied: markdown>=2.6.8 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from tensorboard->keras_tuner) (3.3.4)
Requirement already satisfied: cachetools<5.0,>=2.0.0 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from google-auth<2,>=1.6.3->tensorboard->keras_tuner) (4.2.2)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from google-auth<2,>=1.6.3->tensorboard->keras_tuner) (0.2.8)
Requirement already satisfied: rsa<5,>=3.1.4 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from google-auth<2,>=1.6.3->tensorboard->keras_tuner) (4.7.2)
Requirement already satisfied: requests-oauthlib>=0.7.0 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard->keras_tuner) (1.3.0)
Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard->keras_tuner) (0.4.8)
Requirement already satisfied: oauthlib>=3.0.0 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard->keras_tuner) (3.1.1)
Installing collected packages: pyparsing, numpy, threadpoolctl, packaging, kt-legacy, joblib, scikit-learn, keras-tuner
[33m WARNING: The scripts f2py, f2py3 and f2py3.9 are installed in '/root/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.[0m
[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensorflow 2.4.1 requires flatbuffers~=1.12.0, but you have flatbuffers 20210226132247 which is incompatible.
tensorflow 2.4.1 requires gast==0.3.3, but you have gast 0.4.0 which is incompatible.
tensorflow 2.4.1 requires grpcio~=1.32.0, but you have grpcio 1.36.1 which is incompatible.
tensorflow 2.4.1 requires six~=1.15.0, but you have six 1.16.0 which is incompatible.
tensorflow 2.4.1 requires tensorflow-estimator<2.5.0,>=2.4.0, but you have tensorflow-estimator 2.6.0 which is incompatible.
tensorflow 2.4.1 requires typing-extensions~=3.7.4, but you have typing-extensions 3.10.0.2 which is incompatible.[0m
Successfully installed joblib-1.1.0 keras-tuner-1.1.2 kt-legacy-1.0.4 numpy-1.19.5 packaging-21.3 pyparsing-3.0.8 scikit-learn-1.0.2 threadpoolctl-3.1.0
[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv[0m
Note: you may need to restart the kernel to use updated packages.
import numpy as np
import os
os.environ['TF_XLA_FLAGS'] = '--tf_xla_auto_jit=fusible' # 启用XLA Lite
import tensorflow as tf
from sklearn.model_selection import train_test_split
from tensorflow.keras.layers import Embedding,Conv1D,Flatten,Dense,Bidirectional,MaxPool1D,Dropout,LSTM
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
2022-05-04 15:32:02.085942: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.10.1
2022-05-04 15:32:08.580984: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2022-05-04 15:32:08.583349: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcuda.so.1
2022-05-04 15:32:09.592272: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:
pciBusID: 0000:b1:00.0 name: Tesla V100S-PCIE-32GB computeCapability: 7.0
coreClock: 1.597GHz coreCount: 80 deviceMemorySize: 31.75GiB deviceMemoryBandwidth: 1.03TiB/s
2022-05-04 15:32:09.592410: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.10.1
2022-05-04 15:32:09.597166: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcublas.so.10
2022-05-04 15:32:09.597335: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcublasLt.so.10
2022-05-04 15:32:09.601692: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcufft.so.10
2022-05-04 15:32:09.602446: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcurand.so.10
2022-05-04 15:32:09.606239: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcusolver.so.10
2022-05-04 15:32:09.608142: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcusparse.so.10
2022-05-04 15:32:09.614886: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudnn.so.7
2022-05-04 15:32:09.936465: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
path='./iedb_linear_epitopes_all.fasta'
Positive=[]
Negative=[]
Label = []
Sequences = []
with open(path,'r') as f:
while(True):
id=f.readline()
if 'Positive' in id:
Sequences.append(f.readline().strip('\n').upper())
Label.append(0)
elif 'Negative' in id:
Sequences.append(f.readline().strip('\n').upper())
Label.append(1)
if not id:
break
import random
#取相应长度,作为切段
def Length_transform(Sequences,Length):
seq_outcome =[]
for seq in Sequences:
if len(seq) > Length:
random_index = random.randint(0, len(seq)-Length)
seq = seq[random_index:Length+random_index]
elif len(seq) < Length:
seq.extend([0 for i in range(Length-len(seq))])
seq_outcome.append(seq)
return np.array(seq_outcome)
def ngram_change(Sequences_one):
cut_sequences = []
a = 0
while a < len(Sequences_one):
if Sequences_one[a:a+4] in dictionary_dict:
cut_sequences.append(dictionary_dict[Sequences_one[a:a+4]])
a+=4
elif Sequences_one[a:a+3] in dictionary_dict:
cut_sequences.append(dictionary_dict[Sequences_one[a:a+3]])
a+=3
else:
cut_sequences.append(dictionary_dict[Sequences_one[a]])
a+=1
return cut_sequences
def gram_change(Sequences_one):
cut_sequences = []
a = 0
while a < len(Sequences_one):
cut_sequences.append(dictionary_base[Sequences_one[a]])
a+=1
return cut_sequences
tri_four_vocab = open("tri_four_vocab.txt","r").readlines()
dictionary = []
for tri_four_one in tri_four_vocab:
tri_four_one = tri_four_one.strip()
dictionary.append(tri_four_one.upper())
base='XACDEFGHIKLMNPQRSTVWY'
Protein=[a for a in base]
dictionary_base = {x:y for y,x in enumerate(Protein)}
dictionary.extend(Protein)
dictionary_dict={x:y for y,x in enumerate(dictionary)}
cut_Sequences = []
for Sequences_one in Sequences:
a = ngram_change(Sequences_one)
a.extend(gram_change(Sequences_one))
cut_Sequences.append(a)
cut_Sequences_length = []
for i in cut_Sequences:
cut_Sequences_length.append(len(i))
cut_Sequences_length.sort()
cut_int = cut_Sequences_length[int(len(cut_Sequences_length) * 0.8)]
print(cut_int)
# cut_int = cut_Sequences_length[int(len(cut_Sequences_length) * 0.9)]
# print(cut_int)
# cut_int = cut_Sequences_length[int(len(cut_Sequences_length) * 0.95)]
# print(cut_int)
cut_Sequences = np.array(cut_Sequences)
Label = np.array(Label)
1505
/tmp/ipykernel_4296/1447012666.py:11: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
cut_Sequences = np.array(cut_Sequences)
Sequences_ave = Length_transform(cut_Sequences,Length=cut_int)
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.iter
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.beta_1
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.beta_2
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.decay
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.learning_rate
WARNING:tensorflow:A checkpoint was restored (e.g. tf.train.Checkpoint.restore or tf.keras.Model.load_weights) but not all checkpointed values were used. See above for specific issues. Use expect_partial() on the load status object, e.g. tf.train.Checkpoint.restore(...).expect_partial(), to silence these warnings, or use assert_consumed() to make the check explicit. See https://www.tensorflow.org/guide/checkpoint#loading_mechanics for details.
# #数值转换
# base='XACDEFGHIKLMNPQRSTVWY'
# Protein=[a for a in base]
# Protein_dict={x:y for y,x in enumerate(Protein)}
# def transform_encode(sequences):
# output=[]
# for seq in sequences:
# tmp=[]
# tmp=[Protein_dict[aa] for aa in seq]
# output.append(tmp)
# return np.array(output)
# Sequences_ave_shuzhi = transform_encode(Sequences_ave)
x_train,x_test,y_train,y_test = train_test_split(Sequences_ave,
Label,train_size=0.7,
random_state=0)
x_train.shape
(21389, 1505)
x_test.shape
(9167, 1505)
def build_model(hp):
# 模型搭建
x = tf.keras.Input(shape=(cut_int))
embedding = Embedding(input_dim=len(dictionary_dict),input_length=cut_int,output_dim=hp.Int('units',
min_value=64,
max_value=1024,
step=32))(x) #序列转向量
b_lstm = Bidirectional(LSTM(hp.Int('units',
min_value=16,
max_value=128,
step=16),activation="tanh",return_sequences=True))(embedding) #双向LSTM
flatten_lstm = Flatten()(b_lstm)
#第一个尺度的CNN
conv_1 = Conv1D(filters=hp.Int('Conv1D_units_1',
min_value=16,
max_value=128,
step=16),kernel_size=hp.Int('Conv1D_kernel_size_1',
min_value=1,
max_value=10,
step=1),strides=1,activation='relu')(embedding)
pool_1 = MaxPool1D(pool_size=hp.Int('MaxPool1D_units_1',
min_value=1,
max_value=8,
step=1),strides=2)(conv_1)
flatten_conv_1 = Flatten()(pool_1)
#第二个尺度的CNN
conv_2 = Conv1D(filters=hp.Int('Conv1D_units_2',
min_value=16,
max_value=128,
step=16),kernel_size=hp.Int('Conv1D_kernel_size_2',
min_value=1,
max_value=10,
step=1),strides=1,activation='relu')(embedding)
pool_2 = MaxPool1D(pool_size=hp.Int('MaxPool1D_units_2',
min_value=1,
max_value=8,
step=1),strides=2)(conv_2)
flatten_conv_2 = Flatten()(pool_2)
#第三个尺度的CNN
conv_3 = Conv1D(filters=hp.Int('Conv1D_units_3',
min_value=16,
max_value=128,
step=16),kernel_size=hp.Int('Conv1D_kernel_size_3',
min_value=2,
max_value=10,
step=2),strides=1,activation='relu')(embedding)
pool_3 = MaxPool1D(pool_size=hp.Int('MaxPool1D_units_3',
min_value=4,
max_value=32,
step=2),strides=2)(conv_3)
flatten_conv_3 = Flatten()(pool_3)
concate = tf.concat([flatten_lstm,flatten_conv_1,flatten_conv_2,flatten_conv_3],axis=1) #BiLSTM与多尺度CNN并行
dense_1 = Dense(64,activation='relu')(concate)
dropout = hp.Boolean("dropout")
if dropout:
drop = Dropout(rate=hp.Float("dropout-rate", min_value=0.1, max_value=1, sampling="log"))(dense_1)
dense_2 = Dense(9,activation='relu')(drop)
else:
dense_2 = Dense(9,activation='relu')(dense_1)
dense_3 = Dense(1,activation='sigmoid')(dense_2)
y = dense_3
model = tf.keras.Model(inputs=x,outputs=y)
model.compile(optimizer='Adam',
loss='BinaryCrossentropy',
metrics=['accuracy'])
return model
import keras_tuner as kt
#kerasTuner超参数优化
tuner = kt.RandomSearch(
build_model,
objective='val_loss',
max_trials=9,
executions_per_trial=9,
overwrite=False,
project_name='kt_0.9_scala')
INFO:tensorflow:Reloading Oracle from existing project ./kt_0.9_scala/oracle.json
INFO:tensorflow:Reloading Tuner from ./kt_0.9_scala/tuner0.json
from tensorflow import keras
class Evaluator(keras.callbacks.Callback):
"""评估与保存
"""
def on_epoch_end(self, epoch, logs=None):
predicted = best_model.predict(x_test) #预测
pred = np.squeeze(predicted,axis=-1)
f = pred>0.5
pred[f]=1
pred[pred<0.6]=0
sn_sp_acc_mcc1 = sn_sp_acc_mcc(y_test,pred,pos_label=1)
print(sn_sp_acc_mcc1)
from sklearn.metrics import roc_curve,auc
FPR,TPR,threshold=roc_curve(y_test,best_model.predict(x_test),pos_label=1)
#AUC值计算
AUC=auc(FPR,TPR)
print(AUC)
tuner.search_space_summary()
callback = tf.keras.callbacks.EarlyStopping(
monitor='val_loss', min_delta=0, patience=5, verbose=5,
mode='auto', baseline=None, restore_best_weights=False
)
evaluator = Evaluator()
# tuner.search(x_train,y_train,batch_size=32, epochs=30,validation_data=(x_test, y_test),callbacks=[callback,evaluator])
tuner.results_summary()
best_model = tuner.get_best_models(num_models=1)[0]
Search space summary
Default search space size: 12
units (Int)
{'default': None, 'conditions': [], 'min_value': 64, 'max_value': 1024, 'step': 32, 'sampling': None}
Conv1D_units_1 (Int)
{'default': None, 'conditions': [], 'min_value': 16, 'max_value': 128, 'step': 16, 'sampling': None}
Conv1D_kernel_size_1 (Int)
{'default': None, 'conditions': [], 'min_value': 1, 'max_value': 10, 'step': 1, 'sampling': None}
MaxPool1D_units_1 (Int)
{'default': None, 'conditions': [], 'min_value': 1, 'max_value': 8, 'step': 1, 'sampling': None}
Conv1D_units_2 (Int)
{'default': None, 'conditions': [], 'min_value': 16, 'max_value': 128, 'step': 16, 'sampling': None}
Conv1D_kernel_size_2 (Int)
{'default': None, 'conditions': [], 'min_value': 1, 'max_value': 10, 'step': 1, 'sampling': None}
MaxPool1D_units_2 (Int)
{'default': None, 'conditions': [], 'min_value': 1, 'max_value': 8, 'step': 1, 'sampling': None}
Conv1D_units_3 (Int)
{'default': None, 'conditions': [], 'min_value': 16, 'max_value': 128, 'step': 16, 'sampling': None}
Conv1D_kernel_size_3 (Int)
{'default': None, 'conditions': [], 'min_value': 2, 'max_value': 10, 'step': 2, 'sampling': None}
MaxPool1D_units_3 (Int)
{'default': None, 'conditions': [], 'min_value': 4, 'max_value': 32, 'step': 2, 'sampling': None}
dropout (Boolean)
{'default': False, 'conditions': []}
dropout-rate (Float)
{'default': 0.1, 'conditions': [], 'min_value': 0.1, 'max_value': 1.0, 'step': None, 'sampling': 'log'}
Results summary
Results in ./kt_0.9_scala
Showing 10 best trials
<keras_tuner.engine.objective.Objective object at 0x7f39fc1d1970>
Trial summary
Hyperparameters:
units: 320
Conv1D_units_1: 64
Conv1D_kernel_size_1: 5
MaxPool1D_units_1: 7
Conv1D_units_2: 80
Conv1D_kernel_size_2: 7
MaxPool1D_units_2: 2
Conv1D_units_3: 48
Conv1D_kernel_size_3: 4
MaxPool1D_units_3: 10
dropout: True
dropout-rate: 0.1
Score: 0.38298845622274613
Trial summary
Hyperparameters:
units: 256
Conv1D_units_1: 16
Conv1D_kernel_size_1: 5
MaxPool1D_units_1: 6
Conv1D_units_2: 112
Conv1D_kernel_size_2: 6
MaxPool1D_units_2: 4
Conv1D_units_3: 64
Conv1D_kernel_size_3: 4
MaxPool1D_units_3: 30
dropout: False
Score: 0.44944845967822605
Trial summary
Hyperparameters:
units: 800
Conv1D_units_1: 16
Conv1D_kernel_size_1: 5
MaxPool1D_units_1: 4
Conv1D_units_2: 96
Conv1D_kernel_size_2: 5
MaxPool1D_units_2: 3
Conv1D_units_3: 128
Conv1D_kernel_size_3: 6
MaxPool1D_units_3: 30
dropout: False
dropout-rate: 0.5470399251904895
Score: 0.5080929464764066
# best_model.fit(x_train,y_train,epochs=10)
best_model.summary()
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 1505)] 0
__________________________________________________________________________________________________
embedding (Embedding) (None, 1505, 320) 648320 input_1[0][0]
__________________________________________________________________________________________________
conv1d (Conv1D) (None, 1501, 64) 102464 embedding[0][0]
__________________________________________________________________________________________________
conv1d_1 (Conv1D) (None, 1499, 80) 179280 embedding[0][0]
__________________________________________________________________________________________________
conv1d_2 (Conv1D) (None, 1502, 48) 61488 embedding[0][0]
__________________________________________________________________________________________________
bidirectional (Bidirectional) (None, 1505, 640) 1640960 embedding[0][0]
__________________________________________________________________________________________________
max_pooling1d (MaxPooling1D) (None, 748, 64) 0 conv1d[0][0]
__________________________________________________________________________________________________
max_pooling1d_1 (MaxPooling1D) (None, 749, 80) 0 conv1d_1[0][0]
__________________________________________________________________________________________________
max_pooling1d_2 (MaxPooling1D) (None, 747, 48) 0 conv1d_2[0][0]
__________________________________________________________________________________________________
flatten (Flatten) (None, 963200) 0 bidirectional[0][0]
__________________________________________________________________________________________________
flatten_1 (Flatten) (None, 47872) 0 max_pooling1d[0][0]
__________________________________________________________________________________________________
flatten_2 (Flatten) (None, 59920) 0 max_pooling1d_1[0][0]
__________________________________________________________________________________________________
flatten_3 (Flatten) (None, 35856) 0 max_pooling1d_2[0][0]
__________________________________________________________________________________________________
tf.concat (TFOpLambda) (None, 1106848) 0 flatten[0][0]
flatten_1[0][0]
flatten_2[0][0]
flatten_3[0][0]
__________________________________________________________________________________________________
dense (Dense) (None, 64) 70838336 tf.concat[0][0]
__________________________________________________________________________________________________
dropout (Dropout) (None, 64) 0 dense[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (None, 9) 585 dropout[0][0]
__________________________________________________________________________________________________
dense_2 (Dense) (None, 1) 10 dense_1[0][0]
==================================================================================================
Total params: 73,471,443
Trainable params: 73,471,443
Non-trainable params: 0
__________________________________________________________________________________________________
# best_model.save("best_model_80%长度验证(895)", save_format="tf")
独立测试
#定义SN,SP,ACC,MCC
def sn_sp_acc_mcc(true_label,predict_label,pos_label=1):
import math
pos_num = np.sum(true_label==pos_label)
neg_num = true_label.shape[0]-pos_num
tp =np.sum((true_label==pos_label) & (predict_label==pos_label))
tn = np.sum(true_label==predict_label)-tp
sn = tp/pos_num
sp = tn/neg_num
acc = (tp+tn)/(pos_num+neg_num)
fn = pos_num - tp
fp = neg_num - tn
tp = np.array(tp,dtype=np.float64)
tn = np.array(tn,dtype=np.float64)
fp = np.array(fp,dtype=np.float64)
fn = np.array(fn,dtype=np.float64)
mcc = (tp*tn-fp*fn)/(np.sqrt((tp+fn)*(tp+fp)*(tn+fp)*(tn+fn)))
return sn,sp,acc,mcc
import matplotlib.pyplot as plt
for best_model in tuner.get_best_models(num_models=6):
predicted = best_model.predict(x_test) #预测
pred = np.squeeze(predicted,axis=-1)
f = pred>0.5
pred[f]=1
pred[pred<0.6]=0
sn_sp_acc_mcc1 = sn_sp_acc_mcc(y_test,pred,pos_label=1)
print(sn_sp_acc_mcc1)
from sklearn.metrics import roc_curve,auc
FPR,TPR,threshold=roc_curve(y_test,best_model.predict(x_test),pos_label=1)
#AUC值计算
AUC=auc(FPR,TPR)
print(AUC)
#ROC曲线绘制
plt.figure(figsize=(5,5))
plt.title('ROC curves')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.xlim([-0.05,1.05])
plt.ylim([-0.05,1.05])
plt.plot(FPR,TPR,color='r',label=' (AUC={:.4f})'.format(AUC))
plt.plot([0, 1], [0, 1], color='m', linestyle='--')
plt.legend(loc='lower right')
plt.show()
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.iter
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.beta_1
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.beta_2
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.decay
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.learning_rate
WARNING:tensorflow:A checkpoint was restored (e.g. tf.train.Checkpoint.restore or tf.keras.Model.load_weights) but not all checkpointed values were used. See above for specific issues. Use expect_partial() on the load status object, e.g. tf.train.Checkpoint.restore(...).expect_partial(), to silence these warnings, or use assert_consumed() to make the check explicit. See https://www.tensorflow.org/guide/checkpoint#loading_mechanics for details.
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.iter
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.beta_1
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.beta_2
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.decay
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.learning_rate
WARNING:tensorflow:A checkpoint was restored (e.g. tf.train.Checkpoint.restore or tf.keras.Model.load_weights) but not all checkpointed values were used. See above for specific issues. Use expect_partial() on the load status object, e.g. tf.train.Checkpoint.restore(...).expect_partial(), to silence these warnings, or use assert_consumed() to make the check explicit. See https://www.tensorflow.org/guide/checkpoint#loading_mechanics for details.
(0.8889084507042253, 0.7720103240607973, 0.8444420202901712, 0.6674641683983054)
0.9074599770980343
(0.8839788732394366, 0.7502150845999427, 0.8330969782916985, 0.6425178838782678)
0.9089323725548012
(0.8644366197183099, 0.8124462288500144, 0.8446601941747572, 0.672803062342601)
0.9080733216332696
pip install matplotlib
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/, https://mirrors.aliyun.com/pypi/simple/
Collecting matplotlib
Downloading https://mirrors.aliyun.com/pypi/packages/6a/52/703f568256a3e614a448503a698557d7832b7893fd63d3f7c2ebb54cd6e2/matplotlib-3.5.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (11.2 MB)
[K |████████████████████████████████| 11.2 MB 823 kB/s eta 0:00:01
[?25hRequirement already satisfied: python-dateutil>=2.7 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from matplotlib) (2.8.2)
Collecting kiwisolver>=1.0.1
Downloading https://mirrors.aliyun.com/pypi/packages/f6/13/2a187e2280251f5c035da46e1706d4c8bd6ccc9f34e88c298cffbc5ba793/kiwisolver-1.4.2-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.6 MB)
[K |████████████████████████████████| 1.6 MB 824 kB/s eta 0:00:01 |███████████▍ | 573 kB 919 kB/s eta 0:00:02
[?25hCollecting cycler>=0.10
Downloading https://mirrors.aliyun.com/pypi/packages/5c/f9/695d6bedebd747e5eb0fe8fad57b72fdf25411273a39791cde838d5a8f51/cycler-0.11.0-py3-none-any.whl (6.4 kB)
Requirement already satisfied: packaging>=20.0 in ./.local/lib/python3.9/site-packages (from matplotlib) (21.3)
Collecting fonttools>=4.22.0
Downloading https://mirrors.aliyun.com/pypi/packages/2f/85/2f6e42fb4b537b9998835410578fb1973175b81691e9a82ab6668cf64b0b/fonttools-4.33.3-py3-none-any.whl (930 kB)
[K |████████████████████████████████| 930 kB 987 kB/s eta 0:00:01 |██████████████████████████▊ | 778 kB 987 kB/s eta 0:00:01 |███████████████████████████ | 788 kB 987 kB/s eta 0:00:01
[?25hCollecting pillow>=6.2.0
Downloading https://mirrors.aliyun.com/pypi/packages/15/37/45ad6041473ebb803d0bb265cf7e749c4838dc48c3335a03e63d6aad07d8/Pillow-9.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB)
[K |████████████████████████████████| 4.3 MB 964 kB/s eta 0:00:01
[?25hRequirement already satisfied: pyparsing>=2.2.1 in ./.local/lib/python3.9/site-packages (from matplotlib) (3.0.8)
Requirement already satisfied: numpy>=1.17 in ./.local/lib/python3.9/site-packages (from matplotlib) (1.19.5)
Requirement already satisfied: six>=1.5 in /opt/conda/envs/tf2.4.1/lib/python3.9/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
Installing collected packages: pillow, kiwisolver, fonttools, cycler, matplotlib
[33m WARNING: The scripts fonttools, pyftmerge, pyftsubset and ttx are installed in '/root/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.[0m
Successfully installed cycler-0.11.0 fonttools-4.33.3 kiwisolver-1.4.2 matplotlib-3.5.1 pillow-9.1.0
[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv[0m
Note: you may need to restart the kernel to use updated packages.