【问题标题】:"Function call stack: keras_scratch_graph" - Glove embedded text sentiment analysis《函数调用栈:keras_scratch_graph》——手套嵌入文本情感分析
【发布时间】:2020-05-15 21:43:32
【问题描述】:

在 Jupyter 笔记本中运行的相对简单的代码块(因为 Google Colab 中的 I cannot for the life of me get it working)但在运行模型时出现以下错误。链接的帖子显示了我正在使用的输入的更多细节。

我认为这可能是与 GPU 相关的错误。我遵循了几个类似标题的帖子和 CUDA/Visual Studio 安装教程,但无济于事。

import gensim
import os
from gensim.models import KeyedVectors
from gensim.test.utils import datapath, get_tmpfile
from gensim.scripts.glove2word2vec import glove2word2vec

glove_file = datapath('glove.6B.50d.txt')

word2vec_glove_file = get_tmpfile("glove.6B.50d.word2vec.txt")
glove2word2vec(glove_file, word2vec_glove_file)

model = KeyedVectors.load_word2vec_format(word2vec_glove_file, binary=False)

# get the vector for each word in the glove file

emb_dict = {}
glove = open(glove_file, encoding="utf8")
for line in glove:
    values = line.split()
    word = values[0]
    vector = numpy.asarray(values[1:], dtype='float32')
    emb_dict[word] = vector
glove.close()

# get the top 10k words in the tweets, and find their vector from the glove files

num_words = 10000
glove_dims = 50

emb_matrix = numpy.zeros((num_words, glove_dims))
for w, i in token.word_index.items():
    if i < num_words:
        vect = emb_dict.get(w)
        if vect is not None:
            emb_matrix[i] = vect
    else:
        break

from keras import models, layers
from keras.layers import Embedding, Flatten, Dense

glove_model = models.Sequential()
glove_model.add(Embedding(num_words, glove_dims, input_length=max_tweet_len))
glove_model.add(layers.LSTM(100))
#glove_model.add(Flatten())
glove_model.add(Dense(3, activation='softmax'))

glove_model.layers[0].set_weights([emb_matrix])
glove_model.layers[0].trainable = False

glove_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

history = glove_model.fit(train_seq_x, y_train, epochs=10, batch_size=64)

返回的错误是:

Epoch 1/10
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-11-224ee1a00f0e> in <module>
     13 glove_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
     14 
---> 15 history = glove_model.fit(train_seq_x, y_train, epochs=10, batch_size=64)

~\anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
   1237                                         steps_per_epoch=steps_per_epoch,
   1238                                         validation_steps=validation_steps,
-> 1239                                         validation_freq=validation_freq)
   1240 
   1241     def evaluate(self,

~\anaconda3\lib\site-packages\keras\engine\training_arrays.py in fit_loop(model, fit_function, fit_inputs, out_labels, batch_size, epochs, verbose, callbacks, val_function, val_inputs, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq)
    194                     ins_batch[i] = ins_batch[i].toarray()
    195 
--> 196                 outs = fit_function(ins_batch)
    197                 outs = to_list(outs)
    198                 for l, o in zip(out_labels, outs):

~\anaconda3\lib\site-packages\tensorflow\python\keras\backend.py in __call__(self, inputs)
   3790         value = math_ops.cast(value, tensor.dtype)
   3791       converted_inputs.append(value)
-> 3792     outputs = self._graph_fn(*converted_inputs)
   3793 
   3794     # EagerTensor.numpy() will often make a copy to ensure memory safety.

~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in __call__(self, *args, **kwargs)
   1603       TypeError: For invalid positional/keyword argument combinations.
   1604     """
-> 1605     return self._call_impl(args, kwargs)
   1606 
   1607   def _call_impl(self, args, kwargs, cancellation_manager=None):

~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _call_impl(self, args, kwargs, cancellation_manager)
   1643       raise TypeError("Keyword arguments {} unknown. Expected {}.".format(
   1644           list(kwargs.keys()), list(self._arg_keywords)))
-> 1645     return self._call_flat(args, self.captured_inputs, cancellation_manager)
   1646 
   1647   def _filtered_call(self, args, kwargs):

~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
   1744       # No tape is watching; skip to running the function.
   1745       return self._build_call_outputs(self._inference_function.call(
-> 1746           ctx, args, cancellation_manager=cancellation_manager))
   1747     forward_backward = self._select_forward_and_backward_functions(
   1748         args,

~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in call(self, ctx, args, cancellation_manager)
    596               inputs=args,
    597               attrs=attrs,
--> 598               ctx=ctx)
    599         else:
    600           outputs = execute.execute_with_cancellation(

~\anaconda3\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError:  indices[40,19] = 11263 is not in [0, 10000)
     [[node embedding_1/embedding_lookup (defined at C:\Users\Jorda\anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_1707]

Function call stack:
keras_scratch_graph

按照cmets的要求,分词方法如下:

from keras.preprocessing import text, sequence

# create a tokenizer 
token = text.Tokenizer()
token.fit_on_texts(tweets)
word_index = token.word_index

max_tweet_len = 30

# convert text to sequence of tokens and pad them to ensure equal length vectors 

train_seq_x = sequence.pad_sequences(token.texts_to_sequences(x_train), maxlen=max_tweet_len)
test_seq_x = sequence.pad_sequences(token.texts_to_sequences(x_test), maxlen=max_tweet_len)

print(train_seq_x)
print(test_seq_x)

【问题讨论】:

  • 当你标记你的文本时,你必须抑制(用 0 或填充标记替换)所有不在词汇表中的单词(所有索引 >10000 的单词)
  • 感谢您回复我!当我用emb_matrix = numpy.zeros((num_words, glove_dims)) 实例化矩阵时,这不是默认情况下完成的吗?即如果超过 10k 则中断将使向量为零?
  • 不,emb矩阵只是权重矩阵...报告您构建train_seq_x的代码段
  • Gotcha - 对造成的误解表示歉意。我刚刚更新了帖子以包含此块。
  • 为了确认映射工作正常,我在 emb_matrix 构造循环中添加了两个打印调用:一个打印单词w,另一个打印vect。给定单词的 vect 值与原始 glove.6B.50d.txt 文件中的值匹配。你指的是这个吗?

标签: python tensorflow keras gpu


【解决方案1】:

尝试以这种方式适合您的分词器

num_words = 10000
token = text.Tokenizer(num_words=num_words)

当您定义模型时,将num_words 更改为Embedding 层中的word_index+1

【讨论】:

  • 你很高兴。非常感谢。
猜你喜欢
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 2015-05-24
  • 2018-11-27
  • 2014-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多