numpy - 将张量转换为 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)
解决方案
推荐阅读
- python - 将两个或更多参数传递给 Oracle SQL
- javascript - Discord 机器人回复每次增加 1
- python - 在python中打印字典值和键(索引)
- codeigniter-4 - 在 ci4 中删除 index.php 但没有任何反应
- php - PHP jQuery Tabledit 选择选项
- python - 刮问题。为什么没有数据被抓取?
- javascript - 为什么我的 SCSS 文件在导入后没有应用到我的 React 中?
- flutter - Flutter 和 Dart 扩展
- kotlin - 在 Kotlin 中正确使用 Number 类
- android - 我在E盘安装了flutter,但在C盘安装了android studio。它会起作用吗?