<jupyter_start><jupyter_text><jupyter_code>import tensorflow as tf
from tensorflow.keras import datasets, layers, models, preprocessing
import tensorflow_datasets as tfds
max_len = 200
n_words = 10000
dim_embedding = 256
EPOCHS = 20
BATCH_SIZE = 500
def load_data():
  # Load data.
  (X_train, y_train), (X_test, y_test) = datasets.imdb.load_data(num_words=n_words)
  # Pad sequences with max_len.
  X_train = preprocessing.sequence.pad_sequences(X_train, maxlen=max_len)
  X_test = preprocessing.sequence.pad_sequences(X_test, maxlen=max_len)
  return (X_train, y_train), (X_test, y_test)
def build_model():
  model = models.Sequential()
  # Input: - eEmbedding Layer.
  # The model will take as input an integer matrix of size (batch,
  # input_length).
  # The model will output dimension (input_length, dim_embedding).
  # The largest integer in the input should be no larger
  # than n_words (vocabulary size).
  model.add(layers.Embedding(n_words,
                             dim_embedding, 
                             input_length=max_len))
  model.add(layers.Dropout(0.3))
  # Takes the maximum value of either feature vector from each of
  # the n_words features.
  model.add(layers.GlobalMaxPooling1D())
  model.add(layers.Dense(128, activation='relu'))
  model.add(layers.Dropout(0.5))
  model.add(layers.Dense(1, activation='sigmoid'))
  return model  
(X_train, y_train), (X_test, y_test) = load_data()
model = build_model()
model.summary()
model.compile(optimizer = "adam", 
              loss = "binary_crossentropy",
              metrics = ["accuracy"])
score = model.fit(X_train, y_train,
                  epochs = EPOCHS,
                  batch_size = BATCH_SIZE,
                  validation_data = (X_test, y_test))
score = model.evaluate(X_test, y_test, batch_size=BATCH_SIZE)
print("\nTest score:", score[0])
print('Test accuracy:', score[1])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11, 'fraction_numerical': 0.014210526315789474, 'mean_word_length': 3.7171215880893302, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '631345', 'license': 'Apache-2.0', 'n_tokens_mistral': 664, 'n_tokens_neox': 634, 'n_words': 150, 'path': 'imdb.ipynb', 'repo_name': 'aabor/tf_basics', 'size': '1900'}