Tensorflow:将 Tensor 转换为 numpy 数组然后传入 feed_dict

问题描述 投票:0回答:1

我正在尝试为 CIFAR 分类建立一个 softmax 回归模型。起初,当我尝试将我的图像和标签传递到 feed 字典时,我收到一个错误,提示 feed 字典不接受 Tensors。然后我使用 .eval() 将它们转换为 numpy 数组,但程序挂在 .eval() 行并且不再继续。我怎样才能将这些数据传递到 feed_dict 中?

CIFARIMAGELOADING.PY

import tensorflow as tf
import os
import tensorflow.models.image.cifar10 as cf


IMAGE_SIZE = 24
BATCH_SIZE = 128


def loadimagesandlabels(size):
    # Load the images from the CIFAR data directory
    FLAGS = tf.app.flags.FLAGS
    data_dir = os.path.join(FLAGS.data_dir, 'cifar-10-batches-bin')
    filenames = [os.path.join(data_dir, 'data_batch_%d.bin' % i) for i in xrange(1, 6)]
    filename_queue = tf.train.string_input_producer(filenames)
    read_input = cf.cifar10_input.read_cifar10(filename_queue)

    # Reshape and crop the image
    height = IMAGE_SIZE
    width = IMAGE_SIZE
    reshaped_image = tf.cast(read_input.uint8image, tf.float32)
    cropped_image = tf.random_crop(reshaped_image, [height, width, 3])

    # Generate a batch of images and labels by building up a queue of examples
    print('Filling queue with CIFAR images')
    num_preprocess_threads = 16
    min_fraction_of_examples_in_queue = 0.4
    min_queue_examples = int(BATCH_SIZE*min_fraction_of_examples_in_queue)

    images, label_batch = tf.train.batch([cropped_image,read_input.label],batch_size=BATCH_SIZE, num_threads=num_preprocess_threads, capacity=min_queue_examples+3*BATCH_SIZE)
    print(images)
    print(label_batch)
    return images, tf.reshape(label_batch, [BATCH_SIZE])

CIFAR.PY

#Set up placeholder vectors for image and labels
x = tf.placeholder(tf.float32, shape = [None, 1728])
y_ = tf.placeholder(tf.float32, shape = [None,10])
W = tf.Variable(tf.zeros([1728,10]))
b = tf.Variable(tf.zeros([10]))


#Implement regression model. Multiply input images x by weight matrix W, add the bias b
#Compute the softmax probabilities that are assigned to each class
y = tf.nn.softmax(tf.matmul(x,W) + b)

#Define cross entropy
#tf.reduce sum sums across all classes and tf.reduce_mean takes the average over these sums
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y), reduction_indices = [1]))

#Train the model
#Each training iteration we load 128 training examples. We then run the train_step operation
#using feed_dict to replace the placeholder tensors x and y_ with the training examples
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

#Open up a Session
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(1000) :
    images, labels = CIFARImageLoading.loadimagesandlabels(size=BATCH_SIZE)
    unrolled_images = tf.reshape(images, (1728, BATCH_SIZE))

    #convert labels to their one_hot representations
    # should produce [[1,0,0,...],[0,1,0...],...]
    one_hot_labels = tf.one_hot(indices= labels, depth=NUM_CLASSES, on_value=1.0, off_value= 0.0, axis=-1)

    print(unrolled_images)
    print(one_hot_labels)
    images_numpy, labels_numpy = unrolled_images.eval(session=sess), one_hot_labels.eval(session=sess)
    sess.run(train_step, feed_dict = {x: images_numpy, y_:labels_numpy})




#Evaluate the model
#.equal returns a tensor of booleans, we want to cast these as floats and then take their mean
#to get percent correctness (accuracy)
print("evaluating")
test_images, test_labels = CIFARImageLoading.loadimagesandlabels(TEST_SIZE)
test_images_unrolled = tf.reshape(test_images, (1728, TEST_SIZE))
test_images_one_hot = tf.one_hot(indices= test_labels, depth=NUM_CLASSES, on_value=1.0, off_value= 0.0, axis=-1)
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval(feed_dict = {x: unrolled_images.eval(), y_ : test_images_one_hot.eval()}))
python neural-network tensorflow mnist softmax
1个回答
0
投票

有几件事你不太了解。在整个图表中,您将使用张量。您可以通过使用

tf.placeholder
并将它们喂入
session.run(feed_dict{})
或使用
tf.Variable
并使用
session.run(tf.initialize_all_variables())
对其进行初始化来定义张量。您必须以这种方式输入您的输入,并且它应该是与您在占位符中期望的形状相同的 numpy 数组。这是一个简单的例子:

images = tf.placeholder(type, [1728, BATCH_SIZE])
labels = tf.placeholder(type, [size])

'''
    Build your network here so you have the variable: Output
'''

images_feed, labels_feed = CIFARImageLoading.loadimagesandlabels(size=BATCH_SIZE)
# here you can see your output
print sess.run(Output, feed_dict = {x: images_feed, y_:labels_feed})

你不为 tf.functions 提供 numpy 数组,你总是为它们提供张量。

feed_dict
总是使用 numpy 数组。问题是:您永远不必将张量转换为 numpy 数组作为输入,这是没有意义的。您的输入必须是 numpy 数组,如果它是一个列表,您可以使用
np.asarray(list)
,如果它是一个张量,您做错了。

我不知道

CIFARImageLoading.loadimagesandlabels
返回给你什么,但我想它不是张量,它可能已经是一个 numpy 数组,所以就摆脱这个
.eval()
.

© www.soinside.com 2019 - 2024. All rights reserved.