首页 > 解决方案 > 将张量转换为 numpy 二维数组

问题描述

from transformers import BertTokenizer, TFBertModel
import matplotlib.pyplot as plt
import tensorflow as tf

下面包含的代码会在该行引发错误:

features = bert_encoder([input_word_ids, input_mask, input_type_ids])[0][:,0,:].numpy()

错误是:

AttributeError: 'Tensor' object has no attribute 'numpy'

我在张量流版本 > 2.0 上运行它并tf.executing_eagerly()返回True

我在 numpy() 操作之前检索信息的字典项是:

{
bert_encoder_output: <tf.Tensor 'strided_slice:0' shape=(None, 768) dtype=float32>, 
embedding: <tf.Tensor 'tf_bert_model/Identity:0' shape=(None, 50, 768) dtype=float32>
}

TPU 会话设置:

try:
    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
    tf.config.experimental_connect_to_cluster(tpu)
    tf.tpu.experimental.initialize_tpu_system(tpu)
    strategy = tf.distribute.experimental.TPUStrategy(tpu)
except ValueError:
    strategy = tf.distribute.get_strategy() # for CPU and single GPU
    print('Number of replicas:', strategy.num_replicas_in_sync)

代码:

   from tensorflow import keras
from tensorflow.keras import layers
import tensorflow as tf

if (tf.executing_eagerly()):
    print ("Yes")
    
tf.compat.v1.enable_eager_execution() 

max_len = 50

def get_bert_encoder_output(printInputs = False):
    model_inputs = {}
    bert_encoder = TFBertModel.from_pretrained(model_name)
    # Get Inputs
    input_word_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
    input_mask = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
    input_type_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_type_ids")
    # last hidden-state - the model output - is the first element of the output tuple
    embedding = bert_encoder([input_word_ids, input_mask, input_type_ids])[0]
    bert_encoder_output = (embedding[:,0,:])
    model_inputs['input_word_ids'] = input_word_ids    
    model_inputs['input_mask'] = input_mask
    model_inputs['input_type_ids'] = input_type_ids
    model_inputs['bert_encoder_output'] = bert_encoder_output
    model_inputs['embedding'] = embedding
    if (tf.executing_eagerly()):
     print ("Inside get_bert_encoder_output - Yes executing eagerly")
    features = bert_encoder([input_word_ids, input_mask, input_type_ids])[0][:,0,:].numpy()
    
    if (printInputs):
        print (model_inputs)
        print (features)
        
    return (model_inputs)    

在此处输入图像描述

标签: numpytensorflowbert-language-modeltransformer

解决方案


推荐阅读