【发布时间】:2021-10-26 14:19:39
【问题描述】:
我正在尝试运行 TensorFlow 文本生成 RNN 示例,
https://github.com/tensorflow/text/blob/master/docs/tutorials/text_generation.ipynb
在安装了 TensorFlow 2.6 的本地 Windows 计算机上运行。
我能够成功地运行并训练 RNN 模型。我得到一个“张量”对象没有属性“numpy”错误但添加了,
tf.compat.v1.enable_eager_execution()
这解决了它。
但现在尝试用一些文本测试模型我得到了错误,
Invalid argument: indices[1] = [0] is repeated
这发生在 OneStep 函数内的tf.sparse.to_dense。
class OneStep(tf.keras.Model):
def __init__(self, model, chars_from_ids, ids_from_chars, temperature=1.0):
super().__init__()
self.temperature = temperature
self.model = model
self.chars_from_ids = chars_from_ids
self.ids_from_chars = ids_from_chars
print(len(ids_from_chars.get_vocabulary()))
# Create a mask to prevent "[UNK]" from being generated.
skip_ids = self.ids_from_chars(['[UNK]'])[:, None]
sparse_mask = tf.SparseTensor(
# Put a -inf at each bad index.
values=[-float('inf')]*len(skip_ids),
indices=skip_ids,
# Match the shape to the vocabulary
dense_shape=[len(ids_from_chars.get_vocabulary())])
print(sparse_mask)
self.prediction_mask = tf.sparse.to_dense(sparse_mask)
我添加了一些调试来打印 ids_from_chars
76
SparseTensor(indices=tf.Tensor(
[[0]
[0]], shape=(2, 1), dtype=int64), values=tf.Tensor([-inf -inf], shape=(2,), dtype=float32), dense_shape=tf.Tensor([76], shape=(1,), dtype=int64))
2021-08-25 15:28:23.935194: W tensorflow/core/framework/op_kernel.cc:1692] OP_REQUIRES failed at sparse_to_dense_op.cc:162 : Invalid argument: indices[1] = [0] is repeated
Traceback (most recent call last):
File "app.py", line 1041, in test_nlp_text_generation
result = text_generation.predictionfunction(text, analytic_id)
File "D:\Projects\python-run-2\text_generation.py", line 238, in predictionfunction
one_step_model = OneStep(model, chars_from_ids, ids_from_chars)
File "D:\Projects\python-run-2\text_generation.py", line 166, in __init__
self.prediction_mask = tf.sparse.to_dense(sparse_mask)
File "D:\Users\james\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\sparse_ops.py", line 1721, in sparse_tensor_to_dense
name=name)
File "D:\Users\james\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\gen_sparse_ops.py", line 3161, in sparse_to_dense
_ops.raise_from_not_ok_status(e, name)
File "D:\Users\james\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 6941, in raise_from_not_ok_status
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: indices[1] = [0] is repeated [Op:SparseToDense]
此外,我之前在我的计算机上运行过这个示例。刚刚重新安装了 TensorFlow,并再次从头开始尝试演示。
知道是什么导致了这个错误,或者如何解决它?
【问题讨论】:
标签: python tensorflow tensorflow2.0 recurrent-neural-network