{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# بخش اول) آماده سازی محیط\nدر این بخش کتابخانه های مورد نیاز برنامه معرفی شده‌اند.","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2021-06-18T17:53:15.992020Z","iopub.execute_input":"2021-06-18T17:53:15.992387Z","iopub.status.idle":"2021-06-18T17:53:16.000020Z","shell.execute_reply.started":"2021-06-18T17:53:15.992325Z","shell.execute_reply":"2021-06-18T17:53:15.999234Z"}}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nimport sys\nsys.path.extend(['../input/bert-joint-baseline/']) # add base bert-joint-baseline material to the project\nimport bert_utils\nimport modeling \n\nimport tokenization # for text tokenization\nimport json # for json file usage\n\nimport importlib\nimportlib.reload(bert_utils)","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2021-06-18T18:15:45.605904Z","iopub.execute_input":"2021-06-18T18:15:45.606232Z","iopub.status.idle":"2021-06-18T18:15:47.694710Z","shell.execute_reply.started":"2021-06-18T18:15:45.606182Z","shell.execute_reply":"2021-06-18T18:15:47.693772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"در این بخش محتوای فولدر حاوی فایل‌های ورودی این پروژه نیز نمایش داده شده است.","metadata":{}},{"cell_type":"code","source":"# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n# In this case, we've got some extra BERT model files under `/kaggle/input/bertjointbaseline`\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2021-06-18T18:15:47.697311Z","iopub.execute_input":"2021-06-18T18:15:47.700022Z","iopub.status.idle":"2021-06-18T18:15:47.717682Z","shell.execute_reply.started":"2021-06-18T18:15:47.697550Z","shell.execute_reply":"2021-06-18T18:15:47.716903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# بخش دوم) تنظیمات اولیه\nدر این بخش متغیرهای حاوی آدرس فایل های مورد نیاز این پروژه مقدار دهی شده‌اند ","metadata":{}},{"cell_type":"code","source":"on_kaggle_server = os.path.exists('/kaggle')\nnq_test_file = '../input/tensorflow2-question-answering/simplified-nq-test.jsonl' \npublic_dataset = os.path.getsize(nq_test_file)<20_000_000\nprivate_dataset = os.path.getsize(nq_test_file)>=20_000_000","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:15:47.720606Z","iopub.execute_input":"2021-06-18T18:15:47.723580Z","iopub.status.idle":"2021-06-18T18:15:47.732153Z","shell.execute_reply.started":"2021-06-18T18:15:47.723496Z","shell.execute_reply":"2021-06-18T18:15:47.731334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"تنظیماتی که بر اساس آن مدل برت مقدار دهی می‌شود در این بخش بارگزاری و نمایش داده می‌شود.","metadata":{}},{"cell_type":"code","source":"with open('../input/bert-joint-baseline/bert_config.json','r') as f:\n    config = json.load(f)\nprint(json.dumps(config,indent=4))\n","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:15:47.736345Z","iopub.execute_input":"2021-06-18T18:15:47.738989Z","iopub.status.idle":"2021-06-18T18:15:47.750395Z","shell.execute_reply.started":"2021-06-18T18:15:47.738895Z","shell.execute_reply":"2021-06-18T18:15:47.749537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# بخش سوم) ساخت مدل\nتابع زیر یک لایه دنس می‌سازد","metadata":{}},{"cell_type":"code","source":"class TDense(tf.keras.layers.Layer):\n    def __init__(self,\n                 output_size,\n                 kernel_initializer=None,\n                 bias_initializer=\"zeros\",\n                **kwargs):\n        super().__init__(**kwargs)\n        self.output_size = output_size\n        self.kernel_initializer = kernel_initializer\n        self.bias_initializer = bias_initializer\n    def build(self,input_shape):\n        dtype = tf.as_dtype(self.dtype or tf.keras.backend.floatx())\n        if not (dtype.is_floating or dtype.is_complex):\n          raise TypeError(\"Unable to build `TDense` layer with \"\n                          \"non-floating point (and non-complex) \"\n                          \"dtype %s\" % (dtype,))\n        input_shape = tf.TensorShape(input_shape)\n        if tf.compat.dimension_value(input_shape[-1]) is None:\n          raise ValueError(\"The last dimension of the inputs to \"\n                           \"`TDense` should be defined. \"\n                           \"Found `None`.\")\n        last_dim = tf.compat.dimension_value(input_shape[-1])\n        self.input_spec = tf.keras.layers.InputSpec(min_ndim=2, axes={-1: last_dim})\n        self.kernel = self.add_weight(\n            \"kernel\",\n            shape=[self.output_size,last_dim],\n            initializer=self.kernel_initializer,\n            dtype=self.dtype,\n            trainable=True)\n        self.bias = self.add_weight(\n            \"bias\",\n            shape=[self.output_size],\n            initializer=self.bias_initializer,\n            dtype=self.dtype,\n            trainable=True)\n        super(TDense, self).build(input_shape)\n    def call(self,x):\n        return tf.matmul(x,self.kernel,transpose_b=True)+self.bias\n  ","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:15:47.754646Z","iopub.execute_input":"2021-06-18T18:15:47.755430Z","iopub.status.idle":"2021-06-18T18:15:47.775073Z","shell.execute_reply.started":"2021-06-18T18:15:47.755382Z","shell.execute_reply":"2021-06-18T18:15:47.774093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"تابع زیر یک مدل برت را با استفاده از مقادیر موجود در فایل کانفیگ می‌سازد","metadata":{}},{"cell_type":"code","source":"  \ndef mk_model(config):\n    seq_len = config['max_position_embeddings']\n    unique_id  = tf.keras.Input(shape=(1,),dtype=tf.int64,name='unique_id')\n    input_ids   = tf.keras.Input(shape=(seq_len,),dtype=tf.int32,name='input_ids')\n    input_mask  = tf.keras.Input(shape=(seq_len,),dtype=tf.int32,name='input_mask')\n    segment_ids = tf.keras.Input(shape=(seq_len,),dtype=tf.int32,name='segment_ids')\n    BERT = modeling.BertModel(config=config,name='bert')\n    pooled_output, sequence_output = BERT(input_word_ids=input_ids,\n                                          input_mask=input_mask,\n                                          input_type_ids=segment_ids)\n    \n    logits = TDense(2,name='logits')(sequence_output)\n    start_logits,end_logits = tf.split(logits,axis=-1,num_or_size_splits= 2,name='split')\n    start_logits = tf.squeeze(start_logits,axis=-1,name='start_squeeze')\n    end_logits   = tf.squeeze(end_logits,  axis=-1,name='end_squeeze')\n    \n    ans_type      = TDense(5,name='ans_type')(pooled_output)\n    return tf.keras.Model([input_ for input_ in [unique_id,input_ids,input_mask,segment_ids] \n                           if input_ is not None],\n                          [unique_id,start_logits,end_logits,ans_type],\n                          name='bert-baseline')    ","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:15:47.776570Z","iopub.execute_input":"2021-06-18T18:15:47.777135Z","iopub.status.idle":"2021-06-18T18:15:47.793567Z","shell.execute_reply.started":"2021-06-18T18:15:47.777083Z","shell.execute_reply":"2021-06-18T18:15:47.792699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"مدل برت مورد نیاز با استفاده از فایل کانفیگ بارگزاری شده در مراحل قبل ساخته می‌شود.","metadata":{}},{"cell_type":"code","source":"model= mk_model(config)","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:15:47.794948Z","iopub.execute_input":"2021-06-18T18:15:47.795441Z","iopub.status.idle":"2021-06-18T18:16:00.911005Z","shell.execute_reply.started":"2021-06-18T18:15:47.795398Z","shell.execute_reply":"2021-06-18T18:16:00.910199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"خلاصه مشخصات این مدل برت نمایش داده شده است.","metadata":{}},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:16:00.912427Z","iopub.execute_input":"2021-06-18T18:16:00.912699Z","iopub.status.idle":"2021-06-18T18:16:00.958405Z","shell.execute_reply.started":"2021-06-18T18:16:00.912656Z","shell.execute_reply":"2021-06-18T18:16:00.955902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('../input/unk0201128w/weights')","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:16:00.960051Z","iopub.execute_input":"2021-06-18T18:16:00.960317Z","iopub.status.idle":"2021-06-18T18:16:12.016576Z","shell.execute_reply.started":"2021-06-18T18:16:00.960274Z","shell.execute_reply":"2021-06-18T18:16:12.015941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"مدل ساخته شده در یک فایل ذخیره می‌شود.","metadata":{}},{"cell_type":"code","source":"tf.saved_model.save(model, \"nascar/nq\")","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:16:12.017782Z","iopub.execute_input":"2021-06-18T18:16:12.018069Z","iopub.status.idle":"2021-06-18T18:16:54.157033Z","shell.execute_reply.started":"2021-06-18T18:16:12.018021Z","shell.execute_reply":"2021-06-18T18:16:54.156305Z"},"trusted":true},"execution_count":null,"outputs":[]}]}