<jupyter_start><jupyter_text>Sentiment Analyisis - Natural Language Processing Task  Imports<jupyter_code>import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras import datasets
import numpy as np
import matplotlib.pyplot as plt
print(dir(datasets))<jupyter_output>['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_sys', 'boston_housing', 'cifar10', 'cifar100', 'fashion_mnist', 'imdb', 'mnist', 'reuters']<jupyter_text>Loading data<jupyter_code>imdb = datasets.imdb.load_data(num_words=10000)
(X_train, y_train), (X_test, y_test) = imdb
X_train[:2], y_train[:2]<jupyter_output><empty_output><jupyter_text>Concating the data<jupyter_code>features = np.concatenate([X_train, X_test])
labels = np.concatenate([y_train, y_test])
class_names =np.array( list(reversed(["positive", "negative"])))
targets = np.unique(labels)
class_names, targets<jupyter_output><empty_output><jupyter_text>Getting all the words that correspond to the Index given<jupyter_code>index = datasets.imdb.get_word_index()
reverse_index = dict([(value, key) for (key, value) in index.items()]) 
reverse_index
reverse_index[59386]
sent = [reverse_index.get(i - 3, "#") for i in features[0]]
" ".join(sent)<jupyter_output><empty_output><jupyter_text>> We are replacing every unknown word with a ``. And we can see that this is a positive review  DATA PREPARATIONNow 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.<jupyter_code>def vectorize(sequences, dim=10000):
    res = np.zeros((len(sequences), dim))
    for i, seq in enumerate(sequences):
        res[i, seq] = 1
    return res  
X = vectorize(features)
y = labels.astype(np.float32)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size=.25 )
X_train.shape, y_train.shape, X_test.shape<jupyter_output><empty_output><jupyter_text>Building a Model<jupyter_code>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()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 32)                320032    
_________________________________________________________________
dropout (Dropout)            (None, 32)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 32)                1056      
_________________________________________________________________
dropout_1 (Dropout)          (None, 32)                0         
_________________________________________________________________
dense_2 (Dense)              (None, 50)                1650      
_________________________________________________________________
dropout_2 (Dropout)          (None, 50)                0         
________________________________________________________[...]<jupyter_text>Compiling the model<jupyter_code>model.compile(
    optimizer=keras.optimizers.Adam(learning_rate=1e-3),
    loss = keras.losses.BinaryCrossentropy(from_logits=True),
    metrics=["accuracy"]
)
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)<jupyter_output>Epoch 1/5
2344/2344 [==============================] - 15s 5ms/step - loss: 0.4152 - accuracy: 0.8014 - val_loss: 0.2530 - val_accuracy: 0.8967
Epoch 2/5
2344/2344 [==============================] - 11s 5ms/step - loss: 0.2160 - accuracy: 0.9150 - val_loss: 0.2549 - val_accuracy: 0.8938
Epoch 3/5
2344/2344 [==============================] - 11s 5ms/step - loss: 0.1720 - accuracy: 0.9354 - val_loss: 0.2727 - val_accuracy: 0.8954
Epoch 4/5
2344/2344 [==============================] - 11s 5ms/step - loss: 0.1332 - accuracy: 0.9500 - val_loss: 0.3030 - val_accuracy: 0.8915
Epoch 5/5
2344/2344 [==============================] - 11s 5ms/step - loss: 0.1164 - accuracy: 0.9523 - val_loss: 0.3609 - val_accuracy: 0.8913<jupyter_text>Evaluating the model<jupyter_code>model.evaluate(X_test, y_test)<jupyter_output>391/391 [==============================] - 1s 3ms/step - loss: 0.3609 - accuracy: 0.8913<jupyter_text>Making predictions<jupyter_code>X_test[0], y_test[5]
np.round(model.predict((X_test[:5]))), y_test[:5]
y<jupyter_output><empty_output><jupyter_text>That's all<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15599774732494837, 'fraction_numerical': 0.05368875539703398, 'mean_word_length': 4.9397993311036785, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '179637', 'license': 'MIT', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1696, 'n_words': 381, 'path': 'keras-nn/10_FC_NN_Sentiment_Analysis/Sentiment_Analyisis_imdb.ipynb', 'repo_name': 'CrispenGari/tensorflow-python', 'size': '5327'}