简体   繁体   中英

Tensorflow/Keras model disregarding training instructions?

Edit: after more analysis II discovered that tf recently switched to listing batches instead of samples, which explains why I was seeing 30 instead of 15000. I still don't know why the tested accuracy is so low.

I'm just starting to get into Tensorflow for neural.networks, and thought I would make a simple sentiment prediction model using the imdb Keras dataset.

My problem is that the.network, instead of training each epoch on 10000 examples as it should, trains each epoch on just 30 examples. This results in a worse than random accuracy on the test data. Changing the batch size to 1 fixes the example count to the correct 10000, but the.network is still under 50% in accuracy. I simply cannot see why this is happening.

Here is my code:

import tensorflow as td
from tensorflow import keras
import numpy as np

data = keras.datasets.imdb

(train_data, train_labels), (test_data, test_labels) = data.load_data(num_words=10000)

word_index = data.get_word_index()

word_index = {k:(v+3) for k, v, in word_index.items()}
word_index["<PAD>"] = 0
word_index["<START>"] = 1
word_index["<UNK>"] = 2
word_index["<UNUSED>"] = 3

reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])

train_data = keras.preprocessing.sequence.pad_sequences(train_data, value=word_index["<PAD>"], padding ="post", maxlen=250)
test_data = keras.preprocessing.sequence.pad_sequences(train_data, value=word_index["<PAD>"], padding ="post", maxlen=250)


# === MODEL ===

model = keras.Sequential()
model.add(keras.layers.Embedding(10000, 16))
model.add(keras.layers.GlobalAveragePooling1D())
model.add(keras.layers.Dense(16, activation='relu'))
model.add(keras.layers.Dense(1, activation='sigmoid'))

model.summary()

model.compile(optimizer="adam", loss='binary_crossentropy', metrics=["accuracy"])

x_val = train_data[:10000]
x_train = train_data[10000:]

y_val = train_labels[:10000]
y_train = train_labels[10000:]

fitModel = model.fit(x_train, y_train, epochs=28, batch_size=512, validation_data=(x_val, y_val), verbose=1)

results = model.evaluate(test_data, test_labels)

print(results)

Your X_train data has just one row.

The below two lines must be changed:

x_train = train_data[10000:]

y_train = train_labels[10000:]

Also, you should change your validation data to something that the model is not trained upon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM