0
$\begingroup$

I run two different Keras sequential models on IMDB data:

  1. truncate the input data by the first 100 words and translating to float32 array
  2. convert the input data using categorical array

The "1" model:

loss: 0.6236 - accuracy: 0.6617 - val_loss: 0.7157 - val_accuracy: 0.5215

The "2" model

loss: 0.0217 - accuracy: 0.9943 - val_loss: 0.5735 - val_accuracy:0.8734

Why is the "1" model much worse? How can this be fixed by configuring model settings?

Source code:

from keras.datasets import imdb
from keras import models
from keras import layers
from keras.utils import to_categorical
import numpy as np

TRUNC_SIZE = 100
NUM_WORDS = 10000

def take(list, n):
    if n >= len(list):
        return list
    
    result = []
    
    for i in range(n):
        result.append(list[i])
    
    return result

def decode_review(review):
    result = []

    for i in range(len(review)):
        j = review[i] - 3
        result.append(review[i])
    
    result = take(result, TRUNC_SIZE)
    n = len(result)

    if n < TRUNC_SIZE:
        for i in range(TRUNC_SIZE - n):
            result.append(0)

    return result

(train_in, train_out), (test_in, test_out) = imdb.load_data(num_words = NUM_WORDS)

index = imdb.get_word_index()
rev_index = dict([(i,word) for (word,i) in index.items()])

train_data = np.asarray([decode_review(review) for review in train_in]).astype('float32') / NUM_WORDS
test_data = np.asarray([decode_review(review) for review in test_in]).astype('float32') / NUM_WORDS

train_labels = np.asarray(train_out).astype('float32')
test_labels = np.asarray(test_out).astype('float32')

net =  models.Sequential()
net.add(layers.Dense(128, activation='relu', input_shape=(TRUNC_SIZE,)))
net.add(layers.Dense(32, activation='relu'))
net.add(layers.Dense(1, activation='sigmoid'))

net.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])

x_val = train_data[:10000]
partial_x_train = train_data[10000:]

y_val = train_labels[:10000]
partial_y_train = train_labels[10000:]

net.fit(partial_x_train, partial_y_train, epochs=20, batch_size=512, validation_data=(x_val, y_val))


####################################

def to_cat(data):
    result = np.zeros([len(data), NUM_WORDS])
    
    for i in range(len(data)):
        result[i, data[i]] = 1

    return result

(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words = NUM_WORDS)

index = imdb.get_word_index()
rev_index = dict([(i,word) for (word,i) in index.items()])

train_data = to_cat(train_data)
test_data = to_cat(test_data)

train_labels = np.asarray(train_labels).astype('float32')
test_labels = np.asarray(test_labels).astype('float32')

net =  models.Sequential()
net.add(layers.Dense(32, activation='relu', input_shape=(NUM_WORDS,)))
net.add(layers.Dense(16, activation='relu'))
net.add(layers.Dense(1, activation='sigmoid'))

net.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])

x_val = train_data[:10000]
partial_x_train = train_data[10000:]

y_val = train_labels[:10000]
partial_y_train = train_labels[10000:]

net.fit(partial_x_train, partial_y_train, epochs=20, batch_size=512, validation_data=(x_val, y_val))
$\endgroup$
1
  • 1
    $\begingroup$ The difference seems to simply be caused by the amount of information you are giving each model to learn from. The first model only gets the first 100 words, whereas the second models seems to be getting quite a bit more information. Try comparing the input for the model for an example to see the difference. Additionally, directly casting the words to float probably doesn't make sense and loses information. $\endgroup$
    – Oxbowerce
    Jan 15 at 17:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.