Skip to content

Commit

Permalink
add gpu examples
Browse files Browse the repository at this point in the history
  • Loading branch information
yzhao062 committed Aug 13, 2020
1 parent 5c00ed5 commit 1bf106f
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@
# extracting all the files
print('Extracting mimic demo files now...')
zip.extractall()
print('Done!')

with ZipFile(os.path.join(data_dir, 'image.zip'), 'r') as zip:
# # printing all the contents of the zip file
# zip.printdir()

# extracting all the files
print('Extracting image files now...')
zip.extractall()
print('Done!')
60 changes: 60 additions & 0 deletions examples/learning_models/example_sequence_cpu_phenotyping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# -*- coding: utf-8 -*-
"""Example of using GRU on MIMIC demo mortality prediction
"""
# License: BSD 2 clause


# environment setting
import os
import sys

sys.path.append(
os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))
root_dir = os.path.abspath(os.path.join(__file__, "../../.."))
os.chdir(root_dir)

sys.path.append(root_dir)

### May choose any of these models
from pyhealth.models.sequence.dipole import Dipole
# from pyhealth.models.sequence.lstm import LSTM as model
# from pyhealth.models.sequence.gru import GRU as GRU
# from pyhealth.models.sequence.embedgru import EmbedGRU as model
# from pyhealth.models.sequence.retain import Retain as model
# from pyhealth.models.sequence.raim import RAIM as model
# from pyhealth.models.sequence.tlstm import tLSTM as model
# from pyhealth.models.sequence.xgboost import XGBoost as model
# from pyhealth.models.sequence.rf import RandomForest as model

from pyhealth.data.expdata_generator import sequencedata as expdata_generator
from pyhealth.evaluation.evaluator import func

if __name__ == "__main__":
# override here to specify where the data locates
# root_dir = ''
# root_dir = os.path.abspath(os.path.join(__file__, "../../.."))
data_dir = os.path.join(root_dir, 'datasets', 'mimic')

expdata_id = '2020.0811.data.phenotyping.test.v2'

# set up the datasets
cur_dataset = expdata_generator(expdata_id, root_dir=root_dir)
cur_dataset.get_exp_data(sel_task='phenotyping', data_root=data_dir)
cur_dataset.load_exp_data()
# cur_dataset.show_data()

# initialize the model for training
expmodel_id = '2020.0811.model.phenotyping.test.v2'
clf = Dipole(expmodel_id=expmodel_id, n_batchsize=20, use_gpu=False,
n_epoch=30)
clf.fit(cur_dataset.train, cur_dataset.valid)

# load the best model for inference
clf.load_model()
clf.inference(cur_dataset.test)
pred_results = clf.get_results()
print(pred_results)

# evaluate the model
r = func(pred_results['hat_y'], pred_results['y'])
print(r)
58 changes: 58 additions & 0 deletions examples/learning_models/example_sequence_gpu_mortality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from __future__ import division
from __future__ import print_function

# environment setting
import os
import sys

sys.path.append(
os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))
root_dir = os.path.abspath(os.path.join(__file__, "../../.."))
os.chdir(root_dir)

sys.path.append(root_dir)

### May choose any of these models
# from pyhealth.models.sequence.dipole import Dipole as model
from pyhealth.models.sequence.lstm import LSTM as model
# from pyhealth.models.sequence.gru import GRU as GRU
# from pyhealth.models.sequence.embedgru import EmbedGRU as model
# from pyhealth.models.sequence.retain import Retain as model
# from pyhealth.models.sequence.raim import RAIM as model
# from pyhealth.models.sequence.tlstm import tLSTM as model
# from pyhealth.models.sequence.xgboost import XGBoost as model
# from pyhealth.models.sequence.rf import RandomForest as model

from pyhealth.data.expdata_generator import sequencedata as expdata_generator
from pyhealth.evaluation.evaluator import func

if __name__ == "__main__":
# override here to specify where the data locates
# root_dir = ''
# root_dir = os.path.abspath(os.path.join(__file__, "../../.."))
data_dir = os.path.join(root_dir, 'datasets', 'cms')

expdata_id = '2020.0810.data.mortality.mimic'

# set up the datasets
cur_dataset = expdata_generator(expdata_id, root_dir=root_dir)
cur_dataset.get_exp_data(sel_task='mortality', data_root=data_dir)
cur_dataset.load_exp_data()
cur_dataset.show_data()

# initialize the model for training
# turn on GPU by setting use_gpu to True
expmodel_id = '2020.0810.gru.data.mortality.mimic.gpu'
clf = model(expmodel_id=expmodel_id, n_batchsize=20, use_gpu=True,
n_epoch=100, gpu_ids='0,1')
clf.fit(cur_dataset.train, cur_dataset.valid)

# load the best model for inference
clf.load_model()
clf.inference(cur_dataset.test)
pred_results = clf.get_results()
print(pred_results['hat_y'])

# evaluate the model
r = func(pred_results['hat_y'], pred_results['y'])
print(r)
49 changes: 0 additions & 49 deletions examples/learning_models/gru_mimic_demo_example.py

This file was deleted.

0 comments on commit 1bf106f

Please sign in to comment.