使用数据集迭代器将数据馈入模型:
self.validate_data = tf.data.Dataset.from_tensor_slices(\
(features.astype(float), labels) ).batch(self.batch_size)
...
self.validate_init_op = \
iterator.make_initializer(self.validate_data)
...
self.sess.run(self.validate_init_op)
模型的最后一层正在产生分类结果:
drop2 = tf.layers.dropout(hidden2,rate=0.5)
self.logits = tf.layers.dense(drop2,len(self.label_names),name="logits")
验证循环将self.logits
检索为predictions
,以便我可以将预测与标签进行手动比较。 predictions
是大小为(self.batch_size,self.label_names)
的2D数组,在本次运行中为100x10。每行都是标签概率的数组。同样,self.validate_labels
的行是标签的eye
。
self.sess.run(self.validate_init_op)
for iteration in range(n_validate_batches):
summary, predictions, batch_loss = self.sess.run(\
[self.merged_summary, self.logits, self.loss])
...
for r in range(predictions.shape[0]):
prediced_eye = predictions[r,:]
label_index = iteration * self.batch_size + r
best_guess = np.argmax(prediced_eye)
assert(best_guess < self.validate_labels.shape[1])
if epo==(epochs-1) and iteration<1 and r < 3:
print("DEBUG: best_guess = {}, predictions[{}] = {}". \
format(best_guess, r, str(prediced_eye)))
print("DEBUG: guessed = {}, validate_labels[{}] = {}". \
format(self.validate_labels[label_index,best_guess],
label_index, self.validate_labels[label_index]))
在上面的循环中,我正在打印几个数据点的预测。令我惊讶的是,预测概率数组是相同的:
DEBUG: best_guess = 5, predictions[0] = [0.07977642 0.02335115 0.07917383 0.03262045 0.06117218 0.09661281
0.0569936 0.06503091 0.07465875 0.04835918]
DEBUG: guessed = 0.0, validate_labels[0] = [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
DEBUG: best_guess = 5, predictions[1] = [0.07977642 0.02335115 0.07917383 0.03262045 0.06117218 0.09661281
0.0569936 0.06503091 0.07465875 0.04835918]
DEBUG: guessed = 0.0, validate_labels[1] = [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
DEBUG: best_guess = 5, predictions[2] = [0.07977642 0.02335115 0.07917383 0.03262045 0.06117218 0.09661281
0.0569936 0.06503091 0.07465875 0.04835918]
DEBUG: guessed = 0.0, validate_labels[2] = [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
看来predictions
并没有在数据点之间更新,或者self.logits
仅执行了一次,或者其他原因导致每个批次内以及批次之间的所有预测都相同。 / p>
您能建议这里出现问题吗?
根据Anand的建议,我现在也在打印输入。这是测试代码;区别在于我还在打印输入的Sha1代码:
self.prepare_test_data(self.batch_size)
self.test_init_op = self.iterator.make_initializer(self.test_data)
n_test_batches = len(self.test_labels) // self.batch_size
print("\nTesting the model.")
self.sess.run(self.test_init_op)
guessed = 0
for iteration in range(n_test_batches):
summary, inputs, predictions = self.sess.run(\
[self.merged_summary, self.flat_features, self.logits])
for r in range(predictions.shape[0]):
predicted_eye = predictions[r,:]
label_index = iteration * self.batch_size + r
best_guess = np.argmax(predicted_eye)
assert(best_guess < self.test_labels.shape[1])
if (iteration%50)==0 and r<3:
m = hashlib.sha1()
m.update(inputs[r,:])
print("\nDEBUG: batch {} row = {}: inputs hash = {}". \
format(iteration, r, str(m.digest())))
print("row {}: argmax = {}, predictions = {}". \
format(r, best_guess, str(predicted_eye)))
print("label {}: eye[{}] = {}, test_labels = {}". \
format(label_index, best_guess,
self.test_labels[label_index,best_guess],
self.test_labels[label_index]))
guessed += self.test_labels[label_index,best_guess] / self.batch_size
print("Test top-1 rate =", guessed/n_test_batches)
这是输出的一部分:
DEBUG: batch 50 row = 0: inputs hash = b';\xc9\xf1\xc7\xa3\x183\xd2\xf9\xb4\xa3\xee\x19Q0\xcd\xda;\x8f\x0e'
row 0: argmax = 8, predictions = [ 0.06992176 -0.00317043 0.05659114 0.02182345 0.03729291 0.06833207
0.0450546 0.0401533 0.07006571 0.00714686]
label 5000: eye[8] = 0.0, test_labels = [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
DEBUG: batch 50 row = 1: inputs hash = b'\xcf\xc8P\x8fC\x85C\xa2E\x97\xe5be [\x85 \xd65\xab'
row 1: argmax = 8, predictions = [ 0.06992176 -0.00317043 0.05659114 0.02182345 0.03729291 0.06833207
0.0450546 0.0401533 0.07006571 0.00714686]
label 5001: eye[8] = 0.0, test_labels = [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
DEBUG: batch 50 row = 2: inputs hash = b"'\x8c\x03\xab'\x06\x0c\xb2S,2\x95_\x10\xb0\xe1\n\x94\xbb\xa8"
row 2: argmax = 8, predictions = [ 0.06992176 -0.00317043 0.05659114 0.02182345 0.03729291 0.06833207
0.0450546 0.0401533 0.07006571 0.00714686]
label 5002: eye[8] = 1.0, test_labels = [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
Test top-1 rate = 0.0009
请注意,输入哈希值都不同,但是预测是相同的。