我正在训练一个张量流的递归神经网络,在一个由不同长度的数字序列组成的数据集上,我一直在尝试使用tf.data
API来创建一个有效的管道。不过,我好像不能让这东西工作
我的数据集是一个形状为[10000, ?, 32, 2]
的NumPy数组,它以.npy
格式保存在磁盘上。这里的?
表示元素在第二维度中具有可变长度。10000表示数据集中的小批量数量,32表示小批量的大小。在
我正在使用np.load
打开这个数据集,我试图使用from_tensor_slices
方法创建一个tf.data.dataset
对象,但似乎只有当所有输入张量都具有相同的形状时,这才有效!在
我试着读了docs,但他们只给出了一个非常简单的例子。在
所以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
对象的尝试
我得到的错误是“TypeError:Expected binary or unicode string,get array([[[0.0875,0。],…“
所以我尝试了@mry的答案,现在我可以创建一个数据集对象了。但是我不能像教程中所说的那样使用迭代器迭代这个数据集。这就是我现在的代码-
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)]]
正如您所注意到的,^{} 只适用于可以转换为(密集)} ,如下所示:
tf.Tensor
或tf.SparseTensor
的对象。将可变长度NumPy数据放入Dataset
的最简单方法是使用^{相关问题 更多 >
编程相关推荐