【发布时间】:2020-01-12 15:35:14
【问题描述】:
我正在尝试将几个文件加载到我的管道中,每个文件包含 3 个信号,并且 3 个信号以 10 分钟的间隔排序。 当我加载第一个文件时,它具有这种形状(86、75000,3)。 我正在使用张量流 1.14
我尝试了以下代码,为了让您可以使用代码,我用零模拟加载:
import numpy as np
import tensorflow as tf
def my_func(x):
p = np.zeros([86, 75000, 3])
return p
def load_sign(path):
sign = tf.compat.v1.numpy_function(my_func, [path], tf.float64)
return sign
s = [1, 2] # list with filenames, these are paths, here i simulate with numbers
AUTOTUNE = tf.data.experimental.AUTOTUNE
ds = tf.data.Dataset.from_tensor_slices(s)
ds = ds.map(load_sign, num_parallel_calls=AUTOTUNE)
itera = tf.data.make_one_shot_iterator(ds)
x = itera.get_next()
with tf.Session() as sess:
# sess.run(itera.initializer)
va_sign = sess.run([x])
va = np.array(va_sign)
print(va.shape)
我得到这个形状:(1, 86, 75000, 3) 虽然我想获得 3 个不同的变量,每个变量都具有这种形状:(,75000)
我该怎么做? 我也试过这个代码,但我得到一个错误
import numpy as np
import tensorflow as tf
def my_func(x):
p = np.zeros([86, 75000, 3])
x = p[:,:,0]
y = p[:, :, 1]
z = p[:, :, 2]
return x, y, z
# load the signals, in my example it creates the signals using zeros
def load_sign(path):
a, b, c = tf.compat.v1.numpy_function(my_func, [path], tf.float64)
return tf.data.Dataset.zip((a,b,c))
s = [1, 2] # list with filenames, these are paths, here i simulate with numbers
AUTOTUNE = tf.data.experimental.AUTOTUNE
ds = tf.data.Dataset.from_tensor_slices(s)
ds = ds.map(load_sign, num_parallel_calls=AUTOTUNE)
itera = tf.data.make_one_shot_iterator(ds)
x, y, z = itera.get_next()
with tf.Session() as sess:
# sess.run(itera.initializer)
va_sign = sess.run([x])
va = np.array(va_sign)
print(va.shape)
在这里,我希望 x 具有这种形状:(86, 75000),但我得到了这个错误。我怎样才能让它工作?更好的是,我可以获得具有这种形状的 x (,75000)
TypeError:张量对象仅在启用急切执行时才可迭代。要迭代此张量,请使用 tf.map_fn。
【问题讨论】:
标签: python tensorflow tensorflow-datasets