<jupyter_start><jupyter_text><jupyter_code>!pip install tensorflow==2.4.3
import tensorflow as tf
tf.__version__
import matplotlib.pyplot as plt
from google.colab import drive
drive.mount('/content/drive')
import pandas as pd
df = pd.read_csv('/content/drive/MyDrive/dataset/amazon_reviews_us_Shoes_v1_00.tsv', sep='\t',error_bad_lines=False)
df.head()
df['sentiments'] = df.star_rating.apply(lambda x: 0 if x in [1, 2, 3] else 1)
df[df['sentiments']==1]['review_id'][1]
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import numpy as np
from tensorflow import keras
import pickle
tokenizer = Tokenizer(oov_token="<OOV>")
split = round(len(df)*0.8)
train_reviews = df['review_id'][:split]
train_label = df['sentiments'][:split]
test_reviews = df['review_id'][split:]
test_label = df['sentiments'][split:]
train_reviews

training_sentences = []
training_labels = []
testing_sentences = []
testing_labels = []
for column in train_reviews:
    training_sentences.append(str(column))
for column in train_label:
    training_labels.append(column)
for column in test_reviews:
    testing_sentences.append(str(column))
for column in test_label:
    testing_labels.append(column)
training_sentences
training_sentences[0]
training_labels[0]
vocab_size = 40000
embedding_dim = 16
max_length = 120
trunc_type = 'post'
oov_tok = '<OOV>'
padding_type = 'post'
tokenizer = Tokenizer(num_words=vocab_size, oov_token=oov_tok)
tokenizer.fit_on_texts(training_sentences)
word_index = tokenizer.word_index
tokenizer
sequences = tokenizer.texts_to_sequences(training_sentences)
sequences
padded = pad_sequences(sequences, maxlen=max_length, truncating=trunc_type)
padded
testing_sentences = tokenizer.texts_to_sequences(testing_sentences)
testing_padded = pad_sequences(testing_sentences, maxlen=max_length)
type(training_labels)
training_labels_final = np.array(training_labels)
testing_labels_final = np.array(testing_labels)
training_labels_final
model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, embedding_dim,input_length=max_length),
    tf.keras.layers.GlobalAveragePooling1D(),
    tf.keras.layers.Dense(6, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
num_epochs = 20
history = model.fit(padded, training_labels_final, epochs=num_epochs, validation_data=(testing_padded, testing_labels_final))
import matplotlib.pyplot as plt
def plot_graphs(history, metric):
  plt.plot(history.history[metric])
  plt.plot(history.history['val_'+metric], '')
  plt.xlabel("Epochs")
  plt.ylabel(metric)
  plt.legend([metric, 'val_'+metric])
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plot_graphs(history, 'accuracy')
plt.ylim(None, 1)
plt.subplot(1, 2, 2)
plot_graphs(history, 'loss')
plt.ylim(0, None)
sample_text = [df[df['sentiments']==1]['review_id'][1]]
sample_text
sample_sequences = tokenizer.texts_to_sequences(sample_text)
sample_padded = pad_sequences(sample_sequences, maxlen=max_length, truncating=trunc_type)
predictions = model.predict(sample_padded)
predictions
if predictions[0][0] > 0.5:
  print('Review Positive')
else:
  print('Review Negative')
with open('tokenizer.pickle', 'wb') as handle:
    pickle.dump(tokenizer, handle, protocol=pickle.HIGHEST_PROTOCOL)
model.save('./test_model', save_format='tf')
!zip -r 'test_model.zip' 'test_model'<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12728310502283105, 'fraction_numerical': 0.013984018264840182, 'mean_word_length': 8.033505154639176, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '53898', 'license': 'Apache-2.0', 'n_tokens_mistral': 1275, 'n_tokens_neox': 1225, 'n_words': 221, 'path': 'PYIM_Assgn_4_PYIM009ONL009_RISVANI.ipynb', 'repo_name': 'risvanisaputr/datasets', 'size': '3504'}