# 模型精确度 def compute_accuracy(v_xs, v_ys): global prediction y_pre = sess.run(prediction, feed_dict={xs: v_xs, keep_prop: 0.5}) # y_pre_tru=(y_pre-0.05)*(np.max(datays)-np.min(datays))/(0.95-0.05) accuracy_prediction = 1 / (1 + tf.sqrt(tf.reduce_mean(tf.square(y_pre - ys)))) # accuracy_prediction_tru=1/(1+tf.sqrt(tf.reduce_mean(tf.square(y_pre_tru-datays_test)))) result = sess.run(accuracy_prediction, feed_dict={xs: v_xs, ys: v_ys, keep_prop: 0.5}) print(y_pre) return result这段代码含义
时间: 2024-02-26 12:55:41 浏览: 86
这段代码的含义是定义了一个计算模型精确度的函数,该函数的输入参数为测试数据集(v_xs和v_ys)。在函数内部,首先使用会话(sess)运行模型(prediction)并得到预测结果(y_pre)。然后,使用 TensorFlow 的数学函数计算预测结果(y_pre)与真实结果(ys)之间的误差,进而计算模型的精确度(accuracy_prediction)。最后,使用会话(sess)运行计算模型精确度(accuracy_prediction)的操作,并将测试数据集(v_xs和v_ys)和dropout比率(keep_prop)输入到会话中,得到最终的模型精确度(result)并返回。此外,这段代码中还包含了一些被注释掉的代码,其中的y_pre_tru和accuracy_prediction_tru似乎是计算模型预测结果与真实结果的真实值之间误差和精确度的代码,但并未被使用。
相关问题
with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) sess.run(iterator.initializer) # 初始化迭代器 print("step 0, test accuracy %g" % compute_accuracy(test_img, test_label)) start = time.time() for i in range(max_epoch): batch_xs, batch_ys = sess.run(next_batch) # 此batch是个2维tuple,batch[0]是(100,784)的样本数据数组,batch[1]是(100,10)的样本标签数组 sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: keep_prob_rate}) if (i+1) % 50 == 0: print("step %d, test accuracy %g" % ((i+1), compute_accuracy(mnist.test_images, mnist.test_labels))) end = time.time() print('******************************************************') print("运行时间:%.2f秒" % (end - start)) 出现Traceback (most recent call last): File "D:\PycharmProjects\CNN_mnist\lab.py", line 104, in <module> print("step 0, test accuracy %g" % compute_accuracy(test_img, test_label)) File "D:\PycharmProjects\CNN_mnist\lab.py", line 46, in compute_accuracy y_pre = sess.run(prediction, feed_dict={xs: v_xs, keep_prob: 1}) File "D:\Anaconda3\envs\tf26gpu\lib\site-packages\tensorflow\python\client\session.py", line 967, in run result = self._run(None, fetches, feed_dict, options_ptr, File "D:\Anaconda3\envs\tf26gpu\lib\site-packages\tensorflow\python\client\session.py", line 1164, in _run raise ValueError( ValueError: Cannot feed value of shape (10000, 28, 28) for Tensor 'x_input:0', which has shape '(?, 784)'
根据你提供的错误信息,出现了一个 `ValueError`。错误信息显示无法将形状为 `(10000, 28, 28)` 的值传递给形状为 `(?, 784)` 的张量 `x_input`。
这个错误是因为你的输入数据 `test_img` 的形状是 `(10000, 28, 28)`,但是模型的输入张量 `x_input` 的形状应该是 `(?, 784)`,其中 `?` 表示批次大小。这意味着你需要对输入数据进行扁平化处理,将其从三维的形状 `(10000, 28, 28)` 转换为二维的形状 `(10000, 784)`。
你可以使用 `reshape` 方法来实现这个操作。修改代码如下:
```python
test_img = test_img.reshape(-1, 784) # 将测试集图片数据扁平化
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
sess.run(iterator.initializer) # 初始化迭代器
# 在训练之前先计算初始的测试准确率
print("step 0, test accuracy %g" % compute_accuracy(test_img, test_label))
start = time.time()
for i in range(max_epoch):
batch_xs, batch_ys = sess.run(next_batch)
sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: keep_prob_rate})
if (i+1) % 50 == 0:
# 在每个训练步骤后计算并打印测试准确率
print("step %d, test accuracy %g" % ((i+1), compute_accuracy(test_img, test_label)))
end = time.time()
print('******************************************************')
print("运行时间:%.2f秒" % (end - start))
```
这样,你的输入数据的形状就与模型的输入张量匹配了。如果还有其他问题,请提供更多的上下文或完整的代码,以便我能够更好地帮助你。
阅读全文
相关推荐















