Reputation: 449
I recently completed training a Linear Regression model using a csv data.
The result of the trained data shown here:
However, I'm still dumbfounded as to how to use the model.
How do i give the model an "x" value such that it returns me a "y" value?
Code:
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)
Link to ipynb and csv files here.
Upvotes: 0
Views: 620
Reputation: 17191
You basically want the inputs to be fed to the network using queue runners
during training, but then during inference you want to input it through feed_dict
. This can be done by using tf.placeholder_with_default()
. So when the inputs are not fed through feed_dict
, it will read from the queues, otherwise it takes from 'feed_dict'. Your code should be like:
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')
Upvotes: 2