Created
May 24, 2022 06:20
-
-
Save lando22/1269e35493f7a01ad4f89ede3089e22c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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