Toxicity Analysis

This tutorial is available as an IPython notebook at Malaya/example/toxicity.

This module trained on both standard and local (included social media) language structures, so it is save to use for both.

[1]:
%%time
import malaya
CPU times: user 4.94 s, sys: 665 ms, total: 5.6 s
Wall time: 4.66 s

get labels

[2]:
malaya.toxicity.label
[2]:
['severe toxic',
 'obscene',
 'identity attack',
 'insult',
 'threat',
 'asian',
 'atheist',
 'bisexual',
 'buddhist',
 'christian',
 'female',
 'heterosexual',
 'indian',
 'homosexual, gay or lesbian',
 'intellectual or learning disability',
 'male',
 'muslim',
 'other disability',
 'other gender',
 'other race or ethnicity',
 'other religion',
 'other sexual orientation',
 'physical disability',
 'psychiatric or mental illness',
 'transgender',
 'malay',
 'chinese']
[4]:
string = 'Benda yg SALAH ni, jgn lah didebatkan. Yg SALAH xkan jadi betul. Ingat tu. Mcm mana kesat sekalipun org sampaikan mesej, dan memang benda tu salah, diam je. Xyah nk tunjuk kau open sangat nk tegur cara org lain berdakwah. '
another_string = 'melayu bodoh, dah la gay, sokong lgbt lagi, memang tak guna'
string1 = 'Sis, students from overseas were brought back because they are not in their countries which is if something happens to them, its not the other countries’ responsibility. Student dalam malaysia ni dah dlm tggjawab kerajaan. Mana part yg tak faham?'
string2 = 'Harap kerajaan tak bukak serentak. Slowly release week by week. Focus on economy related industries dulu'

Load multinomial model

All model interface will follow sklearn interface started v3.4,

model.predict(List[str])

model.predict_proba(List[str])
[5]:
model = malaya.toxicity.multinomial()
[6]:
model.predict_proba([string])
[6]:
[{'severe toxic': 0.9983866471486633,
  'obscene': 0.9609727610993377,
  'identity attack': 0.8695613508984636,
  'insult': 0.5893315709933827,
  'threat': 0.022178387416617994,
  'asian': 0.020300810205187092,
  'atheist': 0.011794932510638331,
  'bisexual': 0.002584488616645158,
  'buddhist': 0.004570410474229619,
  'christian': 0.03405075979783316,
  'female': 0.03787090649113612,
  'heterosexual': 0.008360866566466152,
  'indian': 0.9206507865140837,
  'homosexual, gay or lesbian': 0.03492931132214706,
  'intellectual or learning disability': 0.00158322379679834,
  'male': 0.06432988855860852,
  'muslim': 0.06722155678421161,
  'other disability': 0.0,
  'other gender': 0.0,
  'other race or ethnicity': 0.0017973269863205566,
  'other religion': 0.0017937047323945308,
  'other sexual orientation': 0.0012965120040433268,
  'physical disability': 0.001553693991766015,
  'psychiatric or mental illness': 0.024938805254016427,
  'transgender': 0.011663162911194878,
  'malay': 0.9995238230425324,
  'chinese': 0.9912614436972298}]

List available Transformer models

[5]:
malaya.toxicity.available_transformer()
INFO:root:tested on 20% test set.
[5]:
Size (MB) Quantized Size (MB) Accuracy
bert 425.6 111.00 0.814
tiny-bert 57.4 15.40 0.815
albert 48.6 12.80 0.812
tiny-albert 22.4 5.98 0.808
xlnet 446.6 118.00 0.807
alxlnet 46.8 13.30 0.817

Load ALXLNET model

All model interface will follow sklearn interface started v3.4,

model.predict(List[str])

model.predict_proba(List[str])
[9]:
model = malaya.toxicity.transformer(model = 'alxlnet')

Load Quantized model

To load 8-bit quantized model, simply pass quantized = True, default is False.

We can expect slightly accuracy drop from quantized model, and not necessary faster than normal 32-bit float model, totally depends on machine.

[ ]:
quantized_model = malaya.toxicity.transformer(model = 'alxlnet', quantized = True)
WARNING:root:Load quantized model will cause accuracy drop.

