Tensorflow:如何使用`tfrecords`制作模型火车,但使用`feed_dict`进行测试

时间:2017-07-02 14:17:55

标签: tensorflow

我最近使用csv数据完成了线性回归模型的训练。

此处显示训练数据的结果:

Result

但是,我仍然对如何使用该模型感到茫然。

我如何给模型一个“x”值,使它返回一个“y”值?

代码:

with tf.Session() as sess:
# Start populating the filename queue.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        _, cost_value = sess.run([optimizer,cost])

        #Display logs per epoch step
    if (epoch+1) % display_step == 0:
        c = sess.run(cost)
        print( "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
            "W=", sess.run(W), "b=", sess.run(b))

    print("Optimization Finished!")
    training_cost = sess.run(cost)
    print ("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

    #Plot data after completing training
    train_X = []
    train_Y = []
    for i in range(n_samples): #Your input data size to loop through once
        X, Y = sess.run([col1, pred]) # Call pred, to get the prediction with the updated weights
        train_X.append(X)
        train_Y.append(Y)
    #Graphic display
    df = pd.read_csv("battdata2.csv", header=None)
    X = df[0]
    Y = df[1]
    plt.plot(train_X, train_Y, linewidth=1.0, label='Predicted data')
    plt.plot(X, Y, 'ro', label='Input data')
    plt.legend()
    plt.show()

    print("train_X -- -")
    print(train_X)
    print("X -- -")
    print(X)
    print("train_Y -- -")
    print(train_Y)
    print("Y -- -")
    print(Y)

    save_path = saver.save(sess,   "C://Users//Shiina//model.ckpt",global_step=1000)
    print("Model saved in file: %s" % save_path)

coord.request_stop()
coord.join(threads)

链接到ipynb和csv文件here

1 个答案:

答案 0 :(得分:2)

您基本上希望在训练期间使用queue runners将输入提供给网络,但在推理期间,您希望通过feed_dict输入输入。这可以通过使用tf.placeholder_with_default()来完成。因此,当输入未通过feed_dict提供时,它将从队列中读取,否则从“feed_dict”获取。您的代码应该是:

col1_batch, col2_batch = tf.train.shuffle_batch([col1, col2], ...
# if data is not feed through `feed_dict` it will pull from `col*_batch`
_X = tf.placeholder_with_default(col1_batch, shape=[None], name='X')
_y = tf.placeholder_with_default(col2_batch, shape=[None], name='y')