【发布时间】: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) 的张量中提取输入的各个部分,使用图像数据创建嵌入并将其他两个部分连接到该输出嵌入。
有没有更好的方法来输入数据?我唯一的限制是它必须是一个占位符。
【问题讨论】:
-
感谢您的回答。我正在使用一个大型代码模板,并且我想尽可能少地更改它,因为它非常复杂(并且它假设单个图像输入)。我也想知道这是否可能。你能提供一个更大的例子吗?
标签: python numpy machine-learning tensorflow tensor