How to convert numpy arrays to standard TensorFlow format?

125,412

Solution 1

You can use tf.convert_to_tensor():

import tensorflow as tf
import numpy as np

data = [[1,2,3],[4,5,6]]
data_np = np.asarray(data, np.float32)

data_tf = tf.convert_to_tensor(data_np, np.float32)

sess = tf.InteractiveSession()  
print(data_tf.eval())

sess.close()

Here's a link to the documentation for this method:

https://www.tensorflow.org/api_docs/python/tf/convert_to_tensor

Solution 2

You can use tf.pack (tf.stack in TensorFlow 1.0.0) method for this purpose. Here is how to pack a random image of type numpy.ndarray into a Tensor:

import numpy as np
import tensorflow as tf
random_image = np.random.randint(0,256, (300,400,3))
random_image_tensor = tf.pack(random_image)
tf.InteractiveSession()
evaluated_tensor = random_image_tensor.eval()

UPDATE: to convert a Python object to a Tensor you can use tf.convert_to_tensor function.

Solution 3

You can use placeholders and feed_dict.

Suppose we have numpy arrays like these:

trX = np.linspace(-1, 1, 101) 
trY = 2 * trX + np.random.randn(*trX.shape) * 0.33 

You can declare two placeholders:

X = tf.placeholder("float") 
Y = tf.placeholder("float")

Then, use these placeholders (X, and Y) in your model, cost, etc.: model = tf.mul(X, w) ... Y ... ...

Finally, when you run the model/cost, feed the numpy arrays using feed_dict:

with tf.Session() as sess:
.... 
    sess.run(model, feed_dict={X: trY, Y: trY})
Share:
125,412
Keshav Choudhary
Author by

Keshav Choudhary

Updated on July 09, 2022

Comments

  • Keshav Choudhary
    Keshav Choudhary almost 2 years

    I have two numpy arrays:

    • One that contains captcha images
    • Another that contains the corresponding labels (in one-hot vector format)

    I want to load these into TensorFlow so I can classify them using a neural network. How can this be done?

    What shape do the numpy arrays need to have?

    Additional Info - My images are 60 (height) by 160 (width) pixels each and each of them have 5 alphanumeric characters. Here is a sample image:

    sample image.

    Each label is a 5 by 62 array.

  • Keshav Choudhary
    Keshav Choudhary about 8 years
    does this method allow me to use the in built tensor flow functions such as next batch?
  • Sung Kim
    Sung Kim about 8 years
    @KeshavChoudhary next batch in MNIST? Sure, but you need to modify a bit for your dataset.
  • Keshav Choudhary
    Keshav Choudhary about 8 years
    I want to build a dataset like MNIST but with my own images, I am a beginner to TensorFlow and neural networks. What is the easiest way to create a dataset like MNIST so I can follow the basic tutorial ?
  • Chaine
    Chaine almost 7 years
    there is no tf.pack
  • Ali
    Ali almost 7 years
    In the parantheses right aftet tf.pack I have mentioned that you should use tf.stack in newer versions if TensorFlow!