解决tf2 : AttributeError: 'Tensor' object has no attribute 'numpy'

问题描述:

环境:tf2.0
查看代码运行中tensor的值,

def create_mask(inputs, targets):
    input_leave_fun('create_mask',1)
    # 编码器只有padding_mask
    print("查看输入")
    print(type(inputs))

    print(inputs.numpy())		# 此处报错

解决方法

注释@tf.function

#@tf.function  注释此处
def train_step(inputs, targets):
    input_leave_fun('train_step',1)
    tar_inp = targets[:, :-1]
    tar_real = targets[:, 1:]
    print('tar_inp.shape {} tar_real.shape{}'.format(tar_inp.shape, tar_real.shape))
    # 构造mask,调用函数处
    encoder_padding_mask, look_ahead_mask, decoder_padding_mask = create_mask(inputs, tar_inp)

    with tf.GradientTape() as tape:
        pred, _ = transformer(inputs,
                              tar_inp,
                              True,
                              encoder_padding_mask,
                              look_ahead_mask,
                              decoder_padding_mask)
        loss = loss_fun(tar_real, pred)

        print("执行退出")
        sys.exit(0)

你可能感兴趣的:(笔记)