Tensorflow2.0
程序员文章站
2022-04-02 11:05:34
Tensorflow 2.3: AttributeError: ‘Tensor’ object has no attribute ‘numpy’使用tf.py_function()来重构一下函数def masked_sparse_categorical_crossentropy(y_true, y_pred): """ :param y_true: (batch_size, seq_len, 2) dtype=float32 contains word index and word m...
Tensorflow 2.3: AttributeError: ‘Tensor’ object has no attribute ‘numpy’
使用tf.py_function()来重构一下函数
def masked_sparse_categorical_crossentropy(y_true, y_pred):
"""
:param y_true: (batch_size, seq_len, 2) dtype=float32 contains word index and word mask
:param y_pred: (batch_size, seq_len, vocab_size) dtype=float32
:param sample_weight: default None, why use this ? compatible to the standard loss
:return: Penalized loss
"""
y_true_val = y_true # [:, :, 0]
np.set_printoptions(suppress=True)
def pr(x, y):
print(np.max(x.numpy(), axis=-1))
print(np.argmax(x.numpy(), axis=-1))
print(y.numpy())
with open(r'/6T_data/anli/result_neg', 'a', encoding='utf-8') as f:
f.write(str(np.max(x.numpy())))
return x, y
# print(y_pred.numpy())
tf.py_function(pr, [y_pred, y_true], [tf.float32, tf.int32])
# get mask info
padding_mask = tf.cast(tf.not_equal(y_true_val, 0), dtype=tf.float32)
参考:https://*.com/questions/63557955/tensorflow-2-3-attributeerror-tensor-object-has-no-attribute-numpy
本文地址:https://blog.csdn.net/andy123564/article/details/110878970
上一篇: Python3 每行将多个元素/数字写入txt 按行写入txt
下一篇: 如何解决“sbt.ResolveException:未解决的依赖关系:commons-logging#commons-logging; 1.1.1:”exception?
推荐阅读
-
tensorflow2.0之keras实现卷积神经网络
-
TensorFlow2.0:张量的合并与分割实例
-
计算机视觉(python)tensorflow2.0框架下CNN做图像分类
-
tensorflow2.0的函数签名与图结构(推荐)
-
TensorFlow2.0怎么实现K_Means
-
rk3399pro-EVB安装RKNN-Toolkit1.6 Tensorflow2.0 Pytorch1.6
-
TensorFlow2.0(10):加载自定义图片数据集到Dataset
-
TensorFlow2.0:张量的合并与分割实例
-
记录二:tensorflow2.0写MNIST手写体
-
tensorflow2.0之多层感知机进行二分类