<jupyter_start><jupyter_text><jupyter_code>from tensorflow.keras import preprocessing
from tensorflow.keras import models
from tensorflow.keras.datasets import imdb
from sklearn.feature_extraction import text
import numpy as np
(training_data, training_targets), (testing_data, testing_targets) = imdb.load_data(num_words=10000)
data = np.concatenate((training_data, testing_data), axis=0)
targets = np.concatenate((training_targets, testing_targets), axis=0)

length = [len(i) for i in data]


print(f'Number of unique categories for the target variable:\n{len(np.unique(targets))}\n')
print(f'Average review length:\n{round(np.mean(length))}\n')
print(f'Standard Deviation of review lengths:\n{round(np.std(length))}')
index = imdb.get_word_index()
reverse_index = dict([(value, key) for (key, value) in index.items()])
decoded = ' '.join([reverse_index.get(i - 3, '#') for i in data[0]])
print(decoded)
def vectorize(sequences, dimension = 10000):
  results = np.zeros((len(sequences), dimension))
  for i, sequence in enumerate(sequences):
    results[i, sequence] = 1
  return results

data = vectorize(data)
targets = np.array(targets).astype('float32')
test_x = data[:10000]
test_y = targets[:10000]
train_x = data[10000:]
train_y = targets[10000:]
print(f'Length of train features:\n{len(train_x)}\n')
print(f'Length of test features:\n{len(test_x)}\n')
print(f'Length of train targets:\n{len(train_y)}\n')
print(f'Length of test targets:\n{len(test_y)}\n')
from tensorflow.keras.layers import Dense, Dropout, Activation
from tensorflow.keras.optimizers import Adam
dropout_rate = 0.1

model = models.Sequential()
model.add(Dense(50, activation='relu', input_shape=(10000, )))
model.add(Dropout(dropout_rate))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(dropout_rate))
model.add(Dense(2, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])

epochs = 20
batch_size=512
validation_data = (test_x, test_y)
model.summary()
results = model.fit(train_x, train_y, batch_size = batch_size, epochs=epochs, validation_split=0.2, validation_data=validation_data)

print('Test Loss:\n', np.mean(results.history['val_loss']))
print('Test Accuracy:\n', np.mean(results.history['val_acc']))
from keras.preprocessing.text import Tokenizer
import pandas as pd

file_yelp = '/yelp_labelled.txt'

df = pd.read_csv(file_yelp, names=['sentence', 'label'], sep='\t')

df.head()
X = df['sentence']
y = df['label']

from sklearn.model_selection import train_test_split

sentences_train, sentences_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
token = Tokenizer(5000)
token.fit_on_texts(sentences_train)

X_train = token.texts_to_sequences(sentences_train)
X_test = token.texts_to_sequences(sentences_test)

print(sentences_train[0])
print(X_train[0])
print(X.size)
from tensorflow.keras.layers import Embedding, Conv1D, GlobalMaxPool1D
from tensorflow.keras.preprocessing.sequence import pad_sequences

maxlen = 100
epochs = 20
embedding_dim = 50
num_filters = 64
kernel_size = 5
batch_size = 32
validation_data = (X_test, y_test)

X_train = pad_sequences(X_train, padding='post', maxlen=maxlen)
X_test = pad_sequences(X_test, padding='post', maxlen=maxlen)

model = models.Sequential()
model.add(Embedding(3200, embedding_dim, input_length=100))
model.add(Conv1D(num_filters, kernel_size=kernel_size))
model.add(GlobalMaxPool1D())
model.add(Dense(10, activation='softmax'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])
model.summary()
results = model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.2, validation_data=validation_data)


print('Training Loss:\n', np.mean(results.history['loss']))
print('Training Accuracy:\n', np.mean(results.history['acc']))
print()
print('Test Loss:\n', np.mean(results.history['val_loss']))
print('Test Accuracy:\n', np.mean(results.history['val_acc']))<jupyter_output>Epoch 1/20
18/18 [==============================] - 1s 31ms/step - loss: 0.6979 - acc: 0.5006 - val_loss: 0.6983 - val_acc: 0.4857
Epoch 2/20
18/18 [==============================] - 0s 20ms/step - loss: 0.6974 - acc: 0.4754 - val_loss: 0.6951 - val_acc: 0.4857
Epoch 3/20
18/18 [==============================] - 0s 20ms/step - loss: 0.6863 - acc: 0.5188 - val_loss: 0.6910 - val_acc: 0.5071
Epoch 4/20
18/18 [==============================] - 0s 20ms/step - loss: 0.6751 - acc: 0.6328 - val_loss: 0.6793 - val_acc: 0.6929
Epoch 5/20
18/18 [==============================] - 0s 19ms/step - loss: 0.6367 - acc: 0.8925 - val_loss: 0.6520 - val_acc: 0.7071
Epoch 6/20
18/18 [==============================] - 0s 20ms/step - loss: 0.5581 - acc: 0.9423 - val_loss: 0.6141 - val_acc: 0.7357
Epoch 7/20
18/18 [==============================] - 0s 18ms/step - loss: 0.4815 - acc: 0.9627 - val_loss: 0.5875 - val_acc: 0.7786
Epoch 8/20
18/18 [==============================] - 0s 20ms/step - loss: 0.4365 - a[...]
{'fraction_non_alphanumeric': 0.19495731586261664, 'fraction_numerical': 0.0639269406392694, 'mean_word_length': 6.3654970760233915, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '157091', 'license': 'MIT', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1791, 'n_words': 376, 'path': 'Sequential_NN_NLP.ipynb', 'repo_name': 'deshanahan/DATA-602-Homework', 'size': '5037'}