我试图提取使用tf.keras.preprocessing.timeseries_dataset_from_array
窗口化的1D信号的某些特征,从中我获得了一个tf.data.Dataset
对象ds
(参见下面的代码)。理想情况下,我希望使用数据集的内置map
方法将特征函数(使用numpy和scipy)Map到数据集上。
然而,当我天真地尝试这样做时:
import numpy as np
import scipy as sc
import tensorflow as tf
def feat1_func(x, sf, axis):
x = np.asarray(x)
feat1_value = np.apply_along_axis(
lambda vals: sc.integrate.trapezoid(abs(vals), dx=1 / sf), axis=axis, arr=x
)
return feat1_value
features = ['feat1']
feature_map = {'feat1': feat1_func}
x = np.random.rand(100, 5)
y = np.random.randint(low=0, high=2, size=100)
sequence_length = 10
sequence_stride = 3
ds = tf.keras.preprocessing.timeseries_dataset_from_array(
data=x,
targets=y,
sequence_length=sequence_length,
sequence_stride=sequence_stride,
batch_size=None,
shuffle=False,
)
feat_lambda = lambda x, y: (np.array([feature_map[ft](x, sf=1000, axis=0) for ft in features]), y)
ds = ds.map(feat_lambda)
字符串
我收到以下错误消息:
NotImplementedError: Cannot convert a symbolic tf.Tensor (args_0:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported.
型
这个问题最简单的解决方法是什么?当Map发生时,是否可以将符号Tensor转换为渴望Tensor?
1条答案
按热度按时间mbskvtky1#
解决的办法是改变路线
字符串
到
型
tf.numpy_function
接受一个处理numpy数组的函数,并以渴望模式处理Dataset
的Tensor(也就是具有真实的值的Tensor,而不是符号Tensor)。ds = ds.map(feat_lambda)
在tf.py_function
上也没有错误,但是当我试图循环数据集时,我得到了错误:型