将张量转换为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][:,:].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,name="input_mask")
    input_type_ids = tf.keras.Input(shape=(max_len,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_type_ids])[0]
    bert_encoder_output = (embedding[:,:])
    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,:].numpy()
    
    if (printInputs):
        print (model_inputs)
        print (features)
        
    return (model_inputs)    

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...