首页 > 解决方案 > 在张量流数据集中将数据类型从 float32 更改为 float64

问题描述

我正在尝试将 pos_ds 数值特征的数据类型从 float32 更改为 float 64,但无法找到正确的方法。有什么建议么。我正在使用张量流 2.2。

def make_ds(features, labels):
    ds = tf.data.Dataset.from_tensor_slices((dict(features), labels))#.cache()
    ds = ds.shuffle(BUFFER_SIZE).repeat()
    return ds

neg_ds = make_ds(neg_features, neg_labels)
pos_ds = make_ds(pos_features, pos_labels)

for features, label in pos_ds.take(1):
    print("Features:\n", features.values())
    print()
    print("Label: ", label.numpy())

输出:

Features:
dict_values([<tf.Tensor: shape=(), dtype=float32, numpy=4.89784>, <tf.Tensor: shape=(), dtype=float32, numpy=4.727388>, <tf.Tensor: shape=(), dtype=float32, numpy=4.6051702>, <tf.Tensor: shape=(), dtype=float32, numpy=4.727388>, <tf.Tensor: shape=(), dtype=float32, numpy=4.804021>, <tf.Tensor: shape=(), dtype=float32, numpy=4.882802>, <tf.Tensor: shape=(), dtype=float32, numpy=4.912655>, <tf.Tensor: shape=(), dtype=string, numpy=b'nan'>, <tf.Tensor: shape=(), dtype=string, numpy=b'nan'>, <tf.Tensor: shape=(), dtype=string, numpy=b'nan'>, <tf.Tensor: shape=(), dtype=string, numpy=b'0.0'>, <tf.Tensor: shape=(), dtype=string, numpy=b'nan'>, <tf.Tensor: shape=(), dtype=string, numpy=b'NO_DCLRD_URL'>, <tf.Tensor: shape=(), dtype=string, numpy=b'nan'>])
Label: 1

标签: pythontensorflowmachine-learningtensorflow2.0tensorflow-datasets

解决方案


您可以在调用之前先将特征和 pos_labels 转换为张量from_tensor_slices

features = np.zeros(2, dtype=np.float32)
features = tf.convert_to_tensor(features,dtype=tf.float64)
ds = tf.data.Dataset.from_tensor_slices([features])

推荐阅读