今天爱分享给大家带来Tensor is unhashable if Tensor equality is enabled【解决办法】,希望能够帮助到大家。
具体的报错信息如下:Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.
报错地方的代码如下,使用的tensorflow版本为2.0
self.best_model = tf.keras.models.load_model(self.save_model_path)
batch_logits = self.best_model(tensorized_sents)
batch_tagids = tf.argmax(batch_logits, axis=2) # [[[ ]]]
# 将id转化为标注
pred_tag_lists = []
id2tag = dict((id_, tag) for tag, id_ in tag2id.items())
for i, ids in enumerate(batch_tagids):
tag_list = []
if self.crf:
for j in range(lengths[i] - 1): # crf解码过程中,end被舍弃
tag_list.append(id2tag[ids[j]])
else:
for j in range(lengths[i]):
tag_list.append(id2tag[ids[j]]) # 报错的地方在这里
pred_tag_lists.append(tag_list)
报错的地方在遍历tensor并利用id2tag进行还原标签处;怀疑是因为tensor不可以使用下标去遍历的原因,所以需要将tensor转换为array数组
所以在batch_tagids的地方将其转换为array,代码如下:
batch_tagids = tf.argmax(batch_logits, axis=2).numpy()
原文链接:http://blog.itblood.com/1854.html,转载请注明出处。

![神作更新[SLG/3D/官中/动态/萝莉/步兵/中出/更新]死宅与天使与淫荡之家/死宅、天使、和甜蜜的家庭[Ver1.44D&F&H+提取动画][PC]](/wp-content/uploads/replace/2025/07/19/6eb083ddb8163874073e9ecc3503c3ea.webp)