TF 2.0 打印张量值
TF 2.0 print tensor values
我正在学习最新版本的 Tensorflow (2.0),我已经尝试 运行 一个简单的代码来分割矩阵。
使用装饰器 @tf.function 我做了以下 class:
class Data:
def __init__(self):
pass
def back_to_zero(self, input):
time = tf.slice(input, [0,0], [-1,1])
new_time = time - time[0][0]
return new_time
@tf.function
def load_data(self, inputs):
new_x = self.back_to_zero(inputs)
print(new_x)
因此,当 运行 使用 numpy 矩阵的代码时,我无法检索数字。
time = np.linspace(0,10,20)
magntiudes = np.random.normal(0,1,size=20)
x = np.vstack([time, magntiudes]).T
d = Data()
d.load_data(x)
输出:
Tensor("sub:0", shape=(20, 1), dtype=float64)
我需要以 numpy 格式获取此张量,但 TF 2.0 没有 class tf.Session 来使用 运行() 或 eval() 方法。
感谢您为我提供的任何帮助!
在装饰器 @tf.function
指示的图形中,您可以使用 tf.print 打印张量的值。
tf.print(new_x)
这里是如何重写代码
class Data:
def __init__(self):
pass
def back_to_zero(self, input):
time = tf.slice(input, [0,0], [-1,1])
new_time = time - time[0][0]
return new_time
@tf.function
def load_data(self, inputs):
new_x = self.back_to_zero(inputs)
tf.print(new_x) # print inside the graph context
return new_x
time = np.linspace(0,10,20)
magntiudes = np.random.normal(0,1,size=20)
x = np.vstack([time, magntiudes]).T
d = Data()
data = d.load_data(x)
print(data) # print outside the graph context
tf.decorator
上下文之外的张量类型是 tensorflow.python.framework.ops.EagerTensor
类型。要将其转换为 numpy 数组,可以使用 data.numpy()
问题是您无法直接在图中获取张量的值。因此,您要么按照@edkeveked 的建议使用 tf.print
进行操作,要么按如下方式更改代码:
class Data:
def __init__(self):
pass
def back_to_zero(self, input):
time = tf.slice(input, [0,0], [-1,1])
new_time = time - time[0][0]
return new_time
@tf.function
def load_data(self, inputs):
new_x = self.back_to_zero(inputs)
return new_x
time = np.linspace(0,10,20)
magntiudes = np.random.normal(0,1,size=20)
x = np.vstack([time, magntiudes]).T
d = Data()
data = d.load_data(x)
print(data.numpy())
同样的问题,如何只打印张量的值?我已经失去了半天。缓慢但坚定地转向 Pytorch ...
我正在学习最新版本的 Tensorflow (2.0),我已经尝试 运行 一个简单的代码来分割矩阵。 使用装饰器 @tf.function 我做了以下 class:
class Data:
def __init__(self):
pass
def back_to_zero(self, input):
time = tf.slice(input, [0,0], [-1,1])
new_time = time - time[0][0]
return new_time
@tf.function
def load_data(self, inputs):
new_x = self.back_to_zero(inputs)
print(new_x)
因此,当 运行 使用 numpy 矩阵的代码时,我无法检索数字。
time = np.linspace(0,10,20)
magntiudes = np.random.normal(0,1,size=20)
x = np.vstack([time, magntiudes]).T
d = Data()
d.load_data(x)
输出:
Tensor("sub:0", shape=(20, 1), dtype=float64)
我需要以 numpy 格式获取此张量,但 TF 2.0 没有 class tf.Session 来使用 运行() 或 eval() 方法。
感谢您为我提供的任何帮助!
在装饰器 @tf.function
指示的图形中,您可以使用 tf.print 打印张量的值。
tf.print(new_x)
这里是如何重写代码
class Data:
def __init__(self):
pass
def back_to_zero(self, input):
time = tf.slice(input, [0,0], [-1,1])
new_time = time - time[0][0]
return new_time
@tf.function
def load_data(self, inputs):
new_x = self.back_to_zero(inputs)
tf.print(new_x) # print inside the graph context
return new_x
time = np.linspace(0,10,20)
magntiudes = np.random.normal(0,1,size=20)
x = np.vstack([time, magntiudes]).T
d = Data()
data = d.load_data(x)
print(data) # print outside the graph context
tf.decorator
上下文之外的张量类型是 tensorflow.python.framework.ops.EagerTensor
类型。要将其转换为 numpy 数组,可以使用 data.numpy()
问题是您无法直接在图中获取张量的值。因此,您要么按照@edkeveked 的建议使用 tf.print
进行操作,要么按如下方式更改代码:
class Data:
def __init__(self):
pass
def back_to_zero(self, input):
time = tf.slice(input, [0,0], [-1,1])
new_time = time - time[0][0]
return new_time
@tf.function
def load_data(self, inputs):
new_x = self.back_to_zero(inputs)
return new_x
time = np.linspace(0,10,20)
magntiudes = np.random.normal(0,1,size=20)
x = np.vstack([time, magntiudes]).T
d = Data()
data = d.load_data(x)
print(data.numpy())
同样的问题,如何只打印张量的值?我已经失去了半天。缓慢但坚定地转向 Pytorch ...