TensorFlow Tensor在numpy argmax与keras argmax中的处理方式不同

时间:2022-01-10 14:03:19

Why does a TensorFlow tensor behave differently in math functions in Numpy than it behaves in math functions in Keras?

为什么TensorFlow张量在Numpy中的数学函数中的行为与在Keras中的数学函数中的行为不同?

Numpy arrays seem to act normally when put in the same situation as the TensorFlow Tensor.

当与TensorFlow Tensor处于相同的情况时,Numpy数组似乎正常运行。

This example shows that a numpy matrix is handled correctly under numpy functions and keras functions.

这个例子表明在numpy函数和keras函数下正确处理numpy矩阵。

import numpy as np
from keras import backend as K

arr = np.random.rand(19, 19, 5, 80)

np_argmax = np.argmax(arr, axis=-1)
np_max = np.max(arr, axis=-1)

k_argmax = K.argmax(arr, axis=-1)
k_max = K.max(arr, axis=-1)

print('np_argmax shape: ', np_argmax.shape)
print('np_max shape: ', np_max.shape)
print('k_argmax shape: ', k_argmax.shape)
print('k_max shape: ', k_max.shape)

Outputs this (as expected)

输出(如预期)

np_argmax shape:  (19, 19, 5)
np_max shape:  (19, 19, 5)
k_argmax shape:  (19, 19, 5)
k_max shape:  (19, 19, 5)

As opposed to this example

与此示例相反

import numpy as np
from keras import backend as K
import tensorflow as tf

arr = tf.constant(np.random.rand(19, 19, 5, 80))

np_argmax = np.argmax(arr, axis=-1)
np_max = np.max(arr, axis=-1)

k_argmax = K.argmax(arr, axis=-1)
k_max = K.max(arr, axis=-1)

print('np_argmax shape: ', np_argmax.shape)
print('np_max shape: ', np_max.shape)
print('k_argmax shape: ', k_argmax.shape)
print('k_max shape: ', k_max.shape)

which outputs

np_argmax shape:  ()
np_max shape:  (19, 19, 5, 80)
k_argmax shape:  (19, 19, 5)
k_max shape:  (19, 19, 5)

2 个解决方案

#1


2  

You need to execute/run code (say under a TF session) to have tensors evaluated. Until then, the shapes of tensors are not evaluated.

您需要执行/运行代码(例如在TF会话下)以评估张量。在此之前,不评估张量的形状。

TF docs say:

TF文档说:

Each element in the Tensor has the same data type, and the data type is always known. The shape (that is, the number of dimensions it has and the size of each dimension) might be only partially known. Most operations produce tensors of fully-known shapes if the shapes of their inputs are also fully known, but in some cases it's only possible to find the shape of a tensor at graph execution time.

Tensor中的每个元素都具有相同的数据类型,并且数据类型始终是已知的。形状(即,它具有的尺寸数量和每个尺寸的大小)可能只是部分已知。如果输入的形状也是完全已知的,大多数操作都会生成已知形状的张量,但在某些情况下,只能在图形执行时找到张量的形状。

#2


1  

Why don't you try the following code for the 2nd example:

为什么不为第二个示例尝试以下代码:

import numpy as np
from keras import backend as K
import tensorflow as tf

arr = tf.constant(np.random.rand(19, 19, 5, 80))
with tf.Session() as sess:
    arr = sess.run(arr)

np_argmax = np.argmax(arr, axis=-1)
np_max = np.max(arr, axis=-1)

k_argmax = K.argmax(arr, axis=-1)
k_max = K.max(arr, axis=-1)

print('np_argmax shape: ', np_argmax.shape)
print('np_max shape: ', np_max.shape)
print('k_argmax shape: ', k_argmax.shape)
print('k_max shape: ', k_max.shape)

After arr = tf.constant(np.random.rand(19, 19, 5, 80)), the type of arr is tf.Tensor, but after running arr = sess.run(arr) its type will be changed to numpy.ndarray.

在arr = tf.constant(np.random.rand(19,19,5,80))之后,arr的类型是tf.Tensor,但在运行arr = sess.run(arr)之后,其类型将更改为numpy .ndarray。

#1


2  

You need to execute/run code (say under a TF session) to have tensors evaluated. Until then, the shapes of tensors are not evaluated.

您需要执行/运行代码(例如在TF会话下)以评估张量。在此之前,不评估张量的形状。

TF docs say:

TF文档说:

Each element in the Tensor has the same data type, and the data type is always known. The shape (that is, the number of dimensions it has and the size of each dimension) might be only partially known. Most operations produce tensors of fully-known shapes if the shapes of their inputs are also fully known, but in some cases it's only possible to find the shape of a tensor at graph execution time.

Tensor中的每个元素都具有相同的数据类型,并且数据类型始终是已知的。形状(即,它具有的尺寸数量和每个尺寸的大小)可能只是部分已知。如果输入的形状也是完全已知的,大多数操作都会生成已知形状的张量,但在某些情况下,只能在图形执行时找到张量的形状。

#2


1  

Why don't you try the following code for the 2nd example:

为什么不为第二个示例尝试以下代码:

import numpy as np
from keras import backend as K
import tensorflow as tf

arr = tf.constant(np.random.rand(19, 19, 5, 80))
with tf.Session() as sess:
    arr = sess.run(arr)

np_argmax = np.argmax(arr, axis=-1)
np_max = np.max(arr, axis=-1)

k_argmax = K.argmax(arr, axis=-1)
k_max = K.max(arr, axis=-1)

print('np_argmax shape: ', np_argmax.shape)
print('np_max shape: ', np_max.shape)
print('k_argmax shape: ', k_argmax.shape)
print('k_max shape: ', k_max.shape)

After arr = tf.constant(np.random.rand(19, 19, 5, 80)), the type of arr is tf.Tensor, but after running arr = sess.run(arr) its type will be changed to numpy.ndarray.

在arr = tf.constant(np.random.rand(19,19,5,80))之后,arr的类型是tf.Tensor,但在运行arr = sess.run(arr)之后,其类型将更改为numpy .ndarray。