“张量”对象没有属性“分离”

Rab*_*ifa 5 numpy tensorflow huggingface-transformers

我正在使用阿拉伯语 Bert 并批量传递我的训练数据集,我收到的错误消息是 Tensor 不能被视为 numpy 数组,但无法使用 detach().numpy() 分离到 numpy

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:571 train_function  *
    outputs = self.distribute_strategy.run(
<ipython-input-9-271b41658d5b>:46 call  *
    x = self.embed_with_bert(inputs)
<ipython-input-9-271b41658d5b>:39 embed_with_bert  *
    embds = self.bert_layer(all_tokens[:,0,:].detach().numpy(),

AttributeError: 'Tensor' object has no attribute 'detach'
Run Code Online (Sandbox Code Playgroud)

当我将标记生成器更改为“tf”时也是如此

我收到以下错误:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:571 train_function  *
    outputs = self.distribute_strategy.run(
<ipython-input-17-b2127b5212bc>:46 call  *
    x = self.embed_with_bert(inputs)
<ipython-input-53-b99c90611f94>:39 embed_with_bert  *
    embds = self.bert_layer([all_tokens[:,0,:],
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py:550 __call__  *
    result = self.forward(*input, **kwargs)
/usr/local/lib/python3.6/dist-packages/transformers/modeling_bert.py:691 forward  *
    input_shape = input_ids.size()

AttributeError: 'list' object has no attribute 'size'
Run Code Online (Sandbox Code Playgroud)

也尝试过这个

embds = self.bert_layer(tf.unstack(all_tokens[:,0,:]),
                            tf.unstack(all_tokens[:,1,:]),
                            tf.unstack(all_tokens[:,2,:])) 
Run Code Online (Sandbox Code Playgroud)

但它生成了:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:571 train_function  *
    outputs = self.distribute_strategy.run(
<ipython-input-64-e1ff853b33fb>:55 call  *
    x = self.embed_with_bert(inputs)
<ipython-input-29-d3665857b399>:44 embed_with_bert  *
    embds = self.bert_layer(tf.unstack(all_tokens[:,0,:]),tf.unstack(all_tokens[:,1,:]),tf.unstack(all_tokens[:,2,:])) #[:,0,:],all_tokens[:,1,:],all_tokens[:,2,:])
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py:1510 unstack  **
    raise ValueError("Cannot infer num from shape %s" % value_shape)

ValueError: Cannot infer num from shape (None, None)
Run Code Online (Sandbox Code Playgroud)

示例: --> all_tokens 是作为批次发送到模型的张量,但现在我想将其用作 numpy 数组来修复错误?

embds = self.bert_layer(all_tokens[:,0,:],
                               all_tokens[:,1,:],
                               all_tokens[:,2,:])
Run Code Online (Sandbox Code Playgroud)

尽管:

这项工作很好[t是从hugging-face的分词器生成的:

a = bert_layer(t["input_ids"], t["attention_mask"], t["token_type_ids"])
Run Code Online (Sandbox Code Playgroud)