【问题标题】:Tensorflow: Inputting encapsulated data batch through single placeholderTensorflow:通过单个占位符输入封装的数据批次
【发布时间】:2017-09-28 09:52:02
【问题描述】:

我通常在 tensorflow 中为张量索引而苦苦挣扎。 我有图像数据和其他标量数据。我只能使用一个占位符将所有数据输入到神经网络。

图像 (img) 是形状为 (84,84,3) 的 numpy 数组,我有形状为 (2) 的数据 a 和形状为 (1) 的数据 b。

现在我创建一个样本

sample = np.reshape(np.array([img,a,b]),(3,1)) #shape (3,1)

占位符是

input = tf.placeholder(dtype=tf.float32,shape=[None] + list(sample.shape))

现在当 TF 读取一批样本时,我想检索这批图像、a 的批和 b 的批,因为它们需要在神经网络中的不同位置输入。

这是一个最小的例子:

import tensorflow as tf
from tensorflow.contrib import layers
import numpy as np

#Numpy
img = np.random.rand(84,84,3)
a = np.random.rand(2)
b = np.random.rand(1)
sample = np.reshape(np.array([img,a,b]),(3,1)) #shape (3,1)
batch = np.repeat(np.expand_dims(sample,axis=0),32,axis=0) #shape (32,3,1)

#TF
input = tf.placeholder(dtype=tf.float32,shape=[None] + list(sample.shape))

#TODO:
tf_img = tf.#get image batch from input
tf_a = tf.#get a batch from input
tf_b = tf.#get b batch from input

out = layers.convolution2d(tf_img,num_outputs=64,kernel_size=8,stride=2,activation_fn=tf.nn.relu)
out = layers.flatten(out)
out = tf.concat([out,tf_a,tf_b])
out = layers.fully_connected(out,10,activation_fn=tf.nn.relu)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    _ = sess.run(out,feed_dict={input:batch})

如何从形状为 (?,3,1) 的张量中提取输入的各个部分,使用图像数据创建嵌入并将其他两个部分连接到该输出嵌入。

有没有更好的方法来输入数据?我唯一的限制是它必须是一个占位符。

【问题讨论】:

  • 请说明为什么您只能使用一个占位符!如果您的输入数据的所有维度都已知,您可以将每个样本包装成一个形状为(None, 84*84*3+2+1) 的张量,并在图中使用slicing 和reshaping。
  • 感谢您的回答。我正在使用一个大型代码模板,并且我想尽可能少地更改它,因为它非常复杂(并且它假设单个图像输入)。我也想知道这是否可能。你能提供一个更大的例子吗?

标签: python numpy machine-learning tensorflow tensor


【解决方案1】:

这是我上面评论的完整示例:

import numpy as np
import tensorflow as tf

im_height = 84
im_width = 84
im_channels = 3
a_len = 2
b_len = 1

np_img = np.random.rand(im_height, im_width, im_channels)
np_a = np.random.rand(a_len)
np_b = np.random.rand(b_len)

# flatten the input and concatenate to a single 1D numpy array
np_sample = np.concatenate((np_img.reshape(-1), np_a.reshape(-1), np_b.reshape(-1)), axis=0)
# construct a pseudo batch
np_batch = np.repeat(np_sample[np.newaxis, :], 32, axis=0)

tf_batch = tf.placeholder(shape=(None, im_height*im_width*im_channels + a_len + b_len), dtype=tf.float32)

img_stop = im_height*im_width*im_channels
a_stop = img_stop+a_len

# you could also use tf.slice(...) here
tf_img = tf.reshape(tf_batch[:, 0:img_stop], (-1, im_height, im_width, im_channels))
tf_a = tf.reshape(tf_batch[:, img_stop:a_stop], (-1, a_len))
tf_b = tf.reshape(tf_batch[:, a_stop:], (-1, b_len))

with tf.Session() as sess:
    fetch_dict = {'img': tf_img, 'a': tf_a, 'b': tf_b}
    feed_dict = {tf_batch: np_batch}
    res = sess.run(fetch_dict, feed_dict=feed_dict)

assert(np.isclose(res['img'][0, ...], np_img).all())
assert(np.isclose(res['a'][0, :], np_a).all())
assert(np.isclose(res['b'][0, :], np_b).all())

但是,这至少与在代码中添加适当的占位符一样具有侵入性。此外,在我看来,它的可读性要差得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    相关资源
    最近更新 更多