Skip to content

Instantly share code, notes, and snippets.

View re4388's full-sized avatar
🎯
Focusing

re4388 re4388

🎯
Focusing
  • Taipei,Taiwan
View GitHub Profile
@re4388
re4388 / a3
Created January 24, 2020 06:31
a3
!pip3 install --quiet "tensorflow>=1.7"
!pip3 install --quiet tensorflow-hub
!pip3 install seaborn
import tensorflow as tf
import tensorflow_hub as hub
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import re
@re4388
re4388 / a4
Created January 24, 2020 06:32
a4
['HUM', 'ENTY', 'LOC', 'ABBR', 'DESC', 'NUM']
@re4388
re4388 / a5
Created January 24, 2020 06:32
a5
new_text = ["current us president?", "what compose my pc?", 'where is USA', 'what U.S.A mean?',
'tell me about how economy work', 'the number of PI?']
@re4388
re4388 / a11
Last active January 24, 2020 06:35
a11
import seaborn as sns
import matplotlib.pyplot as plt
df_train = get_dataframe('test_data.txt')
category_counts = len(df_train.label.cat.categories)
print(category_counts) # 6
sns.barplot(x.index,x)
plt.gca().set_ylabel('samples')
@re4388
re4388 / a12
Created January 24, 2020 06:37
a12
def UniversalEmbedding(x):
return embed(tf.squeeze(tf.cast(x, tf.string)), signature="default", as_dict=True)["default"]
input_text = layers.Input(shape=(1,), dtype=tf.string)
embedding = layers.Lambda(UniversalEmbedding, output_shape=(embed_size,))(input_text)
dense = layers.Dense(256, activation='relu')(embedding)
pred = layers.Dense(category_counts, activation='softmax')(dense)
model = Model(inputs=[input_text], outputs=pred)
@re4388
re4388 / a13
Created January 24, 2020 06:38
a13
train_text = np.array(df_train['text'].tolist(), dtype=object)[:, np.newaxis]
train_label = np.asarray(pd.get_dummies(df_train.label), dtype = np.int8)
print(train_text.shape)
print(train_label.shape)
print(train_label[:3])
# also handel the test set
df_test = get_dataframe('test_data.txt')
test_text = np.array(df_test['text'].tolist(), dtype=object)[:, np.newaxis]
@re4388
re4388 / a16
Created January 24, 2020 06:39
a16
with tf.Session() as session:
K.set_session(session)
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
history = model.fit(train_text, train_label,
validation_data=(test_text, test_label),
epochs=10,
batch_size=32)
model.save_weights('./model.h5')
@re4388
re4388 / a18
Created January 24, 2020 06:43
a18
def run_pred(text_list):
with tf.Session() as session:
K.set_session(session)
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
model.load_weights('./model.h5')
predicts = model.predict(np.array(text_list, dtype=object)[:, np.newaxis], batch_size=32)
categories = df_train.label.cat.categories.tolist()
predict_labels = [categories[logit] for logit in predicts.argmax(axis=1)]
return predict_labels
@re4388
re4388 / a1
Last active January 25, 2020 07:46
a1
# code origin: https://www.jianshu.com/p/e1d95c4e1697
class Entity:
def __init__(self, size, x, y):
""" init x, y when init the instance"""
self.x, self.y = x, y
self.size = size
def __call__(self, x, y):
'''you can change x, y after'''
@re4388
re4388 / a2
Created January 25, 2020 07:46
a2
# code origin: https://www.kaggle.com/timesler/facial-recognition-model-in-pytorch
class DetectionPipeline:
"""Pipeline class for detecting faces in the frames of a video file."""
def __init__(self, detector, n_frames=None, batch_size=60, resize=None):
"""Constructor for DetectionPipeline class.
Keyword Arguments:
n_frames {int} -- Total number of frames to load. These will be evenly spaced