1

I am using this code to train Bert for Turkish language model classification with 2 labels. But when I run the following code:

import numpy as np
import pandas as pd

df = pd.read_excel (r'preparedDataNoId.xlsx')
df = df.sample(frac = 1)

from sklearn.model_selection import train_test_split

train_df, test_df = train_test_split(df, test_size=0.10)

print('train shape: ',train_df.shape)
print('test shape: ',test_df.shape)

train_df["text"]=train_df["text"].apply(lambda r: str(r))
train_df['label']=train_df['label'].astype(int)
from simpletransformers.classification import ClassificationModel

model = ClassificationModel('bert', 'dbmdz/bert-base-turkish-uncased', use_cuda=False,num_labels=2,
                            args={'reprocess_input_data': True, 'overwrite_output_dir': True, 'num_train_epochs': 3, "train_batch_size": 64 , "fp16":False, "output_dir": "bert_model"})

model.train_model(train_df) 

It takes a lot of time, it doesn't stop and the screen keeps showing:

This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:

    if __name__ == '__main__':
        freeze_support()
        ...
jottbe
  • 4,228
  • 1
  • 15
  • 31
matjar trk
  • 77
  • 1
  • 5

1 Answers1

0

As the error suggests you should wrap your code with an if __name__ == '__main__':

So your code would be:

import numpy as np
import pandas as pd

if __name__ == '__main__':
    df = pd.read_excel(r'preparedDataNoId.xlsx')
    df = df.sample(frac=1)

    from sklearn.model_selection import train_test_split

    train_df, test_df = train_test_split(df, test_size=0.10)

    print('train shape: ', train_df.shape)
    print('test shape: ', test_df.shape)

    train_df["text"] = train_df["text"].apply(lambda r: str(r))
    train_df['label'] = train_df['label'].astype(int)
    from simpletransformers.classification import ClassificationModel

    model = ClassificationModel('bert', 'dbmdz/bert-base-turkish-uncased', use_cuda=False, num_labels=2,
                                args={'reprocess_input_data': True, 'overwrite_output_dir': True, 'num_train_epochs': 3,
                                      "train_batch_size": 64, "fp16": False, "output_dir": "bert_model"})

    model.train_model(train_df)

Why is this happening?

On Windows the subprocesses will import (i.e. execute) the main module at start. You need to insert an if __name__ == '__main__': guard in the main module to avoid creating subprocesses recursively.

Quoted from: https://stackoverflow.com/a/18205006/6025629

Mike Xydas
  • 469
  • 5
  • 12