What is the best way to read SQL dataset in to Tensorflow?












3












$begingroup$


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()?










share|improve this question











$endgroup$












  • $begingroup$
    So just to confirm, you've already migrated entire dataset from PostgreSQL to SQLite3, right? If not, you may like to go through this, as you've already noticed SQLite is a better companion with TF.
    $endgroup$
    – Random Nerd
    Nov 24 '18 at 6:22










  • $begingroup$
    Yes I did. My problem is how to manipule SqlDataset on Tensorflow using tf.data API
    $endgroup$
    – ARAT
    Nov 24 '18 at 14:35
















3












$begingroup$


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()?










share|improve this question











$endgroup$












  • $begingroup$
    So just to confirm, you've already migrated entire dataset from PostgreSQL to SQLite3, right? If not, you may like to go through this, as you've already noticed SQLite is a better companion with TF.
    $endgroup$
    – Random Nerd
    Nov 24 '18 at 6:22










  • $begingroup$
    Yes I did. My problem is how to manipule SqlDataset on Tensorflow using tf.data API
    $endgroup$
    – ARAT
    Nov 24 '18 at 14:35














3












3








3


2



$begingroup$


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()?










share|improve this question











$endgroup$




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()?







machine-learning deep-learning tensorflow dataset sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 15:06









Random Nerd

708




708










asked Nov 24 '18 at 4:01









ARATARAT

1435




1435












  • $begingroup$
    So just to confirm, you've already migrated entire dataset from PostgreSQL to SQLite3, right? If not, you may like to go through this, as you've already noticed SQLite is a better companion with TF.
    $endgroup$
    – Random Nerd
    Nov 24 '18 at 6:22










  • $begingroup$
    Yes I did. My problem is how to manipule SqlDataset on Tensorflow using tf.data API
    $endgroup$
    – ARAT
    Nov 24 '18 at 14:35


















  • $begingroup$
    So just to confirm, you've already migrated entire dataset from PostgreSQL to SQLite3, right? If not, you may like to go through this, as you've already noticed SQLite is a better companion with TF.
    $endgroup$
    – Random Nerd
    Nov 24 '18 at 6:22










  • $begingroup$
    Yes I did. My problem is how to manipule SqlDataset on Tensorflow using tf.data API
    $endgroup$
    – ARAT
    Nov 24 '18 at 14:35
















$begingroup$
So just to confirm, you've already migrated entire dataset from PostgreSQL to SQLite3, right? If not, you may like to go through this, as you've already noticed SQLite is a better companion with TF.
$endgroup$
– Random Nerd
Nov 24 '18 at 6:22




$begingroup$
So just to confirm, you've already migrated entire dataset from PostgreSQL to SQLite3, right? If not, you may like to go through this, as you've already noticed SQLite is a better companion with TF.
$endgroup$
– Random Nerd
Nov 24 '18 at 6:22












$begingroup$
Yes I did. My problem is how to manipule SqlDataset on Tensorflow using tf.data API
$endgroup$
– ARAT
Nov 24 '18 at 14:35




$begingroup$
Yes I did. My problem is how to manipule SqlDataset on Tensorflow using tf.data API
$endgroup$
– ARAT
Nov 24 '18 at 14:35










1 Answer
1






active

oldest

votes


















0












$begingroup$

You use the methods on SqlDataset to manipulate the data. For example, create a train/test split with:



test_dataset = dataset.take(1000) 
train_dataset = dataset.skip(1000)


I would get rid of unneeded columns in the SELECT statement to reduce the size of the data as early as possible.






share|improve this answer









$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "557"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f41636%2fwhat-is-the-best-way-to-read-sql-dataset-in-to-tensorflow%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0












    $begingroup$

    You use the methods on SqlDataset to manipulate the data. For example, create a train/test split with:



    test_dataset = dataset.take(1000) 
    train_dataset = dataset.skip(1000)


    I would get rid of unneeded columns in the SELECT statement to reduce the size of the data as early as possible.






    share|improve this answer









    $endgroup$


















      0












      $begingroup$

      You use the methods on SqlDataset to manipulate the data. For example, create a train/test split with:



      test_dataset = dataset.take(1000) 
      train_dataset = dataset.skip(1000)


      I would get rid of unneeded columns in the SELECT statement to reduce the size of the data as early as possible.






      share|improve this answer









      $endgroup$
















        0












        0








        0





        $begingroup$

        You use the methods on SqlDataset to manipulate the data. For example, create a train/test split with:



        test_dataset = dataset.take(1000) 
        train_dataset = dataset.skip(1000)


        I would get rid of unneeded columns in the SELECT statement to reduce the size of the data as early as possible.






        share|improve this answer









        $endgroup$



        You use the methods on SqlDataset to manipulate the data. For example, create a train/test split with:



        test_dataset = dataset.take(1000) 
        train_dataset = dataset.skip(1000)


        I would get rid of unneeded columns in the SELECT statement to reduce the size of the data as early as possible.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 22 mins ago









        Brian SpieringBrian Spiering

        3,6131028




        3,6131028






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Data Science Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f41636%2fwhat-is-the-best-way-to-read-sql-dataset-in-to-tensorflow%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Callistus I

            Tabula Rosettana

            How to label and detect the document text images