在tensorflow数据集中将数据类型从float32更改为float64

时间:2020-08-07 00:45:53

标签: python tensorflow machine-learning tensorflow2.0 tensorflow-datasets

我正在尝试将pos_ds数值特征的数据类型从float32更改为float 64,但是找不到正确的方法。有什么建议。我正在使用tensorflow 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

1 个答案:

答案 0 :(得分:0)

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

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