繁体   English   中英

如何打印值而不是张量?

[英]How can I print a value rather than a tensor?

我解决问题。 谢谢你。


我在 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("测试精度:{:04f}".format(total_accuracy))

我也想打印厘米。 数据集很大,无法一次计算出cm。

可以为每个批次计算和添加混淆矩阵吗?

我不知道confusion_matrix_tf 是什么,因为它不在您的代码中。 但我认为这是https://www.tensorflow.org/api_docs/python/tf/math/confusion_matrix的一些变体。

如果由于示例数量太大而无法一次计算,您可以将其作为测试迭代的一部分进行计算,然后只累积结果。

如果它只是对事物进行计数,则会在您的数据集上累积计数(eval 会为您提供一些结果,您只需将其全部加起来)。

如果您已经在计算比率,那么您需要通过使用每次运行中的示例数作为权重进行加权平均来进行调整。

如果由于类的数量而矩阵太大,那么我所知道的没有简单的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM