<jupyter_start><jupyter_text>Prepare Environment<jupyter_code>!pip install -q tensorflow-gpu==2.0.0-beta1
# !pip install -q tensorflow-gpu==1.15
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import os
from sklearn.metrics import classification_report
from gensim.models import Word2Vec


tf.__version__
# train and save word2vec model, this step can be removed after uploading the trained model to github
# !wget https://github.com/raqueeb/datasets/raw/master/bnwiki-texts.zip
# !unzip bnwiki-texts.zip

# preprocessed_text_file_path = 'bnwiki-texts-preprocessed.txt'

# lines_from_file = []
# with open(preprocessed_text_file_path, encoding='utf8') as text_file:
#     for line in text_file:
#         lines_from_file.append(line)

# tokenized_lines = []
# for single_line in lines_from_file:
#     tokenized_lines.append(single_line.split())

# model = Word2Vec(tokenized_lines, size=200, window=5, min_count=10)

# model.wv.most_similar('ছেলে', topn=5)


# model.wv.save_word2vec_format('bn-wiki-word2vec-300.txt', binary=False)<jupyter_output><empty_output><jupyter_text>Dataset<jupyter_code>!wget http://1172.16.58.3:8090/bn-wiki-word2vec-300.txt
!ls
!wget https://raw.githubusercontent.com/tensorflow/hub/master/examples/text_embeddings_v2/export_v2.py
# !wget https://raw.githubusercontent.com/tensorflow/hub/master/examples/text_embeddings/export.py

!python export_v2.py --embedding_file=/content/bn-wiki-word2vec-300.txt --export_path=text_module --num_lines_to_ignore=1 
# !python export.py --embedding_file=/content/bn-wiki-word2vec-300.txt --export_path=text_module --num_lines_to_ignore=1 --preprocess_text=True
module_path = "text_module"
embedding_layer = hub.KerasLayer(module_path, trainable=False)
embedding_layer(['বাস বাস আমার '], ['আমার']).shape

!wget http://172.16.31.10:8090/bangla-sentiment.neg
!wget http://172.16.31.10:8090/bangla-sentiment.pos

all_sentences = []
with open('bangla-sentiment.pos', encoding='utf8') as f:
    all_sentences.extend([(line.strip(), 'positive') for line in f])
        
with open('bangla-sentiment.neg', encoding='utf8') as f:
    all_sentences.extend([(line.strip(), 'negative') for line in f])<jupyter_output>--2019-11-21 05:39:02--  http://172.16.31.10:8090/bangla-sentiment.neg
Connecting to 172.16.31.10:8090... connected.
HTTP request sent, awaiting response... 200 OK
Length: 363162 (355K) [application/octet-stream]
Saving to: ‘bangla-sentiment.neg’

bangla-sentiment.ne 100%[===================>] 354.65K   386KB/s    in 0.9s    

2019-11-21 05:39:04 (386 KB/s) - ‘bangla-sentiment.neg’ saved [363162/363162]

--2019-11-21 05:39:05--  http://172.16.31.10:8090/bangla-sentiment.pos
Connecting to 172.16.17.320:8090... connected.
HTTP request sent, awaiting response... 200 OK
Length: 220062 (215K) [application/octet-stream]
Saving to: ‘bangla-sentiment.pos’

bangla-sentiment.po 100%[===================>] 214.90K   235KB/s    in 0.9s    

2019-11-21 05:39:07 (235 KB/s) - ‘bangla-sentiment.pos’ saved [220062/220062]<jupyter_text>We can check the distribution of labels in the training and validation examples after shuffling.<jupyter_code>pos_count = 0
neg_count = 0
for sentence, label in all_sentences:
  if label =='positive':
      pos_count +=1
  else:
      neg_count +=1
print(pos_count)
print(neg_count)
import random

def generator():
    random.shuffle(all_sentences)    
    for sentence, label in all_sentences:
        if label =='positive':
            label = tf.keras.utils.to_categorical(1, num_classes=2)
        else:
            label = tf.keras.utils.to_categorical(0, num_classes=2)
        sentence_tensor = tf.constant(sentence, dtype=tf.dtypes.string)
        yield sentence_tensor, label
def make_dataset(train_size):
  data = tf.data.Dataset.from_generator(generator=generator, 
                                        output_types=(tf.string, tf.float32))
  train_size = 4000
  train_data = data.take(train_size)
  validation_data = data.skip(train_size)
  return train_data, validation_data
