4

I would like to train a CNN with a large dataset. Currently I load all data into tf.constant and then loop through it with a small Batch size in tf.Session(). That works fine for a small fraction of the dataset, but when I increase the input size I get the error:

ValueError: Cannot create a tensor proto whose content is larger than 2GB.

How can I avoid that?

arm712
  • 107
  • 2
  • 11
  • Possible duplicate of [Initializing tensorflow Variable with an array larger than 2GB](https://stackoverflow.com/questions/35394103/initializing-tensorflow-variable-with-an-array-larger-than-2gb) – Steven Jul 13 '17 at 14:07

2 Answers2

6

Do not load data to constant, it will be part of your computational graph.

You should rather:

  • Create an op which is loading your data in stream fashion
  • Load data in python part, and use feed_dict to pass the batch into the graph
lejlot
  • 56,903
  • 7
  • 117
  • 144
5

For TensorFlow 1.x and Python 3, there is my simple solution:

X_init = tf.placeholder(tf.float32, shape=(m_input, n_input))
X = tf.Variable(X_init)
sess.run(tf.global_variables_initializer(), feed_dict={X_init: data_for_X})

In practice, you will mostly specify Graph and Session for continuous computation, this following code will help you:

my_graph = tf.Graph()
sess = tf.Session(graph=my_graph)
with my_graph.as_default():
    X_init = tf.placeholder(tf.float32, shape=(m_input, n_input))
    X = tf.Variable(X_init)
    sess.run(tf.global_variables_initializer(), feed_dict={X_init: data_for_X})
    .... # build your graph with X here
.... # Do some other things here
with my_graph.as_default():
    output_y = sess.run(your_graph_output, feed_dict={other_placeholder: other_data})
Tom
  • 3,568
  • 2
  • 19
  • 18