假设您有一个具有值和标签的 tensorflow 数据集。在我的情况下,我从时间序列创建它:
f = pd.read_csv('MY.csv', index_col=0, parse_dates=True)
#extract the column we are interested in
single_col = df[['Close']]
#Convert to TFDataset
WINDOW_SIZE = 10
dataset = tf.data.Dataset.from_tensor_slices((single_col_df.values))
d = dataset.window(WINDOW_SIZE, shift=1, drop_remainder=True)
d2 = d.flat_map(lambda window: window.batch(WINDOW_SIZE+1))
#create data and ground truth
d3 = d2.map(lambda window: (window[:-1], window[-1:]))
#get the total data and shuffle
len_ds = 0
for item in d2:
len_ds +=1
d_shuffled = d3.shuffle(buffer_size=len_ds)
# split train/test
train_size = int(0.7 * len_ds)
val_size = int(0.15 * len_ds)
test_size = int(0.15 * len_ds)
train_dataset = d_shuffled.take(train_size)
test_dataset = d_shuffled.skip(train_size)
val_dataset = test_dataset.skip(test_size)
test_dataset = test_dataset.take(test_size)
train_dataset = train_dataset.batch(32).prefetch(2)
val_dataset = val_dataset.batch(32)
现在出于评估目的,我想获得测试的真实值,所以我正在运行
y = np.concatenate([y for x, y in test_dataset], axis=0)
但这会在每次数组不同排序时返回,因此无法与模型预测的模型进行比较。例如,当在 jupyter notebook 中运行上述行并将前 5 个值打印y
为 `y[:5] 时,有一次我得到
array([[26.04000092],
[16.39999962],
[18.98999977],
[42.31000137],
[19.82999992]])
另一个我得到
array([[15.86999989],
[43.27999878],
[19.32999992],
[48.38000107],
[17.12000084]])
但长度y
保持不变,所以我假设元素只是随机播放。无论如何,我无法将这些值与预测值进行比较,因为它们的顺序不同:
y_hat = model.predict(test_dataset)
此外,我也得到了不同的评估结果。例如,
x = []
y = []
for _x,_y in test_dataset:
x.append(_x)
y.append(_y)
x = np.array(x)
y = np.array(y)
model.evaluate(x=x, y=y)
每次定义数组x
并y
重新执行循环时,我都会得到不同x
的y
数组,从而导致不同的评估结果。