我有一个训练有素的张量流模型,我如何使用相同的预测?

时间:2017-10-23 18:36:38

标签: machine-learning tensorflow neural-network recurrent-neural-network

我通过调用“train_neural_network”功能来训练我的模型,该功能训练模型并存储模型,精度达到83%左右,我面临的问题是如何使用我保存的模型进行预测?要恢复哪个变量以及如何传递输入(分批或整批)?

def make_model(data,train_x):

    n_nodes_hl1 = 2000
    n_nodes_hl2 = 2000
    n_nodes_hl3 = 2000

    n_classes = 2 # No of classification

    hidden_1_layer = {'weights': tf.Variable(tf.truncated_normal([len(train_x[0]), n_nodes_hl1], stddev=0.1),name= 'weights'),
                      'biases': tf.Variable(tf.constant(0.1, shape=[n_nodes_hl1]),name = 'biases')}

    hidden_2_layer = {'weights': tf.Variable(tf.truncated_normal([n_nodes_hl1, n_nodes_hl2], stddev=0.1),name= 'weights'),
                      'biases': tf.Variable(tf.constant(0.1, shape=[n_nodes_hl2]),name = 'biases')}

    hidden_3_layer = {'weights': tf.Variable(tf.truncated_normal([n_nodes_hl2, n_nodes_hl3], stddev=0.1,),name= 'weights'),
                      'biases': tf.Variable(tf.constant(0.1, shape=[n_nodes_hl3]),name = 'biases')}

    output_layer = {'weights': tf.Variable(tf.truncated_normal([n_nodes_hl3, n_classes], stddev=0.1),name= 'weights'),
                    'biases': tf.Variable(tf.constant(0.1, shape=[n_classes]),name = 'biases'), }


    layer_1 = tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases'])
    # now goes through an activation function - sigmoid function
    layer_1 = tf.nn.relu(layer_1)
    print ("Layer 1 done!!")
    # input for layer 2 = result of activ_func for layer 1
    layer_2 = tf.add(tf.matmul(layer_1, hidden_2_layer['weights']), hidden_2_layer['biases'])
    layer_2 = tf.nn.relu(layer_2)
    print ("Layer 2 done!!")

    layer_3 = tf.add(tf.matmul(layer_2, hidden_3_layer['weights']), hidden_3_layer['biases'])
    layer_3 = tf.nn.relu(layer_3)
    print ("Layer 3 done!!")

    output = tf.matmul(layer_3, output_layer['weights'],name = "output") + output_layer['biases']

    return output



def train_neural_network(train_x,train_y,test_x,test_y):
    tf.reset_default_graph()
    with tf.name_scope('input'):
        x = tf.placeholder('float', [None, len(train_x[0])],name= 'x_input')
        y = tf.placeholder('float',name = 'y-input')
    # Merge all the summaries and write them out to /tmp/mnist_logs (by default)


    prediction = make_model(x,train_x)
    print ('model ready!!')
    with tf.name_scope('pred'):
        pred = tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y)
    with tf.name_scope('cost'):
        cost = tf.reduce_mean(pred)
    with tf.name_scope('train'):
        optimizer = tf.train.AdamOptimizer().minimize(cost,name = 'optimizer')


    tf.summary.scalar("cost", cost)




    n_epochs = 10
    batch_size = 100


    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())  # initializes our variables. Session has now begun.
        merged = tf.summary.merge_all()
        train_writer = tf.summary.FileWriter('train/2/',
                                              sess.graph)
        test_writer = tf.summary.FileWriter('test/')

        for epoch in range(n_epochs):
            epoch_loss = 0  # we'll calculate the loss as we go

            i = 0
            while i < len(train_x):
                #we want to take batches(chunks); take a slice, then another size)
                start = i
                end = i+batch_size

                batch_x = np.array(train_x[start:end])
                batch_y = np.array(train_y[start:end])
                _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})
                if i%200 == 0:
                    train_writer.add_summary(_, i)
                epoch_loss += c
                i+=batch_size
            print('Epoch', epoch, 'completed out of', n_epochs, 'loss:', epoch_loss)
            with tf.name_scope('accuracy'):
                correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
                accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
                tf.summary.scalar("accuracy", accuracy)


            print('Accuracy:', accuracy.eval({x: test_x, y: test_y}))
            saver = tf.train.Saver()
            tf_log = 'tf.log'
            saver.save(sess, "model3.ckpt")

    return accuracy

This is my model 这就是我进行预测的方式,但每次都失败了:

def test_neural_network(test_x):

    batch_size = 100
    i = 0
    batch_x = np.array(test_x[i:i+batch_size])
    tf.reset_default_graph()
    x = tf.placeholder('float', [len(batch_x),len(test_x[0])])
    y = tf.placeholder('float',[2])
    prediction = make_model(x,batch_x)
    # pred1 = tf.nn.softmax(logits=prediction)
    # weight = tf.get_variable("weights_3", shape=[len(batch_x),2],initializer = tf.zeros_initializer)
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        saver.restore(sess, "model3.ckpt")
        p = tf.argmax(prediction,1)
        print (p.eval({x:batch_x))

给出形状数组(batch_size,2),期望值[0,1]或[1,0],但得到十进制值。

2 个答案:

答案 0 :(得分:0)

您遇到问题,因为您在变量«weight»上启动会话。但在您的情况下,您想知道网络的输出。尝试在最后一层启动会话

答案 1 :(得分:0)

  

如何使用我保存的模型进行预测?哪个变量来   恢复以及如何传递输入(分批或整批)?

关于您的设计的几条评论。您不必在测试时重建图表,因为它会保存在会话检查点旁边。看看this question

这样,您的代码将被简化很多,因为您不必分别保留占位符和交叉熵损失功能。将名称添加到softmax图层,如下所示:

with tf.name_scope('pred'):
  pred = tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y, name='softmax')

在您恢复图表后,您可以通过以下方式找到目标操作:

graph = sess.graph
pred = graph.get_operation_by_name("pred/softmax")

如果您的测试数据不大,您可以一次性自由提供所有测试数据,但如果测试数据明显大于批量大小,则可以轻松获得out-of-memory。在这种情况下,您也应该使用迷你批次进行测试。

至于你的测试准确性,可以有很多理由,例如overfitting。使用完整代码更新问题,以便可以复制。