Predict batch of strings

[11]:
model.predict_proba([string,another_string])
[11]:
[{'severe toxic': 0.201493,
  'obscene': 0.12493244,
  'identity attack': 0.005829394,
  'insult': 0.08384159,
  'threat': 0.0010293126,
  'asian': 0.0004298091,
  'atheist': 0.0005042255,
  'bisexual': 0.0007214546,
  'buddhist': 0.00031352043,
  'christian': 0.001463592,
  'female': 0.095250845,
  'heterosexual': 0.00018996,
  'indian': 0.029991329,
  'homosexual, gay or lesbian': 0.00020930171,
  'intellectual or learning disability': 0.00018399954,
  'male': 0.017134428,
  'muslim': 0.0050880015,
  'other disability': 0.000233531,
  'other gender': 4.813075e-05,
  'other race or ethnicity': 0.00010916591,
  'other religion': 0.00031152368,
  'other sexual orientation': 0.00026413798,
  'physical disability': 0.000107735395,
  'psychiatric or mental illness': 3.6627054e-05,
  'transgender': 0.00016203523,
  'malay': 0.08275634,
  'chinese': 0.001092732},
 {'severe toxic': 0.9906007,
  'obscene': 0.90202737,
  'identity attack': 0.9795381,
  'insult': 0.6345859,
  'threat': 0.015953332,
  'asian': 0.014682382,
  'atheist': 0.0035497844,
  'bisexual': 0.0326609,
  'buddhist': 0.0101745725,
  'christian': 0.025312841,
  'female': 0.00968048,
  'heterosexual': 0.029808193,
  'indian': 0.011105597,
  'homosexual, gay or lesbian': 0.13856784,
  'intellectual or learning disability': 0.04939267,
  'male': 0.014529228,
  'muslim': 0.024640262,
  'other disability': 0.0009796321,
  'other gender': 0.037679344,
  'other race or ethnicity': 0.033878565,
  'other religion': 0.003752023,
  'other sexual orientation': 0.103711344,
  'physical disability': 0.00469586,
  'psychiatric or mental illness': 0.001588594,
  'transgender': 0.003436562,
  'malay': 0.9901147,
  'chinese': 0.1126565}]
[7]:
quantized_model.predict_proba([string,another_string])
[7]:
[{'severe toxic': 0.41759574,
  'obscene': 0.4265346,
  'identity attack': 0.028848499,
  'insult': 0.28315687,
  'threat': 0.0126080215,
  'asian': 0.0013097227,
  'atheist': 0.0036462843,
  'bisexual': 0.00034737587,
  'buddhist': 0.0020015836,
  'christian': 0.0017515123,
  'female': 0.027979434,
  'heterosexual': 0.00059223175,
  'indian': 0.017766118,
  'homosexual, gay or lesbian': 0.0018276274,
  'intellectual or learning disability': 0.0021783412,
  'male': 0.010304272,
  'muslim': 0.039312482,
  'other disability': 0.00081172585,
  'other gender': 0.0010196567,
  'other race or ethnicity': 0.00034156442,
  'other religion': 0.0018753111,
  'other sexual orientation': 0.0074941516,
  'physical disability': 9.429455e-05,
  'psychiatric or mental illness': 0.0018553138,
  'transgender': 0.0012151003,
  'malay': 0.015449166,
  'chinese': 0.041799486},
 {'severe toxic': 0.99501216,
  'obscene': 0.92981744,
  'identity attack': 0.9498557,
  'insult': 0.85752094,
  'threat': 0.027202696,
  'asian': 0.0037056804,
  'atheist': 0.0037265718,
  'bisexual': 0.033751667,
  'buddhist': 0.0025248528,
  'christian': 0.0072034895,
  'female': 0.009898871,
  'heterosexual': 0.03563884,
  'indian': 0.045443088,
  'homosexual, gay or lesbian': 0.15225068,
  'intellectual or learning disability': 0.008484751,
  'male': 0.082333446,
  'muslim': 0.03321591,
  'other disability': 0.0020376444,
  'other gender': 0.004865974,
  'other race or ethnicity': 0.0036396086,
  'other religion': 0.0006239116,
  'other sexual orientation': 0.023028428,
  'physical disability': 0.00034190973,
  'psychiatric or mental illness': 0.0032041247,
  'transgender': 0.006126247,
  'malay': 0.9389714,
  'chinese': 0.18799332}]

