<jupyter_start><jupyter_text>کد مربوطه به مقاله ارسالی با عنوان: ترکیب روش آسیب مغزی بهینه و منظم‌سازی تُنُک ‌ در کوچک‌سازی یک مدل یادگیری عمیقبه لحاظ ماهیت تصادفی مقداردهی اولیه‌ی وزنهای شبکه‌های عصبی و روال آموزش شبکه، اختلافات جزیی در اجراهای مختلف وجود دارد.<NAME>, m.amintoosi at gmail.com<jupyter_code>import os, time
if hasattr(time, 'tzset'):
    os.environ['TZ'] = 'Asia/Tehran'
    time.tzset()
print(time.strftime('%Y/​%m/%d, %X'))
import torch
torch.manual_seed(1)
import random
random.seed(1)
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
!pip install pycm
!git clone https://github.com/mamintoosi/Reg-OBD-for-VGG-Pruning
%cd Reg-OBD-for-VGG-Pruning
!ls models
%%time
# Transfer Learning: Train the VGG16 model
torch.manual_seed(1)
random.seed(1)
# Download a pre-trained VGG16 model and train it
%run finetune_SpReg_pds.py --train --use-cuda \
  --train_epoch 10 --ds_name 'STL10' \
  --output_model 'vgg'
fig, ax = plt.subplots()
ax.plot(np.arange(1,len(Train_loss)+1), Train_loss, label='Train Loss', color='blue')
ax.plot(np.arange(1,len(Train_loss)+1), Val_loss, label='Valid Loss', color = 'red')
ax.plot(np.argmin(Val_loss)+1, np.min(Val_loss),'g*')
# legend = ax.legend(loc='upper right', fontsize='x-large')
legend = ax.legend(loc='center right', fontsize='x-large')
plt.xlabel('Epoch No.')
plt.ylabel('Loss')
plt.show()
print(np.argmin(Val_loss))
%%time
# Test report the trained model 
torch.manual_seed(1)
random.seed(1)
%run finetune_SpReg_pds.py --test --use-cuda --ds_name 'STL10' \
  --input_model 'vgg' --output_model 'vgg'
!ls -as models
%%time
# Pruning the trained model using HSQGL12 method
torch.manual_seed(1)
random.seed(1)
%run finetune_SpReg_pds.py --prune --use-cuda --train_epoch 20 --reg_name 'HSQGL12' --ds_name 'STL10' \
  --input_model 'vgg' --output_model 'HSQGL12' --landa 1e-5
%%time
# Test report for HSQGL12 method
torch.manual_seed(1)
random.seed(1)
%run finetune_SpReg_pds.py --test --use-cuda --ds_name 'STL10' \
  --input_model 'HSQGL12' --output_model 'HSQGL12'
!ls -as models
# Pruning the trained model using OBD method
%%time
torch.manual_seed(1)
random.seed(1)
%run finetune_SpReg_pds.py --prune --use-cuda --ds_name 'STL10' \
  --input_model 'vgg' --output_model 'OBD' \
  --eat 3
%%time
# Test report for OBD method
torch.manual_seed(1)
random.seed(1)
%run finetune_SpReg_pds.py --test --use-cuda --ds_name 'STL10' \
  --input_model 'OBD' --output_model 'OBD'
!ls -as models
%%time
# Train HSQGL12_OBD
torch.manual_seed(1)
random.seed(1)
%run finetune_SpReg_pds.py --prune --use-cuda --ds_name 'STL10' \
  --input_model 'HSQGL12' --output_model 'HSQGL12_OBD' \
  --eat 3
%%time
# Test report for HSQGL12+OBD method
torch.manual_seed(1)
random.seed(1)
%run finetune_SpReg_pds.py --test --use-cuda --ds_name 'STL10' \
  --input_model 'HSQGL12_OBD' --output_model 'HSQGL12_OBD'
# Print Model Sizes
!ls -as models
# !stat -c '%y' models/vgg_HSQGL12_OBD.pt
# print(time.strftime('%Y/​%m/%d, %X'))
%%time
# Pruning the OBD pruned model using HSQGL12 method
torch.manual_seed(1)
random.seed(1)
%run finetune_SpReg_pds.py --prune --use-cuda --train_epoch 20 --reg_name 'HSQGL12' --ds_name 'STL10' \
  --input_model 'OBD' --output_model 'OBD_HSQGL12' --landa 1e-5
!ls -as models
%%time
# Test report for OBD+HSQGL12 method
torch.manual_seed(1)
random.seed(1)
%run finetune_SpReg_pds.py --test --use-cuda --ds_name 'STL10' \
  --input_model 'OBD_HSQGL12' --output_model 'OBD_HSQGL12'
%matplotlib inline
# نمایش تعداد فیلترهای هرس شده در هر لایه
pkl_file = open('STL10_OBD_dic.pkl','rb')
dic = pkl.load(pkl_file)
dic_obd = bar_filters_pruned(dic)

pkl_file = open('STL10_HSQGL12_OBD_dic.pkl','rb')
dic = pkl.load(pkl_file)
dic_reg_obd = bar_filters_pruned(dic)

d = {
    'OBD': dic_obd,
    'HSQGL12-OBD': dic_reg_obd
}
df = pd.DataFrame(d)
df1 = df.stack().reset_index().rename(columns={'level_1': 'Method', 0: 'Num Filters Pruned'})
df1.rename(columns={"level_0": "Layer No."},inplace=True)
sns.barplot(x=df1['Layer No.'], y='Num Filters Pruned', data=df1, hue='Method')
!ls -as models
models_dir = 'models/'
eps = 1e-4
model = torch.load('models/vgg.pt', map_location=lambda storage, loc: storage)
te, _ = num_parameters(model,eps)
print('Total Parameters:', te)
mdl_list = ['vgg.pt', 'HSQGL12.pt', 'OBD.pt', 'HSQGL12_OBD.pt', 'OBD_HSQGL12.pt']
print('MDL \t\t ne \t\t ze \t\t ze/te \t 1-ze/te ne/te \t 1-ne/te ')
print('MDL \t ne \t ze \t ze/te \t 1-ze/te \t ne/te \t 1-ne/te ')
for mdl in mdl_list:
  model_file_name = models_dir+mdl
  model = torch.load(model_file_name, map_location=lambda storage, loc: storage)
  num_el, ze = num_parameters(model,eps)
  print("{:15s}\t {} \t {} \t {:.3f} \t {:.3f} \t {:.3f} \t {:.3f}".format(\
          mdl,num_el, ze, ze/te, 1-ze/te, num_el/te, 1-num_el/te))
!zip -r STL10.zip ./STL10*.*
files.download('STL10.zip')<jupyter_output>adding: STL10_HSQGL12_cm.pkl (deflated 56%)
  adding: STL10_HSQGL12_OBD_cm.pkl (deflated 55%)
  adding: STL10_HSQGL12_OBD_dic.pkl (deflated 33%)
  adding: STL10_loss.pkl (stored 0%)
  adding: STL10_OBD_cm.pkl (deflated 55%)
  adding: STL10_OBD_dic.pkl (deflated 34%)
  adding: STL10_OBD_HSQGL12_cm.pkl (deflated 56%)
  adding: STL10_vgg_cm.pkl (deflated 59%)
{'fraction_non_alphanumeric': 0.16395326565791993, 'fraction_numerical': 0.03466768818234055, 'mean_word_length': 4.514255543822598, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '114032', 'license': 'MIT', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2398, 'n_words': 500, 'path': 'main_STL10.ipynb', 'repo_name': 'mamintoosi/Reg-OBD-for-VGG-Pruning', 'size': '5227'}