【问题标题】:Change datatype from float32 to float64 in a tensorflow dataset在张量流数据集中将数据类型从 float32 更改为 float64
【发布时间】:2020-08-07 00:45:53
【问题描述】:

我正在尝试将 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

【问题讨论】:

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


    【解决方案1】:

    您可以先将特征和 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])
    

    【讨论】:

    • 感谢您的建议,但特征数据具有混合数据类型。某些列是字符串数据类型,而其他列是浮点数。
    • 那么我请您详细说明您的问题。您要求将 float32 转换为 float64。请发布一个小数据示例,并具体说明您想要实现的目标
    猜你喜欢
    • 2019-03-16
    • 2016-06-14
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    相关资源
    最近更新 更多