Open toxicity visualization dashboard

Default when you call predict_words it will open a browser with visualization dashboard, you can disable by visualization=False.

[13]:
model.predict_words(another_string)
[14]:
from IPython.core.display import Image, display

display(Image('toxicity-dashboard.png', width=800))
_images/load-toxic_21_0.png

Vectorize

Let say you want to visualize sentence / word level in lower dimension, you can use model.vectorize,

def vectorize(self, strings: List[str], method: str = 'first'):
    """
    vectorize list of strings.

    Parameters
    ----------
    strings: List[str]
    method : str, optional (default='first')
        Vectorization layer supported. Allowed values:

        * ``'last'`` - vector from last sequence.
        * ``'first'`` - vector from first sequence.
        * ``'mean'`` - average vectors from all sequences.
        * ``'word'`` - average vectors based on tokens.

    Returns
    -------
    result: np.array
    """

Sentence level

[8]:
texts = [string, another_string, string1, string2]
r = quantized_model.vectorize(texts, method = 'first')
[9]:
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt

tsne = TSNE().fit_transform(r)
tsne.shape
[9]:
(4, 2)
[11]:
plt.figure(figsize = (7, 7))
plt.scatter(tsne[:, 0], tsne[:, 1])
labels = texts
for label, x, y in zip(
    labels, tsne[:, 0], tsne[:, 1]
):
    label = (
        '%s, %.3f' % (label[0], label[1])
        if isinstance(label, list)
        else label
    )
    plt.annotate(
        label,
        xy = (x, y),
        xytext = (0, 0),
        textcoords = 'offset points',
    )
_images/load-toxic_26_0.png

Word level

[17]:
r = quantized_model.vectorize(texts, method = 'word')
[18]:
x, y = [], []
for row in r:
    x.extend([i[0] for i in row])
    y.extend([i[1] for i in row])
[19]:
tsne = TSNE().fit_transform(y)
tsne.shape
[19]:
(107, 2)
[20]:
plt.figure(figsize = (7, 7))
plt.scatter(tsne[:, 0], tsne[:, 1])
labels = x
for label, x, y in zip(
    labels, tsne[:, 0], tsne[:, 1]
):
    label = (
        '%s, %.3f' % (label[0], label[1])
        if isinstance(label, list)
        else label
    )
    plt.annotate(
        label,
        xy = (x, y),
        xytext = (0, 0),
        textcoords = 'offset points',
    )
_images/load-toxic_31_0.png

Pretty good, outliers are toxic words.

Stacking models

More information, you can read at https://malaya.readthedocs.io/en/latest/Stack.html

[16]:
albert = malaya.toxicity.transformer(model = 'albert')
INFO:tensorflow:loading sentence piece model
[18]:
malaya.stack.predict_stack([model, albert], [another_string])
[18]:
[{'severe toxic': 0.9968317,
  'obscene': 0.43022493,
  'identity attack': 0.90531594,
  'insult': 0.42289576,
  'threat': 0.0058603976,
  'asian': 0.000983668,
  'atheist': 0.0005495089,
  'bisexual': 0.0009623809,
  'buddhist': 0.0003632398,
  'christian': 0.0018632574,
  'female': 0.006050684,
  'heterosexual': 0.0025569045,
  'indian': 0.0056869243,
  'homosexual, gay or lesbian': 0.012232827,
  'intellectual or learning disability': 0.00091394753,
  'male': 0.011594971,
  'muslim': 0.0042621437,
  'other disability': 0.00027529505,
  'other gender': 0.0010361207,
  'other race or ethnicity': 0.0012320877,
  'other religion': 0.00091365684,
  'other sexual orientation': 0.0027996385,
  'physical disability': 0.00010540871,
  'psychiatric or mental illness': 0.000815311,
  'transgender': 0.0016718076,
  'malay': 0.96644485,
  'chinese': 0.05199418}]
[ ]: