问题描述
我有一个 TensorFlow 数据集,其中包含近 15000 张彩色图像,分辨率为 168*84,每张图像都有标签.它的类型和形状是这样的:
I have a TensorFlow dataset which contains nearly 15000 multicolored images with 168*84 resolution and label for each image. Its type and shape are like this:
< ConcatenateDataset shapes: ((168, 84, 3), ()), types: (tf.float32, tf.int32)>
我需要用它来训练我的网络.这就是为什么我需要将它作为参数传递给我在其中构建图层的函数:
I need to use it to train my network.That's why I need to pass it as parameter to this function that I built my layers in:
def cnn_model_fn(features, labels, mode):
input_layer = tf.reshape(features["x"], [-1, 168, 84, 3])
# Convolutional Layer #1
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu)
.
.
.
我尝试使用 tf.eval() 和 np.ravel() 将每个张量转换为 np.array(我猜这是上面函数的正确类型).但我失败了.
I tried to convert each tensor into np.array(which is the proper type for the function above, i guess) by using tf.eval() and np.ravel(). But I failed.
那么,我怎样才能将此数据集转换为正确的类型以将其传递给函数?
So, how can I convert this dataset into the proper type to pass it to the function?
加号
我是 python 和 tensorflow 的新手,如果我们不能直接使用它们来构建层,我想我不明白为什么会有数据集(顺便说一下,我正在关注 TensorFlow 网站上的教程).
I am new to python and tensorflow and I don't think I understand why there are datasets if we can not use them directly to build layers(I am following the tutorial in TensorFlow's website btw).
谢谢.
推荐答案
这听起来不像是您使用 Tensorflow Dataset 管道进行设置,这里是这样做的指南:
It doesn't sound like you set up things using the Tensorflow Dataset pipeline, here is the guide for doing so:
https://www.tensorflow.org/programmers_guide/datasets
您可以遵循该方法(这是正确的方法,但要习惯它需要很小的学习曲线),或者您可以将 numpy 数组传递给 sess.run
作为feed_dict
参数.如果您采用这种方式,那么您应该只创建一个 tf.placeholder
,它将由 feed_dict
中的值填充.此处的许多基本教程示例都遵循这种方法:
You can either follow that (it's the right approach, but there's a small learning curve to get used to it), or you can just pass in the numpy array to sess.run
as part of the feed_dict
parameter. If you go this way then you should just create a tf.placeholder
which will be populated by the value in feed_dict
. Many of the basic tutorial examples here follow this approach:
https://github.com/aymericdamien/TensorFlow-Examples
这篇关于如何将 Tensorflow 数据集转换为 2D numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!