Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<set_options>
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Dense, Input, BatchNormalization, Dropout, Concatenate
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.callbacks import ModelCheckpoint
warnings.simplefilter(action='ignore', category=FutureWarning )<load_from_csv>
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv" )
%%time
train_pickle_file = '/kaggle/input/pickling/train.csv.pandas.pickle'
train_data = pickle.load(open(train_pickle_file, 'rb'))
train_data.info()<set_options>
train_data = train.text.values
train_labels = train.target.values
test_data = test.text.values
class RunningMean:
def __init__(self, WIN_SIZE=20, n_size = 1):
self.n = 0
self.mean = np.zeros(n_size)
 self.cum_sum = 0
self.past_value = 0
self.WIN_SIZE = WIN_SIZE
self.windows = collections.deque(maxlen=WIN_SIZE+1)
 def clear(self):
self.n = 0
self.windows.clear()
def push(self, x):
x = fillna_npwhere_njit(x, self.past_value)
 self.past_value = x
self.windows.append(x)
 self.cum_sum += x
if self.n < self.WIN_SIZE:
self.n += 1
self.mean = self.cum_sum / float(self.n)
 else:
self.cum_sum -= self.windows.popleft()
self.mean = self.cum_sum / float(self.WIN_SIZE)
 def get_mean(self):
return self.mean if self.n else np.zeros(n_size)
 def __str__(self):
return "Current window values: {}".format(list(self.windows))
def fillna_npwhere_njit(array, values):
if np.isnan(array.sum()):
array = np.where(np.isnan(array), values, array)
 return array<statistical_test>
%%time
module_url = 'https://tfhub.dev/google/universal-sentence-encoder-large/4'
embed = hub.KerasLayer(module_url, trainable=False, name='USE_embedding' )
a = RunningMean(WIN_SIZE=10000)
 for index, row in tqdm(train_data[:100000].iterrows()):
a.push(np.array(row))
a.get_mean()<set_options>
def build_model(embed):
model = Sequential([
Input(shape=[], dtype=tf.string),
embed,
Dense(128, activation='relu'),
BatchNormalization() ,
Dropout(0.5),
Dense(64, activation='relu'),
BatchNormalization() ,
Dropout(0.5),
Dense(1, activation='sigmoid')
 ])
 model.compile(Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy'])
 return model
class RunningStats:
def __init__(self, WIN_SIZE=20, n_size = 1):
self.n = 0
self.mean = 0
self.run_var = 0
self.WIN_SIZE = WIN_SIZE
self.past_value = 0
self.windows = collections.deque(maxlen=WIN_SIZE+1)
 def clear(self):
self.n = 0
self.windows.clear()
def push(self, x):
x = fillna_npwhere_njit(x, self.past_value)
 self.past_value = x
self.windows.append(x)
 if self.n < self.WIN_SIZE:
self.n += 1
delta = x - self.mean
self.mean += delta / self.n
self.run_var += delta *(x - self.mean)
 else:
x_removed = self.windows.popleft()
old_m = self.mean
self.mean +=(x - x_removed)/ self.WIN_SIZE
self.run_var +=(x + x_removed - old_m - self.mean)*(x - x_removed)
 def get_mean(self):
return self.mean if self.n else np.zeros(n_size)
 def get_var(self):
return self.run_var /(self.n)if self.n > 1 else np.zeros(n_size)
 def get_std(self):
return math.sqrt(self.get_var())
 def get_all(self):
return list(self.windows)
 def __str__(self):
return "Current window values: {}".format(list(self.windows)) 
<normalization>
checkpoint = ModelCheckpoint('model.h5', monitor='val_loss', save_best_only=True)
 train_history = model.fit(
 train_data, train_labels,
validation_split=0.2,
epochs=20,
callbacks=[checkpoint],
batch_size=32
)
class RunningEWMean:
def __init__(self, WIN_SIZE=20, n_size = 1, lt_mean = None):
if lt_mean is not None:
self.s = lt_mean
else:
self.s = np.zeros(n_size)
 self.past_value = np.zeros(n_size)
 self.alpha = 2 /(WIN_SIZE + 1)
 def clear(self):
self.s = 0
def push(self, x):
x = fillna_npwhere_njit(x, self.past_value)
 self.past_value = x
self.s = self.alpha * x +(1 - self.alpha)* self.s
def get_mean(self):
return self.s<statistical_test>
model.load_weights('model.h5')
 test_pred = model.predict(test_data )
a = RunningEWMean(WIN_SIZE=10000)
 for index, row in tqdm(train_data[:100000].iterrows()):
a.push(np.array(row))
a.get_mean()<concatenate>
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False )