简体   繁体   English

TF2.0:变压器 model ValueError:形状 (40759, 128) 和 (40765, 128) 不兼容

[英]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.

相关问题 CNN ValueError:形状 (10, 4) 和 (10, 128, 128, 4) 不兼容 - CNN ValueError: Shapes (10, 4) and (10, 128, 128, 4) are incompatible 不兼容的形状:[128,1] 与 [128,3,3] - Incompatible shapes: [128,1] vs. [128,3,3] TF2.0 中的 saved_model.prune() - saved_model.prune() in TF2.0 ValueError:未找到 SavedModel 包。 尝试将 TF2.0 模型部署到 SageMaker 时 - ValueError: no SavedModel bundles found! when trying to deploy a TF2.0 model to SageMaker 如何在 Swift 中运行 tf2.0 model - How to run a tf2.0 model in Swift InvalidArgumentError:不兼容的形状:[32,128] 与 [128,128] [Op:BroadcastTo] - InvalidArgumentError: Incompatible shapes: [32,128] vs. [128,128] [Op:BroadcastTo] ValueError:“顺序”层的输入 0 与该层不兼容:预期形状=(无,128,128,3),找到形状=(32,128,3) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 128, 128, 3), found shape=(32, 128, 3) ValueError:LSTM 模型中的形状不兼容 - ValueError: Shapes are incompatible in LSTM model Tensorflow + Keras训练:InvalidArgumentError:不兼容的形状:[7,128,2,2] vs [7,128,3,3] - Tensorflow + Keras training: InvalidArgumentError: Incompatible shapes: [7,128,2,2] vs [7,128,3,3] 将 tf1.x saved_model.pb 重新保存到新的 tf2.0 saved_model.pb - resave tf1.x saved_model.pb into new tf2.0 saved_model.pb
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM