TF2.0:变压器模型ValueError:形状(40759、128)和(40765、128)不兼容

时间:2019-11-12 06:06:12

标签: python nlp tensorflow2.0

Transformer模型具有以下参数。我使用h5py保存并重新加载了模型。我仅在少数数据集上遇到此错误。

h5f = h5py.File(path + '.model.weights.h5', 'w')

# Weights reloaded

variables = []
    h5f = h5py.File(path + '.model.weights.h5', 'r')
    for idx in sorted([int(i) for i in h5f]):
        variables.append(np.array(h5f[str(idx)]))
    h5f.close()
    for idx, t in enumerate(this.model.trainable_variables):
        t.assign(variables[idx])

训练模型的超参数是:

BUFFER_SIZE = 20000
BATCH_SIZE = 64
MAX_LENGTH = 40
num_layers = 4
d_model = 128
dff = 512
num_heads = 8
input_vocab_size = tokenizer_pt.vocab_size + 2
target_vocab_size = tokenizer_en.vocab_size + 2
dropout_rate = 0.1

transformer = Transformer(num_layers, d_model, num_heads, dff,
                          input_vocab_size, target_vocab_size, 
                          pe_input=input_vocab_size, 
                          pe_target=target_vocab_size,
                          rate=dropout_rate)

重新加载模型后,出现以下错误。我可以保存所有参数,但是加载失败并出现不兼容错误。这些张量形状表示什么?

raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (40759, 128) and (40765, 128) are incompatible

跟踪:

File "/Users/Models/Model.py", line 400, in load
    t.assign(modelTrainables[idx])
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py", line 600, in assign
    self._shape.assert_is_compatible_with(value_tensor.shape)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/python/framework/tensor_shape.py", line 700, in assert_is_compatible_with
    raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (40759, 128) and (40765, 128) are incompatible

1 个答案:

答案 0 :(得分:0)

问题在于加载令牌生成器。使用保存功能时会保存一些标点符号,但是在加载时,其中一些标点无法加载(不知道根本原因),从而导致形状不匹配。一旦我清除了文字以删除标点符号和多余的空格,它就可以正常工作。