Skip to content

Instantly share code, notes, and snippets.

@lando22
Created May 24, 2022 06:20
Show Gist options
  • Save lando22/1269e35493f7a01ad4f89ede3089e22c to your computer and use it in GitHub Desktop.
Save lando22/1269e35493f7a01ad4f89ede3089e22c to your computer and use it in GitHub Desktop.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Embedding, Flatten, Dense
import numpy as np
import matplotlib.pyplot as plt
# Set the number of words to consider as features
max_features = 10000
# Cut texts after this number of words (among top max_features most common words)
maxlen = 500
# Load the data as lists of integers.
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
# This turns our lists of integers
# into a 2D integer tensor of shape `(samples, maxlen)`
x_train = pad_sequences(x_train, maxlen=maxlen)
x_test = pad_sequences(x_test, maxlen=maxlen)
# Build the model
model = Sequential()
model.add(Embedding(10000, 8, input_length=maxlen))
model.add(Flatten())
# We add the classifier on top
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
model.summary()
history = model.fit(x_train, y_train,
epochs=10,
batch_size=32,
validation_split=0.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment