我解决了这个问题。谢谢。
我正在使用 python 中的 tensorflow 实现 CNN。
# Create model
def multilayer_perceptron(x):
# Hidden fully connected layer
W1_1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01))
L1_1 = tf.nn.conv2d(X, W1_1, strides=[1, 1, 1, 1], padding='SAME') # (?, 192, 112, 32)
L1_1 = tf.nn.relu(L1_1)
W1_2 = tf.Variable(tf.random_normal([3, 3, 32, 32], stddev=0.01))
L1_2 = tf.nn.conv2d(L1_1, W1_2, strides=[1, 1, 1, 1], padding='SAME') # (?, 192, 112, 32)
L1_2 = tf.nn.relu(L1_2)
# Pooling
L1_2 = tf.nn.max_pool(L1_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # (?, 96, 52 ,32)
W 和 L 太长,所以我省略了它们
# Pooling
L4_3 = tf.nn.max_pool(L4_3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # (?, 12, 7 ,84)
L4_flat = tf.reshape(L4_3, [-1, 12 * 7 * 84])
hypothesis = tf.matmul(L4_flat, Weight) + Bias
return hypothesis
# Construct model
logits = multilayer_perceptron(X)
# obtain cm after training
confusion_matrix_tf = tf.confusion_matrix(tf.argmax(logits, 1), tf.argmax(y_, 1))
# Define loss and optimizer -- loss_op = cost
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(cost)
with tf.name_scope('eval'):
pred = tf.nn.softmax(logits) # Apply softmax to logits
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(Y, 1))
# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
# Initializing the variables
init = tf.global_variables_initializer()
total_accuracy = 0
stack = 0
with tf.Session() as sess:
sess.run(init)
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.
for i in range(int(len(train_X)/(batch_size*10))):
start = ((i + 1) * batch_size) - batch_size
end = ((i + 1) * batch_size)
batch_xs = train_X[start:end]
batch_ys = train_Y[start:end]
feed_dict = {X: batch_xs, Y: batch_ys, keep_prob: keep_prob_training}
# Run optimization op (back prop) and cost op (to get loss value)
_, c = sess.run([train_op, cost], feed_dict=feed_dict)
avg_cost = avg_cost + c
# Compute average loss
# Display logs per epoch step
if epoch % display_step == 0:
print("Epoch:", '%04d' % (epoch+1), "cost={:.9f}".format(avg_cost/(len(train_X)/(batch_size*10))))
print("Optimization Finished!")
# Test model
for i in range(len(test_X) // batch_size):
start = ((i + 1) * batch_size) - batch_size
end = ((i + 1) * batch_size)
batch_xs = test_X[start:end]
batch_ys = test_Y[start:end]
accuracy_val = accuracy.eval(feed_dict={X: batch_xs, Y: batch_ys, keep_prob: keep_prob_training})
stack = stack + accuracy
total_accuracy = stack / (i + 1)
print("Test Accuracy: {:04f}".format(total_accuracy)) # line 199
cm = confusion_matrix_tf.eval(feed_dict={X: test_X, Y: test_Y, keep_prob: 1.0})
print("CM=\n", cm)
编辑: 这是我运行这段代码时的错误。
Traceback (most recent call last):
File "C:/Users/admin/PycharmProjects/fingerPrint/CNN.py", line 199, in <module>
print("Test Accuracy: {:04f}".format(total_accuracy))
TypeError: unsupported format string passed to Tensor.__format__
第 199 行:print("Test Accuracy: {:04f}".format(total_accuracy))
而且我也想打印 cm。 数据集很大,无法一次性计算cm。
是否可以为每个批次计算并添加混淆矩阵?
答案 0 :(得分:0)
我不知道confusion_matrix_tf 是什么,因为它不在您的代码中。但我认为这是 https://www.tensorflow.org/api_docs/python/tf/math/confusion_matrix 的一些变体。
如果由于样本数量太大而无法一次计算,您可以将其作为测试迭代的一部分进行计算,然后将结果累加。
如果它只是计算事物,则在您的数据集上累积计数(eval 会给您一些结果,您只需将其全部加起来即可)。
如果您已经在计算比率,那么您需要通过使用每次运行中的示例数量作为权重进行加权平均来进行调整。
如果矩阵因为类的数量而太大,那么我知道没有简单的解决方案。