【问题标题】:InvalidArgumentError Sentiment analyser with kerasInvalidArgumentError 使用 keras 进行情绪分析
【发布时间】:2018-08-23 10:15:03
【问题描述】:

我使用 Keras 作为二元分类问题构建了一个情感分析器。我正在使用使用 GRU 的 Imdb 数据集。 我的代码是:

# coding=utf-8
# ==========
#   MODEL
# ==========

# imports
from __future__ import print_function
from timeit import default_timer as timer
from datetime import timedelta
from keras.models import Sequential
from keras.preprocessing import sequence
from keras import regularizers
from keras.layers import Dense, Embedding
from keras.layers import GRU, LeakyReLU, Bidirectional
from keras.datasets import imdb

#start a timer
start = timer()

# Hyperparameters
Model_Name = 'my_model.h5'
vocab_size = 5000
maxlen = 1000
batch_size = 512
hidden_layer_size = 2
test_split = 0.3
dropout = 0.1
num_epochs = 1
alpha = 0.2
validation_split = 0.25
l1 = 0.01
l2 = 0.01

# Dataset loading
print('Loading data...')
(x_train, y_train), (x_test, y_test) = imdb.load_data(path="imdb.npz",
                                                      maxlen=maxlen)

print(len(x_train), 'train sequences')
print(len(x_test), 'test sequences')

# Data preprocessing
# Sequence padding
print('Pad sequences (samples x time)')
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)

# Network building
print('Build model...')
model = Sequential()
model.add(Embedding(vocab_size, hidden_layer_size))
model.add(Bidirectional(GRU(hidden_layer_size, kernel_initializer='uniform', kernel_regularizer=regularizers.l1_l2(l1=l1,l2=l2), dropout=dropout, recurrent_dropout=dropout,return_sequences=True)))
model.add(LeakyReLU())
model.add(Bidirectional(GRU(hidden_layer_size, kernel_initializer='uniform', dropout=dropout, kernel_regularizer=regularizers.l1_l2(l1=l1,l2=l2), recurrent_dropout=dropout)))
model.add(LeakyReLU())
model.add(Dense(1, activation='softmax', kernel_initializer='uniform', kernel_regularizer=regularizers.l1_l2(l1=l1,l2=l2)))

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

print('Train...')

# Fit the model
history = model.fit(x_train, y_train, batch_size=batch_size, epochs=num_epochs, validation_split=validation_split)
score, acc = model.evaluate(x_test, y_test, batch_size=batch_size, verbose=1)

# Create a summary, a plot and print the scores of the model
model.summary()

print('Test score:', score)
print('Test accuracy:', acc)

# Save model architecture, weights, training configuration (loss,optimizer),
# and also the state of the optimizer, so you can resume where you stopped
model.save(Model_Name)
end = timer()
print('Running time:  ' + str(timedelta(seconds=(end - start))) + '  in Hours:Minutes:Seconds')

我不断收到我不完全理解的错误消息:

InvalidArgumentError (see above for traceback): indices[502,665] = 5476 is not in [0, 5000)
[[Node: embedding_1/Gather = Gather[Tindices=DT_INT32, Tparams=DT_FLOAT, validate_indices=true, _device=/job:localhost/replica:0/task:0/device:CPU:0](embedding_1/embeddings/read, embedding_1/Cast)]]

谁能帮助我了解导致此错误的原因以及如何解决?

【问题讨论】:

    标签: python-3.x keras deep-learning sentiment-analysis word-embedding


    【解决方案1】:

    该错误抱怨不存在的单词索引。那是因为您只是限制了 Emedding 特征的数量(即有一个索引为 5476 的单词不在[0, 5000) 范围内,其中 5000 指的是您设置的vocab_size)。要解决此问题,您还需要将 vocab_size 作为 num_words 参数传递给 load_data 函数,如下所示:

    ... = imdb.load_data(num_words=vocab_size, ...)
    

    通过这种方式,您将单词限制为最常用的单词(即数据集中出现频率最高的 vocab_size 单词),其索引范围为 [0, vocab_size)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-29
      • 2015-09-23
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 2022-01-10
      相关资源
      最近更新 更多