我正在 Tensorflow 中通过不同长度的数字序列数据集训练循环神经网络,并一直在尝试使用tf.data
用于创建高效管道的 API。但是我似乎无法让这个东西发挥作用
我的方法
我的数据集是一个 NumPy 形状数组[10000, ?, 32, 2]
它作为文件保存在我的磁盘上.npy
格式。这里的?
表示元素在第二维中具有可变长度。 10000 表示数据集中的小批量的数量,32 表示小批量的大小。
我在用np.load
打开这个数据集,我正在尝试创建一个tf.data.Dataset
对象使用from_tensor_slices
方法,但似乎只有在所有输入张量具有相同形状时才有效!
我尝试阅读docs但他们只给出了一个非常简单的例子。
My code
因此 numpy 文件已生成如下 -
dataset = []
for i in xrange(num_items):
#add an element of shape [?, 32, 2] to the list where `?` takes
# a random value between [1, 40]
dataset.append(generate_random_rnn_input())
with open('data.npy', 'w') as f:
np.save(f, dataset)
下面给出的代码是我尝试创建一个tf.data.Dataset
object
# dataset_list is a list containing `num_items` number of itesm
# and each item has shape [?, 32, 2]
dataset_list = np.load('data.npy')
# error, this doesn't work!
dataset = tf.data.Dataset.from_tensor_slices(dataset_list)
我得到的错误是“TypeError:预期的二进制或unicode字符串,得到数组([[[0.0875, 0.], ...”
继续,仍然需要帮助!
所以我尝试了@mrry的答案,现在我可以创建一个数据集对象。However,我无法按照教程中所述使用迭代器迭代此数据集。这就是我的代码现在的样子 -
dataset_list = np.load('data.npy')
dataset = tf.data.Dataset.from_generator(lambda: dataset_list,
dataset_list[0].dtype,
tf.TensorShape([None, 32, 2]))
dataset = dataset.map(lambda x : tf.cast(x, tf.float32))
iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()
with tf.Session() as sess:
print sess.run(next_element) # The code fails on this line
我得到的错误是AttributeError: 'numpy.dtype' object has no attribute 'as_numpy_dtype'
。我完全不知道这意味着什么。
这是完整的堆栈跟踪 -
2018-05-15 04:19:25.559922: W tensorflow/core/framework/op_kernel.cc:1261] Unknown: exceptions.AttributeError: 'numpy.dtype' object has no attribute 'as_numpy_dtype'
Traceback (most recent call last):
File "/home/vastolorde95/virtualenvs/thesis/local/lib/python2.7/site-packages/tensorflow/python/ops/script_ops.py", line 147, in __call__
ret = func(*args)
File "/home/vastolorde95/virtualenvs/thesis/local/lib/python2.7/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 378, in generator_py_func
nest.flatten_up_to(output_types, values), flattened_types)
AttributeError: 'numpy.dtype' object has no attribute 'as_numpy_dtype'
2018-05-15 04:19:25.559989: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at iterator_ops.cc:891 : Unknown: exceptions.AttributeError: 'numpy.dtype' object has no attribute 'as_numpy_dtype'
Traceback (most recent call last):
File "/home/vastolorde95/virtualenvs/thesis/local/lib/python2.7/site-packages/tensorflow/python/ops/script_ops.py", line 147, in __call__
ret = func(*args)
File "/home/vastolorde95/virtualenvs/thesis/local/lib/python2.7/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 378, in generator_py_func
nest.flatten_up_to(output_types, values), flattened_types)
AttributeError: 'numpy.dtype' object has no attribute 'as_numpy_dtype'
[[Node: PyFunc = PyFunc[Tin=[DT_INT64], Tout=[DT_DOUBLE], token="pyfunc_1"](arg0)]]
Traceback (most recent call last):
File "pipeline_test.py", line 320, in <module>
tf.app.run()
File "/home/vastolorde95/virtualenvs/thesis/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 126, in run
_sys.exit(main(argv))
File "pipeline_test.py", line 316, in main
train(FLAGS.num_training_iterations, FLAGS.report_interval, FLAGS.report_interval_verbose)
File "pipeline_test.py", line 120, in train
print(sess.run(next_element))
File "/home/vastolorde95/virtualenvs/thesis/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 905, in run
run_metadata_ptr)
File "/home/vastolorde95/virtualenvs/thesis/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1140, in _run
feed_dict_tensor, options, run_metadata)
File "/home/vastolorde95/virtualenvs/thesis/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run
run_metadata)
File "/home/vastolorde95/virtualenvs/thesis/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.UnknownError: exceptions.AttributeError: 'numpy.dtype' object has no attribute 'as_numpy_dtype'
Traceback (most recent call last):
File "/home/vastolorde95/virtualenvs/thesis/local/lib/python2.7/site-packages/tensorflow/python/ops/script_ops.py", line 147, in __call__
ret = func(*args)
File "/home/vastolorde95/virtualenvs/thesis/local/lib/python2.7/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 378, in generator_py_func
nest.flatten_up_to(output_types, values), flattened_types)
AttributeError: 'numpy.dtype' object has no attribute 'as_numpy_dtype'
[[Node: PyFunc = PyFunc[Tin=[DT_INT64], Tout=[DT_DOUBLE], token="pyfunc_1"](arg0)]]
[[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[?,32,2]], output_types=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](OneShotIterator)]]