【发布时间】:2019-08-30 00:57:51
【问题描述】:
首先,我按照人们的指示尝试恢复模型,但我还没有找到任何线索。 以下是我保存模型的代码,模型已成功保存。
import tensorflow as tf
from sklearn.utils import shuffle
EPOCHS = 10
BATCH_SIZE = 128
x = tf.placeholder(tf.float32, (None, 32, 32, 3),name='x')
y = tf.placeholder(tf.int32, (None),name='y')
one_hot_y = tf.one_hot(y, 43)
rate = 0.001
logits = LeNet(x)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=one_hot_y, logits=logits)
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate = rate)
training_operation = optimizer.minimize(loss_operation)
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(one_hot_y, 1))
accuracy_operation = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver = tf.train.Saver()
def evaluate(TrainX, trainLabels):
num_examples = len(TrainX)
total_accuracy = 0
sess = tf.get_default_session()
for offset in range(0, num_examples, BATCH_SIZE):
batch_x, batch_y = TrainX[offset:offset+BATCH_SIZE], trainLabels[offset:offset+BATCH_SIZE]
accuracy = sess.run(accuracy_operation, feed_dict={x: batch_x, y: batch_y})
total_accuracy += (accuracy * len(batch_x))
return total_accuracy / num_examples
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
num_examples = len(trainImages)
print("Training...")
print()
for i in range(EPOCHS):
TrainX, trainLabels = shuffle(TrainX, trainLabels)
for offset in range(0, num_examples, BATCH_SIZE):
end = offset + BATCH_SIZE
batch_x, batch_y = TrainX[offset:end], trainLabels[offset:end]
sess.run(training_operation, feed_dict={x: batch_x, y: batch_y})
validation_accuracy = evaluate(TrainX, trainLabels)
print("EPOCH {} ...".format(i+1))
print("Validation Accuracy = {:.3f}".format(validation_accuracy))
print()
saver.save(sess, './lenet')
print("Model saved")
培训...
时代 1 ... 验证准确度 = 0.765
时代 2 ... 验证准确度 = 0.911
第三纪元 ... 验证准确度 = 0.933
纪元 4 ... 验证准确度 = 0.958
纪元 5 ... 验证准确度 = 0.965
第 6 纪元 ... 验证准确度 = 0.973
纪元 7 ... 验证准确度 = 0.978
第 8 纪元 ... 验证准确度 = 0.986
纪元 9 ... 验证准确度 = 0.985
纪元 10 ... 验证准确度 = 0.980
模型已保存
我的问题是当我放置新的测试数据时如何使用此模型,假设我放置了 5 个测试数据以查看它们在经过训练模型时的准确度。我想看看正确适合训练模型的测试数据和标签的准确性。感谢您抽出宝贵时间,如果您有不明白的地方,我愿意为您提供更多详细信息。
【问题讨论】:
-
您可以查看这个问题:stackoverflow.com/questions/33759623/…。他们已经清楚地解释了这一点。
标签: python tensorflow neural-network