Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.20 KB | None | 0 0
  1. import os, sys
  2. import argparse
  3.  
  4. def mkdir(path):
  5. try:
  6. os.mkdir(path)
  7. except OSError:
  8. print ("Creation of the directory %s failed" % path)
  9. else:
  10. print ("Successfully created the directory %s " % path)
  11.  
  12.  
  13. def start_experiment(exp):
  14. # clean up, repackage code
  15. gpu = 'v100'
  16. cmd = 'rm code.zip; zip code.zip **.py **.sh **.yml **/*.py'
  17. os.system(cmd)
  18.  
  19. exp_files_path = 'experiment-files'
  20. exp_name = '{}_model-{}_embeddings-{}_annotations-{}_mode-{}_humantrain-{}_humantest-{}_synthtrain-{}_synthtest-{}_reshape-{}_batchsize-{}_itersperepoch'.format(exp['model'], exp['embedding_type'], exp['annotations'], exp['mode'], exp['max_train_human'], exp['max_test_human'], exp['max_train_synthetic'], exp['max_test_synthetic'], exp['reshape_type'], exp['batch_size'], exp['iters_per_epoch'])
  21.  
  22. mkdir(exp_files_path)
  23. mkdir('experiment-files/' + exp_name)
  24. mkdir('experiment-files/' + exp_name + "/results/")
  25.  
  26. outpath = '{}-results'.format(exp_name)
  27. pyfile = '{}.py'.format(exp_name)
  28. ymlfile = '{}.yml'.format(exp_name)
  29. outtxt = '{}.txt'.format(exp_name)
  30.  
  31. exp_files_path = exp_files_path + "/" + exp_name
  32.  
  33. # create python file
  34. py = '''\n
  35. import os \n
  36. exp1 = 'python2 reinforcement.py --model {} --embedding_type {} --annotations {} --mode {} --max_train_human {} --max_test_human {} --max_train_synthetic {} --max_test_synthetic {} --bert_reshape_type {} --batch_size {} --iters_per_epoch {} --epochs 200'\n
  37. os.system(exp1)\n'''.format(exp['model'], exp['embedding_type'], exp['annotations'], exp['mode'], exp['max_train_human'], exp['max_test_human'], exp['max_train_synthetic'], exp['max_test_synthetic'], exp['reshape_type'], exp['batch_size'], exp['iters_per_epoch'])
  38.  
  39. with open(pyfile, 'w') as f:
  40. f.write(py)
  41.  
  42.  
  43. sh = '''\n
  44. # >>> conda initialize >>>\n
  45. __conda_setup="$('/opt/conda/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"\n
  46. if [ $? -eq 0 ]; then\n
  47. eval "$__conda_setup"\n
  48. else\n
  49. if [ -f "/opt/conda/etc/profile.d/conda.sh" ]; then\n
  50. . "/opt/conda/etc/profile.d/conda.sh"\n
  51. else\n
  52. export PATH="/opt/conda/bin:$PATH"\n
  53. fi\n
  54. fi\n
  55. unset __conda_setup\n
  56. # <<< conda initialize <<<\n
  57.  
  58. set -ex\n
  59. exec 1>training-log.txt \n
  60. exec 2>training-log-err.txt \n
  61.  
  62. mkdir -p ~/.conda_envs/spr\n
  63. cd ~/.conda_envs\n
  64. cp "${{DATA_DIR}}/spr.tar.gz" .\n
  65. chown gpuuser: spr.tar.gz\n
  66. chmod 600 spr.tar.gz\n
  67. tar xfzo spr.tar.gz -C spr\n
  68. conda activate ~/.conda_envs/spr\n
  69.  
  70.  
  71. # cd /job/model-code/
  72. rm -rf "${{DATA_DIR}}/{}"
  73. mkdir -p "${{DATA_DIR}}/{}"
  74. cp -R /job/model-code "${{DATA_DIR}}/{}/code"
  75. cd "${{DATA_DIR}}/{}/code"
  76. # cp -R /job/model-code "${{RESULT_DIR}}"/code
  77. # cd "${{RESULT_DIR}}"/code
  78.  
  79. export PYTHONUNBUFFERED=1
  80. set +e
  81. bash -lc "$*"
  82. result=$?
  83. echo "${{result}}" > exit_code
  84. exit "${{result}}"
  85. '''.format(exp_name, exp_name, exp_name, exp_name)
  86.  
  87. with open('run.sh', 'w') as f:
  88. f.write(sh)
  89.  
  90. # create yml file
  91. yml ='''\n
  92. model_definition:\n
  93. framework: \n
  94. name: pytorch \n
  95. version: '0.4' \n
  96. runtimes: \n
  97. name: python \n
  98. version: '2.7' \n
  99. name: {} \n
  100. description: This is a sample training-run \n
  101. execution:\n
  102. command: bash run.sh python {} \n
  103. compute_configuration: \n
  104. name: {} \n
  105. training_data_reference: \n
  106. name: training-data-reference_name \n
  107. connection: \n
  108. endpoint_url: https://s3.us-south.objectstorage.softlayer.net \n
  109. access_key_id: 8ffab3dfa4fa44158659c33117043b58 \n
  110. secret_access_key: 876e94a6c02ee334a6a61c126e16e7898c260969d6b59e88 \n
  111. source:\n
  112. bucket: qq-data\n
  113. type: s3\n
  114. training_results_reference:\n
  115. name: training-results-reference_name\n
  116. connection:\n
  117. endpoint_url: https://s3.us-south.objectstorage.softlayer.net \n
  118. access_key_id: 8ffab3dfa4fa44158659c33117043b58 \n
  119. secret_access_key: 876e94a6c02ee334a6a61c126e16e7898c260969d6b59e88 \n
  120. target: \n
  121. bucket: qq-results \n
  122. type: s3 \n
  123. '''.format(exp_name, pyfile, gpu)
  124.  
  125. with open(ymlfile, 'w') as f:
  126. f.write(yml)
  127.  
  128. cmd = 'zip code.zip {} {} {}'.format(pyfile, ymlfile, 'run.sh')
  129. os.system(cmd)
  130.  
  131. # start exp
  132. cmd = 'bx ml train code.zip {}'.format(ymlfile)
  133. os.system(cmd)
  134.  
  135. cmd = 'mv {}.yml {}; mv {}.py {}; mv code.zip {}; mv run.sh {}'.format(exp_name, exp_files_path, exp_name, exp_files_path, exp_files_path, exp_files_path)
  136. print('moving cmd: {}'.format(cmd))
  137. os.system(cmd)
  138.  
  139. sys.exit(0)
  140.  
  141.  
  142.  
  143.  
  144.  
  145. def main():
  146. datasets = {}
  147.  
  148. # dataset specific (num examples per category)
  149. datasets['local-small'] = {'num_train_human': 10, 'num_test_human': 2, 'num_train_synthetic': 0, 'num_test_synthetic': 0}
  150. # datasets['local'] = {'num_train_human': 1600, 'num_test_human': 400, 'num_train_synthetic': 0, 'num_test_synthetic': 0}
  151.  
  152. # datasets['global-small'] = {'num_train_human': 10, 'num_test_human': 2, 'num_train_synthetic': 0, 'num_test_synthetic': 0}
  153. # datasets['global-small'] = {'num_train_human': 1100, 'num_test_human': 200, 'num_train_synthetic': 0, 'num_test_synthetic': 0}
  154.  
  155.  
  156. # model specific (embeddings allowed per model, reshape layers type)
  157. model_configs = {}
  158. model_configs['bert-full'] = {'embedding_type':
  159. [
  160. 'bert',
  161. 'one-hot',
  162. 'random',
  163. 'bert-fixed',
  164. 'bert-word',
  165. 'bert-word-fixed'
  166. ],
  167. 'reshape_type':
  168. [
  169. 'linear',
  170. 'MLP'
  171. ]}
  172.  
  173.  
  174. # model_configs['gpt2'] = model_configs['bert-full']
  175. # model_configs['gpt2-large'] = model_configs['bert-full']
  176. model_configs['lstm'] = {'embedding_type': 'lstm'}
  177.  
  178. # agnostic
  179. batch_size = [1, 4, 8, 32]
  180. iters_per_epoch = ['default', '500', '1000']
  181.  
  182. # TODO + options for learning rate schedule, + support for no-gradient models
  183.  
  184. exp_params = {}
  185. i = 0
  186. for batch in batch_size:
  187. if i > 0:
  188. sys.exit(0)
  189. exp_params['batch_size'] = batch
  190. for iters in iters_per_epoch:
  191. exp_params['iters_per_epoch'] = iters
  192. for mode, info in datasets.items():
  193. if 'local' in mode:
  194. exp_params['mode'] = 'local'
  195. elif 'global' in mode:
  196. exp_params['mode'] = 'global'
  197.  
  198. exp_params['max_train_human'] = int(info['num_train_human'])
  199. exp_params['max_test_human'] = int(info['num_test_human'])
  200. exp_params['max_train_synthetic'] = int(info['num_train_synthetic'])
  201. exp_params['max_test_synthetic'] = int(info['num_test_synthetic'])
  202.  
  203. if info['num_train_synthetic'] == 0 and info['num_test_synthetic']:
  204. exp_params['annotations'] = 'human'
  205. else:
  206. exp_params['annotations'] = 'both'
  207.  
  208. for model, config in model_configs.items():
  209. exp_params['model'] = model
  210. for embed_type in config['embedding_type']:
  211. exp_params['embedding_type'] = embed_type
  212. if 'reshape_type' in config:
  213. for reshape_type in config['reshape_type']:
  214. exp_params['reshape_type'] = reshape_type
  215. start_experiment(exp_params)
  216. else:
  217. start_experiment(exp_params)
  218. i += 1
  219.  
  220. if __name__ == '__main__':
  221. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement