为什么TensorFlow张量在Numpy中的数学函数行为与在Keras中的数学函数中的行为不同?
Numpy数组似乎在与TensorFlow张量相同的情况下正常工作。
此示例显示在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)产出(如预期)
np_argmax shape: (19, 19, 5)
np_max shape: (19, 19, 5)
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))
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)发布于 2018-02-05 06:09:11
您需要执行/运行代码(例如在TF会话下)来评估张量。在此之前,不计算张量的形状。
信托基金的医生说:
张量中的每个元素都具有相同的数据类型,并且数据类型总是已知的。形状(即它所拥有的维数和每个维度的大小)可能只部分已知。大多数操作产生完全已知形状的张量,如果输入的形状也是完全已知的,但在某些情况下,只有在图执行时才能找到张量的形状。
发布于 2018-02-05 06:06:24
为什么不为第二个示例尝试以下代码:
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。
https://stackoverflow.com/questions/48615667
复制相似问题