# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="zj7d6enfXO0I"
# ### Sentiment Analyisis - Natural Language Processing Task

# + [markdown] id="OktPX6xTXazv"
# ### Imports

# + id="pjuSkXtBXJ9L"
import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras import datasets
import numpy as np
import matplotlib.pyplot as plt

# + colab={"base_uri": "https://localhost:8080/"} id="Uc6ZI-C5Xc2q" outputId="05bb156e-cf4e-4907-d6fa-8c2600d7e4ac"
print(dir(datasets))

# + [markdown] id="CwXmc7RdXkWz"
# ### Loading data

# + colab={"base_uri": "https://localhost:8080/"} id="yO1d58mbXivq" outputId="9ba31af1-b398-4718-a64f-61a1fd8edd91"
imdb = datasets.imdb.load_data(num_words=10000)

# + id="hiSQo5NBXjR9"
(X_train, y_train), (X_test, y_test) = imdb

# + colab={"base_uri": "https://localhost:8080/"} id="c_FTYu_qXqqZ" outputId="69c823ce-da38-4d99-cb39-907b43402148"
X_train[:2], y_train[:2]

# + [markdown] id="HeItLjcvXuVc"
# ### Concating the data

# + id="tQ6cfpR8Xt_1"
features = np.concatenate([X_train, X_test])
labels = np.concatenate([y_train, y_test])

# + colab={"base_uri": "https://localhost:8080/"} id="Ke0AxzfMXsre" outputId="9dedd73a-f66a-45fe-d373-427e0483b0fd"
class_names =np.array( list(reversed(["positive", "negative"])))
targets = np.unique(labels)
class_names, targets

# + [markdown] id="gWxwArC3YVWG"
# ### Getting all the words that correspond to the Index given

# + colab={"base_uri": "https://localhost:8080/"} id="DdKXZRW0YMU6" outputId="9c3b7e0c-3cbe-4b5d-b40f-ba1df455b046"
index = datasets.imdb.get_word_index()
reverse_index = dict([(value, key) for (key, value) in index.items()]) 
reverse_index

# + colab={"base_uri": "https://localhost:8080/", "height": 35} id="203K2r58YXay" outputId="a8c46276-1e0c-444e-a47f-215ab8eb63b6"
reverse_index[59386]

# + colab={"base_uri": "https://localhost:8080/", "height": 171} id="dCny-KWbYanh" outputId="45e4c59c-8b97-4726-d93d-4f583284bb17"
sent = [reverse_index.get(i - 3, "#") for i in features[0]]
" ".join(sent)


# + [markdown] id="VMR5zAW1YgtV"
# > We are replacing every unknown word with a `#`. And we can see that this is a positive review

# + [markdown] id="cbc8Ot6OYpgM"
# ### DATA PREPARATION
# Now it's time to prepare our data. We will vectorize every review and fill it with zeros so it contains exactly 10,000 numbers. That means we fill every review that is shorter than 10,000 with zeros. We need to do this because the biggest review is nearly that long and every input for our neural network needs to have the `same size`. We will also transform the targets into floats.

# + id="DXvQka2_Yc3T"
def vectorize(sequences, dim=10000):
    res = np.zeros((len(sequences), dim))
    for i, seq in enumerate(sequences):
        res[i, seq] = 1
    return res  


# + id="OR0pwhhDYsXw"
X = vectorize(features)
y = labels.astype(np.float32)

# + id="EDxPNnU8YvB1"
from sklearn.model_selection import train_test_split

# + id="rZrWXJt3YvLE"
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size=.25 )

# + colab={"base_uri": "https://localhost:8080/"} id="C1fu2SflYzrg" outputId="bacc889a-e965-4cc9-ff70-5208cae5ded6"
X_train.shape, y_train.shape, X_test.shape

# + [markdown] id="e_JAKomXZdbD"
# ### Building a Model

# + colab={"base_uri": "https://localhost:8080/"} id="SHTjYWYAZdI0" outputId="b958ef84-9bef-4e98-955f-930a33383412"
model = keras.Sequential([
    keras.layers.Dense(32, activation='relu', input_shape=(10000, )),
    keras.layers.Dropout(0.3, noise_shape=None, seed=None),
    keras.layers.Dense(32, activation='relu'),
    keras.layers.Dropout(0.3, noise_shape=None, seed=None),
    keras.layers.Dense(50, activation = "relu"),
    keras.layers.Dropout(0.2, noise_shape=None, seed=None),
    keras.layers.Dense(50, activation = "relu"),
    # Output- Layer
    keras.layers.Dense(1, activation = "sigmoid")
  ])
model.summary()

# + [markdown] id="LQZ2sLSQaR9H"
# ### Compiling the model

# + id="Jcgz-8D0Y2j7"
model.compile(
    optimizer=keras.optimizers.Adam(learning_rate=1e-3),
    loss = keras.losses.BinaryCrossentropy(from_logits=True),
    metrics=["accuracy"]
)

# + colab={"base_uri": "https://localhost:8080/"} id="4S4S52jiZQYs" outputId="5c52d54e-230c-414f-dfec-6e8b72afe357"
BATCH_SIZE = 16
EPOCHS = 5
VALIDATION_SET = (X_test, y_test)

model.fit(X_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS, validation_data=VALIDATION_SET)

# + [markdown] id="DeSGuQ_rdNmk"
# #### Evaluating the model

# + colab={"base_uri": "https://localhost:8080/"} id="CS6vwWhGdSOP" outputId="c3386aa2-9755-40ad-b8d6-b62b3facb133"
model.evaluate(X_test, y_test)

# + [markdown] id="tR5YfbeWdFEG"
# #### Making predictions

# + colab={"base_uri": "https://localhost:8080/"} id="XSJto463bh87" outputId="9822f43d-7d30-4acd-c1f3-c851407f3fdd"
X_test[0], y_test[5]

# + colab={"base_uri": "https://localhost:8080/"} id="b9r_Fk23da75" outputId="436c710b-d855-47fa-e870-bdbb76e58d30"
np.round(model.predict((X_test[:5]))), y_test[:5]

# + colab={"base_uri": "https://localhost:8080/"} id="jYrR7J7be45q" outputId="957bca45-5f5a-49df-c7b6-23cc547a6015"
y

# + [markdown] id="0O3RsJqRh28n"
# That's all

# + id="z_P3ajIVggjo"


StarcoderdataJupyterScriptsDedupFiltered