【bert4keras】:AttributeError: ‘PaddedBatchDataset‘ object has no attribute ‘ndim‘

【bert4keras】:AttributeError: ‘PaddedBatchDataset‘ object has no attribute ‘ndim‘,第1张

问题

在使用bert4keras的时候遇到一个问题:

AttributeError: 'PaddedBatchDataset' object has no attribute 'ndim'
解决

花了好长时间定位问题,原本以为是keras版本问题,后来才从bert4keras源码看到,作者其实是设置了在import keras之前会判断环境变量中的TF_KERAS,但是我在写代码之前会将这个设置放在main中,导致其实使用的还是keras而不是tf.keras


import os

import json
import math
import random
import numpy as np
import tensorflow as tf
from bert4keras.backend import keras, K # 这里先引用了keras
from bert4keras.layers import Loss
from bert4keras.models import build_transformer_model
from bert4keras.tokenizers import Tokenizer
from bert4keras.optimizers import Adam
from bert4keras.optimizers import extend_with_weight_decay
from bert4keras.optimizers import extend_with_piecewise_linear_lr
from bert4keras.optimizers import extend_with_gradient_accumulation
from bert4keras.snippets import sequence_padding, open
from bert4keras.snippets import DataGenerator
from bert4keras.snippets import text_segmentate
import jieba_fast as jieba
...

if __name__ == '__main__':
	os.environ['TF_KERAS'] = '1'  # 必须使用tf.keras,注意,这里要放在引用keras之前

改为:


import os

import json
import math
import random
import numpy as np
import tensorflow as tf
os.environ['TF_KERAS'] = '1'  # 必须使用tf.keras,注意,这里要放在引用keras之前
from bert4keras.backend import keras, K # 这里先引用了keras
from bert4keras.layers import Loss
from bert4keras.models import build_transformer_model
from bert4keras.tokenizers import Tokenizer
from bert4keras.optimizers import Adam
from bert4keras.optimizers import extend_with_weight_decay
from bert4keras.optimizers import extend_with_piecewise_linear_lr
from bert4keras.optimizers import extend_with_gradient_accumulation
from bert4keras.snippets import sequence_padding, open
from bert4keras.snippets import DataGenerator
from bert4keras.snippets import text_segmentate
import jieba_fast as jieba
...

if __name__ == '__main__':
	...

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/943727.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-18
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存