2017-07-06 121 views
0

我是新来tensorflow和我有一个快速的问题,这是我的模式,为MNISTMNIST图像预测模型

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)) 

所以在这里我很困惑,即函数是否是一个模型或仅返回预测输出。任何人都可以解释,谢谢

+0

此代码是否运行?你能以一种让我运行它的格式提供完整的代码吗? (例如Github) – finbarr

+0

是的代码工作,但我的问题是关于模型制作 –

+0

这个功能据说是训练模型以及输入图像的输出吗? –

回答

2

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

因此,该函数返回模型的输出层,这将用于做出预测。可以说,你可以称之为“模型”,但我认为将会话变量称为“模型”会更好。

+0

这是什么意思? prediction = neural_network_model(mnist_training_data_set) –

+0

预测变量得到什么?函数调用后 –

+0

这意味着你所预测的值等于'output',它是你在函数内定义的神经网络的输出层。 – finbarr