[英]TF2.0: Transformer model ValueError: Shapes (40759, 128) and (40765, 128) are incompatible
The Transformer model has the following params.变压器 model 具有以下参数。 I saved and reloaded the model using h5py.
我使用 h5py 保存并重新加载了 model。 I get this errors only for few datasets.
我只在少数数据集上得到这个错误。
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])
The hyperparameters to train the model are:训练 model 的超参数是:
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)
Once I reload the model, I get the following error.重新加载 model 后,出现以下错误。 I could save all the parameters, but load fails with incompatibility error.
我可以保存所有参数,但加载失败并出现不兼容错误。 What do those tensor shapes indicate?
这些张量形状表示什么?
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (40759, 128) and (40765, 128) are incompatible
Traceback:追溯:
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
The problem was with loading the tokenizers.问题在于加载标记器。 There were punctuations which were getting saved while using the save function, but on load, some of these failed to load (don't know the root cause) and that caused the mismatch in shapes.
使用保存 function 时保存了一些标点符号,但在加载时,其中一些无法加载(不知道根本原因),导致形状不匹配。 Once I cleaned the text to remove punctuation and extra spaces, it worked fine.
一旦我清理了文本以删除标点符号和多余的空格,它就可以正常工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.