在使用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__':
...
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)