Python 在numpy argmax和keras argmax中,TensorFlow张量的处理方式不同
为什么一个张量流张量在Numpy的数学函数中的行为与在Keras的数学函数中的行为不同 当把Numpy数组放在与张量流张量相同的位置时,它似乎能正常工作 此示例显示在numpy函数和keras函数下正确处理numpy矩阵Python 在numpy argmax和keras argmax中,TensorFlow张量的处理方式不同,python,numpy,tensorflow,keras,Python,Numpy,Tensorflow,Keras,为什么一个张量流张量在Numpy的数学函数中的行为与在Keras的数学函数中的行为不同 当把Numpy数组放在与张量流张量相同的位置时,它似乎能正常工作 此示例显示在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
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)
输出(如预期的那样)
与这个例子相反
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)
哪个输出
np_argmax shape: ()
np_max shape: (19, 19, 5, 80)
k_argmax shape: (19, 19, 5)
k_max shape: (19, 19, 5)
对于第二个示例,为什么不尝试以下代码:
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)
在
arr=tf.constant(np.random.rand(19,19,5,80))
之后,arr
的类型是tf.Tensor
,但是在运行arr=sess.run(arr)
之后,它的类型将更改为numpy.ndarray,您需要执行/运行代码(比如在tf会话下)来计算张量。在此之前,不会对张量的形状进行评估
TF文件说:
张量中的每个元素都有相同的数据类型,并且数据类型总是已知的。形状(即,其尺寸的数量和每个尺寸的大小)可能仅部分已知。如果输入的形状也是完全已知的,大多数操作都会生成形状完全已知的张量,但在某些情况下,只有在图形执行时才可能找到张量的形状
啊,应该抓到的。谢谢你的帮助!
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)