如何解决将张量转换为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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。