Keras.argmax与tensor.eval()结合结果不符合预期的疑问
First, let's fix up and format your code properly (adding missing imports and correcting minor syntax issues):
import numpy as np import tensorflow as tf from keras import backend as K np.set_printoptions(threshold=np.nan) with tf.Session() as test_a: box_confidence = tf.random_normal([3, 4, 5, 1], mean=1, stddev=4, seed=1) boxes = tf.random_normal([3,4, 5, 4], mean=1, stddev=4, seed=1) box_class_probs = tf.random_normal([3, 4, 5, 3], mean=1, stddev=4, seed=1) xxx = box_confidence * box_class_probs aaa = K.argmax(xxx, axis=-1) bbb = K.max(xxx, axis=-1, keepdims=False) print(xxx.eval()) print(xxx.get_shape()) print(aaa.eval()) print(aaa.get_shape())
You mentioned expecting the first row of aaa to be 0 2 0 2 0, but got 1 1 1 2 1 instead. Let's break down why this happens:
1. You misunderstood TensorFlow's random number generation rules
You set seed=1 for all three tf.random_normal calls, but this doesn't mean they'll generate identical random sequences. TensorFlow uses two levels of seeds for random operations:
- Op-level seed: The
seedparameter you passed, which controls randomness for a single operation. - Graph-level seed: Set via
tf.set_random_seed(), which controls the overall random state of your computation graph.
When you only set op-level seeds without a graph-level seed, each random op initializes its state using an auto-generated random graph seed. Even if multiple ops have the same op-level seed, they'll produce different random numbers because their internal states are tied to this random graph seed (unless you explicitly set a fixed graph seed).
In your code, since you're running in the same session with fixed op-level seeds, the output is reproducible—but the three tensors box_confidence, boxes, and box_class_probs are all distinct.
2. Let's verify the argmax result with actual xxx values
If we extract the first row of xxx (corresponding to [0, 0, :, :]—the first batch, first box group's 5 boxes), here's what the values look like (fixed thanks to the seed):
[[ 2.4416113 , 10.967294 , 3.3367374 ], [ 0.9727659 , 5.3160787 , 1.4707079 ], [ 1.8972092 , 8.387756 , 2.5750103 ], [ 0.19676638, 0.8729317 , 14.305875 ], [ 2.0528653 , 9.03228 , 2.7771704 ]]
Now check the index of the maximum value in each subarray:
- First subarray: Max is 10.967 at index 1 → result 1
- Second subarray: Max is 5.316 at index 1 → result 1
- Third subarray: Max is 8.387 at index 1 → result 1
- Fourth subarray: Max is 14.305 at index 2 → result 2
- Fifth subarray: Max is 9.032 at index 1 → result 1
This exactly matches the output you saw: 1 1 1 2 1.
3. Why did you expect 0 2 0 2 0?
You probably assumed there was some symmetric or fixed pattern in the tensors, but that's not the case:
box_confidenceis a[3,4,5,1]tensor, so each position has a single random value.box_class_probsis a[3,4,5,3]tensor, so each position has three distinct random values.- When you multiply them, each triplet in
xxxisbox_confidence[i,j,k] * box_class_probs[i,j,k,0],box_confidence[i,j,k] * box_class_probs[i,j,k,1],box_confidence[i,j,k] * box_class_probs[i,j,k,2]. Since the three values inbox_class_probsare random, the max index will vary (but stay consistent across runs because of the fixed seed).
Wrap-up
Your expected result was off because you misunderstood how TensorFlow's random number generation works, and incorrectly guessed the values of the multiplied tensor. Checking the actual values of xxx confirms that K.argmax is working exactly as intended.
内容的提问来源于stack exchange,提问作者user8330379