train_data, validation_data = make_dataset(0.80)
# get a single batch of 2 elements from train_data
next(iter(train_data.batch(2)))
sentences_in_a_single_batch, labels_in_a_single_batch = next(iter(train_data.batch(2)))
sentences_in_a_single_batch
sentences_in_a_single_batch.shape
labels_in_a_single_batch.shape
sentence, label = next(iter(train_data.take(1)))
# numpy() returns the string as bytes. we need to decode it to read it
sentence.numpy().decode('utf8')
# label after converted by to_categorical()
label.numpy()<jupyter_output><empty_output><jupyter_text>Model Training and Evaluation  Model<jupyter_code>def create_model():
  model = tf.keras.Sequential()
  model.add(embedding_layer)
  # model.add(tf.keras.layers.Flatten())
  # model.add(tf.keras.layers.SpatialDropout1D(0.2))
  # model.add(tf.keras.layers.LSTM(100, dropout=0.2, recurrent_dropout=0.2))
  # model.add(Dense(13, activation='softmax'))
  model.add(tf.keras.layers.Dense(256, activation="relu"))
  model.add(tf.keras.layers.Dense(128, activation="relu"))
  model.add(tf.keras.layers.Dense(2, activation="softmax"))
  model.compile(optimizer="adam",loss="categorical_crossentropy",metrics=['acc'])
  return model
model = create_model()
# Create earlystopping callback
# early_stopping_callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=3)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>batch_size = 256
history = model.fit(train_data.batch(batch_size), 
                    validation_data=validation_data.batch(batch_size), 
                    epochs=10,)
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
keras_layer (KerasLayer)     multiple                  200881800 
_________________________________________________________________
dense (Dense)                multiple                  77056     
_________________________________________________________________
dense_1 (Dense)              multiple                  32896     
_________________________________________________________________
dense_2 (Dense)              multiple                  258       
=================================================================
Total params: 200,992,010
Trainable params: 110,210
Non-trainable params: 200,881,800
_________________________________________________________________<jupyter_text>Saving modelAfter training the model we can export it as a [SavedModel](https://www.tensorflow.org/beta/guide/saved_model) to deploy or share with others.<jupyter_code>tf.saved_model.save(model, export_dir="my_model")<jupyter_output>INFO:tensorflow:Assets written to: my_model/assets<jupyter_text>Prediction<jupyter_code>sents = ['আমরা খুবি খুশি অফারটির জন্য', 'বই পড়তে পছন্দ করি', 'বই পড়তে পছন্দ করি না', 'আমার ভালো লাগছে না', 
         'আমার কষ্ট লাগছে', 'এই বইটা বেশ ভালো লাগছে', 'একটা দুর্ঘটনা ঘটে গেল',
         'জিপি আমার প্রিয় নেটওয়ার্ক', 'মোবাইল অপারেটর বেশ টাকা কাটে', 'আমাদের প্রতিদিনের সমস্যা নিয়ে ঝামেলায় আছি',
         'ঢাকা-সিলেটসহ আশপাশের সড়কের যানবাহন চলাচল বন্ধ হয়ে যায়',]
pred_dataset = tf.data.Dataset.from_tensor_slices(sents)
prediction = model.predict(np.array(sents))

for sentence, pred_sentiment in zip(sents, prediction.argmax(axis=1)):
  print("Sentence:{} - predicted: {}".format(sentence, pred_sentiment))<jupyter_output>Sentence:আমরা খুবি খুশি অফারটির জন্য - predicted: 1
Sentence:বই পড়তে পছন্দ করি - predicted: 1
Sentence:বই পড়তে পছন্দ করি না - predicted: 0
Sentence:আমার ভালো লাগছে না - predicted: 0
Sentence:আমার কষ্ট লাগছে - predicted: 0
Sentence:এই বইটা বেশ ভালো লাগছে - predicted: 1
Sentence:একটা দুর্ঘটনা ঘটে গেল - predicted: 0
Sentence:জিপি আমার প্রিয় নেটওয়ার্ক - predicted: 1
Sentence:মোবাইল অপারেটর বেশ টাকা কাটে - predicted: 1
Sentence:আমাদের প্রতিদিনের সমস্যা নিয়ে ঝামেলায় আছি - predicted: 0
Sentence:ঢাকা-সিলেটসহ আশপাশের সড়কের যানবাহন চলাচল বন্ধ হয়ে যায় - predicted: 0
{'fraction_non_alphanumeric': 0.15317775571002978, 'fraction_numerical': 0.04692154915590864, 'mean_word_length': 4.7673586256263425, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 7, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '343330', 'license': 'MIT', 'n_tokens_mistral': 3413, 'n_tokens_neox': 3299, 'n_words': 550, 'path': 'sentiment_model_positive_negetive.ipynb', 'repo_name': 'shamimreza/TensorFlow2', 'size': '8055'}