【问题标题】:MNIST image prediction modelMNIST 图像预测模型
【发布时间】:2023-04-24 23:15:01
【问题描述】:

我是 tensorflow 的新手,我有一个简单的问题,这是我的 MNIST 模型代码

def neural_network_model(data):

    hidden_1_layer = {'weights': tf.Variable(tf.random_normal([784, n_nodes_hl1])),
                      'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))}

    hidden_2_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                      'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))}

    hidden_3_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                      'biases': tf.Variable(tf.random_normal([n_nodes_hl3]))}

    output_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
                    'biases': tf.Variable(tf.random_normal([n_classes])), }
    l1 = tf.add(
        tf.matmul(
            data,
            hidden_1_layer['weights']),
        hidden_1_layer['biases'])
    l1 = tf.nn.relu(l1)

    l2 = tf.add(
        tf.matmul(
            l1,
            hidden_2_layer['weights']),
        hidden_2_layer['biases'])
    l2 = tf.nn.relu(l2)

    l3 = tf.add(
        tf.matmul(
            l2,
            hidden_3_layer['weights']),
        hidden_3_layer['biases'])
    l3 = tf.nn.relu(l3)

    output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']

    return output

我的问题是这个函数是否代表输入“数据”的输出值?或者这个函数代表一个完整的模型,在训练后将用于测试/预测图像?

这是我用于预测特定图像的代码:

prediction=neural_network_model(mnist_training_data_set)
p=tf.argmax(prediction,1)
print(p.eval(feed_dict={x: i}, session=sess))

所以我很困惑,该函数是模型还是仅返回预测输出。谁能解释一下,谢谢

【问题讨论】:

  • 这段代码能运行吗?您能否以允许我运行它的格式提供完整的代码? (例如 Github)
  • 是的,代码有效,但我的问题与制作的模型有关
  • 这个函数据说是经过训练的模型以及输入图像的输出?

标签: python machine-learning tensorflow


【解决方案1】:

此函数创建模型并将其添加到计算图中。预测的输出将由p.eval(feed_dict={x: i}, session=sess) 行返回。

因此,该函数返回模型的输出层,您将使用它来进行预测。可以说您可以将其称为“模型”,但我认为将会话变量称为“模型”会更好。

【讨论】:

  • 这是什么意思?预测=neural_network_model(mnist_training_data_set)
  • 预测变量得到什么?函数调用后
  • 表示您预测的值等于output,即您在函数内部定义的神经网络的输出层。