text_plain_output_1.png
from tensorflow.keras.layers.experimental.preprocessing import TextVectorization

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import tensorflow as tf

tf.__version__

data = pd.read_csv('../input/womens-ecommerce-clothing-reviews/Womens Clothing E-Commerce Reviews.csv', index_col=0)

data.shape

data = data[~data['Review Text'].isnull()]
data.shape

labels = tf.keras.utils.to_categorical(data['Recommended IND'])
output_shape = labels.shape[1]
(labels, output_shape)

v_size = 2000
max_len = 100
e_dim = 64
batch_size = 256

pre_processing_layer = TextVectorization(max_tokens=v_size, output_sequence_length=max_len, name='Notes_preprocessing_layer')

pre_processing_layer.adapt(X_train)
vocab = pre_processing_layer.get_vocabulary()

tf.keras.backend.clear_session()
model = tf.keras.models.Sequential([tf.keras.layers.Embedding(input_dim=v_size, output_dim=e_dim, name='embedding', mask_zero=True), tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)), tf.keras.layers.Dense(16, activation='relu'), tf.keras.layers.Dense(output_shape, activation='softmax')])
metrics = [tf.keras.metrics.CategoricalAccuracy()]
model.summary()

model.compile(optimizer='adam', loss=tf.keras.losses.CategoricalCrossentropy(from_logits=False), metrics=metrics)

train_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
valid_dataset = tf.data.Dataset.from_tensor_slices((X_val, y_val))
test_dataset = tf.data.Dataset.from_tensor_slices((X_test, y_test))
options = tf.data.Options()
options.experimental_distribute.auto_shard_policy = tf.data.experimental.AutoShardPolicy.DATA
raw_train_ds = train_dataset.shuffle(X_train.shape[0]).batch(batch_size).with_options(options)
raw_val_ds = valid_dataset.batch(batch_size).with_options(options)
raw_test_ds = test_dataset.batch(batch_size).with_options(options)

@tf.autograph.experimental.do_not_convert
def vectorize_text(text, label):
    """ convert text to tokens """
    text = tf.expand_dims(text, -1)
    return (pre_processing_layer(text), label)
text_batch, label_batch = next(iter(raw_train_ds.shuffle(50)))
first_review, first_label = (text_batch[0], label_batch[0])

train_ds = raw_train_ds.map(vectorize_text)
val_ds = raw_val_ds.map(vectorize_text)
test_ds = raw_test_ds.map(vectorize_text)
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
test_ds = test_ds.cache().prefetch(buffer_size=AUTOTUNE)

model.fit(train_ds, validation_data=val_ds, epochs=3, verbose=1)