Reputation: 1107
I'm new to neural networks
i have created a simple network according to this tutorial. It is trained to clarify text among 3 categories: sport, graphics and space https://medium.freecodecamp.org/big-picture-machine-learning-classifying-text-with-neural-networks-and-tensorflow-d94036ac2274
with tf.Session() as sess:
sess.run(init)
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(len(newsgroups_train.data)/batch_size)
print("total_batch",total_batch)
# Loop over all batches
for i in range(total_batch):
batch_x,batch_y = get_batch(newsgroups_train,i,batch_size)
# Run optimization op (backprop) and cost op (to get loss value)
c,cc = sess.run([loss,optimizer], feed_dict={input_tensor: batch_x,output_tensor:batch_y})
print("C = ", c)
print("Cc = ", cc)
# Compute average loss
avg_cost += c / total_batch
# Display logs per epoch step
if epoch % display_step == 0:
print("inpt ten =", batch_y)
print("Epoch:", '%04d' % (epoch+1), "loss=", \
"{:.9f}".format(avg_cost))
I wonder how after training i can feed this model with my own text and get the result
Thanks
Upvotes: 2
Views: 1801
Reputation: 449
Like janu777 said, we can save and load models for reuse. We first create a Saver object and then save the session (after the model is trained):
saver = tf.train.Saver()
... train the model ...
save_path = saver.save(sess, "/tmp/model.ckpt")
In the example model the last "step" in the model architecture (i.e. the last thing done inside the multilayer_perceptron
method) is:
'out': tf.Variable(tf.random_normal([n_classes]))
So to get a prediction we get the index of the maximum value of this array (the predicted class):
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, "/tmp/model.ckpt")
print("Model restored.")
classification = sess.run(tf.argmax(prediction, 1), feed_dict={input_tensor: input_array})
print("Predicted category:", classification)
You can check the whole code here: https://github.com/dmesquita/understanding_tensorflow_nn
Upvotes: 1
Reputation: 1978
Tensorflow has option to save and load models for reuse. You can save your trained model by adding this:
model_saver = tf.train.Saver()
#Training cycle
#your code to train
model_saver.save(sess,MODEL_SAVE_PATH)
Once your model is saved you can restore it again and test it like this:
model_saver.restore(sess, MODEL_SAVE_PATH)
c,cc = sess.run([loss,optimizer], feed_dict={input_tensor: batch_x,output_tensor:batch_y})
Here batch_x and batch_y represents your test data.
check this for more details on saving and restoring models.
Hope you find this helpful.
Upvotes: 1