What is the best way to read SQL database in to Tensorflow?
Currently, I am using Postgres on server and developed DL algorithm on Tensorflow using Jupyter Lab. How can I import data into Jupyter Lab using tf.data
API? I do not want to store the data in the disk and keep running the algorithm when the new data arrives.
It seems like tf.data.experimental.SqlDataset
only support for sqlite.
(NOTE: I did not upgrade my Tensorflow, so, I am using tf.contrib.data.SqlDataset()
for the minimal working example.)
I migrated the data from PostgreSQL to SQLite3 and using
#Ignore the warnings
import warnings
warnings.filterwarnings("ignore")
import tensorflow as tf
#To start an input pipeline, you must define a source
dataset = tf.contrib.data.SqlDataset("sqlite", "/home/musara1/musara_dev.sqlite3",
"SELECT * FROM basetable LIMIT 10",
(tf.string, tf.int32, tf.int32, tf.int32, tf.int32, tf.int32, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.int32, tf.int32, tf.int32))
iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()
# Prints the rows of the result set of the above query.
sess=tf.InteractiveSession()
print(sess.run(next_element))
I can print the next element. However, there are other transformations I need to do on the dataset. such as splitting into training/validation/testing and getting rid of some columns et cetera. However, the output of tf.contrib.data.SqlDataset()
is for me
<SqlDataset shapes: ((), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ()), types: (tf.string, tf.int32, tf.int32, tf.int32, tf.int32, tf.int32, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.float64, tf.int32, tf.int32, tf.int32)>
I have 25 columns and tf.contrib.data.SqlDataset()
creates 25 different tensorflow.python.framework.ops.Tensor
. How can I bring them together? Therefore, I can use tf.data.Dataset.from_tensor_slices()
?