相关疑难解决方法(0)

如何在张量流中将“张量”转换为“numpy”数组?

我试图在 tesnorflow2.0 版本中将张量转换为 numpy。由于 tf2.0 启用了急切执行,因此它应该默认工作并且在正常运行时也工作。当我在 tf.data.Dataset API 中执行代码时,它给出了一个错误

“AttributeError: 'Tensor' 对象没有属性 'numpy'”

我在 tensorflow 变量之后尝试了“.numpy()”,而对于“.eval()”,我无法获得默认会话。

from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
# tf.executing_eagerly()
import os
import time
import matplotlib.pyplot as plt
from IPython.display import clear_output
from model.utils import  get_noise
import cv2


def random_noise(input_image):
  img_out = get_noise(input_image)
  return img_out


def load_denoising(image_file):
  image = tf.io.read_file(image_file)
  image = tf.image.decode_png(image)
  real_image = image
  input_image = random_noise(image.numpy())
  input_image = tf.cast(input_image, tf.float32)
  real_image = tf.cast(real_image, tf.float32)
  return input_image, real_image


def …
Run Code Online (Sandbox Code Playgroud)

python tensorflow tensorflow-datasets tensorflow2.0

7
推荐指数
1
解决办法
9329
查看次数

AttributeError: 'Tensor' object has no attribute 'numpy' in Tensorflow 2.1

I am trying to convert the shape property of a Tensor in Tensorflow 2.1 and I get this error:

AttributeError: 'Tensor' object has no attribute 'numpy'
Run Code Online (Sandbox Code Playgroud)

I already checked that the output of tf.executing eagerly() is True,

A bit of context: I load a tf.data.Dataset from a TFRecords, then I apply a map. The maping function is trying to convert the shape property of one of the dataset sample Tensor to numpy:

def _parse_and_decode(serialized_example):
    """ parse and decode …
Run Code Online (Sandbox Code Playgroud)

python numpy tensorflow tensorflow2.x

6
推荐指数
1
解决办法
7357
查看次数