Python 不同尺寸的TensorFlow reduce_max
是否可以在不同形状之间执行tf.reduce_max操作(本质上是“添加:操作”) 我得到了这个错误:Python 不同尺寸的TensorFlow reduce_max,python,tensorflow,conv-neural-network,Python,Tensorflow,Conv Neural Network,是否可以在不同形状之间执行tf.reduce_max操作(本质上是“添加:操作”) 我得到了这个错误: Tensor("reduce_max/parallel_0/split:0", shape=(900000,), dtype=float32, device=/device:GPU:1) -- Tensor("reduce_max/parallel_1/split:0", shape=(870000,), dtype=float32, device=/device:GPU:0) tensor
Tensor("reduce_max/parallel_0/split:0", shape=(900000,),
dtype=float32, device=/device:GPU:1) --
Tensor("reduce_max/parallel_1/split:0", shape=(870000,),
dtype=float32, device=/device:GPU:0)
tensorflow.python.framework.errors_impl.InvalidArgumentError:
Dimensions must be equal, but are 900000 and 870000 for
'reduce_max/Add' (op: 'Add') with input shapes: [900000], [870000].
下面是一个例子,我创建了一个参差不齐的张量(用于创建可变维度的张量),然后对其应用reduce_max操作 示例:
%tensorflow_version 2.x
import tensorflow as tf
# Create a Ragged Tensor of variable length
rt = tf.ragged.constant([[9, 8, 7], [], [6, 5], [4]])
print("Ragged Tensor:","\n",rt,"\n")
# Convert to Tensor to have same length
rt = rt.to_tensor()
print("Tensor of same length:","\n",rt,"\n")
# Apply reduce_max to get the max value along axis=1
rt = tf.reduce_max(rt, axis=1)
print("Reduce Max Tensor:","\n",rt,"\n")
输出-
Ragged Tensor:
<tf.RaggedTensor [[9, 8, 7], [], [6, 5], [4]]>
Tensor of same length:
tf.Tensor(
[[9 8 7]
[0 0 0]
[6 5 0]
[4 0 0]], shape=(4, 3), dtype=int32)
Reduce Max Tensor:
tf.Tensor([9 0 6 4], shape=(4,), dtype=int32)
参差不齐张量:
相同长度的张量:
tf张量(
[[9 8 7]
[0 0 0]
[6 5 0]
[4,0]],shape=(4,3),dtype=int32)
减小最大张量:
tf.Tensor([9 06 4],shape=(4,),dtype=int32)
欢迎使用stack overflow,请参阅始终共享可复制的代码和错误。