首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow strings.to_number

Tensorflow strings.to_number
EN

Stack Overflow用户
提问于 2020-03-08 13:34:02
回答 1查看 183关注 0票数 0

我的数据由许多csv文件组成,每个csv文件包含一行和许多浮点数。

代码语言:javascript
复制
import tensorflow as tf

PATH = "C:\\DeepFakes\\Training\\im2data\\train\\"
#tf.config.experimental_run_functions_eagerly(False)

def process_path(file_path):

    label = tf.strings.split(file_path, "\\")[-2]
    data = tf.strings.split (tf.io.read_file(file_path),','
    try:
        data = tf.strings.to_number(
           data, out_type=tf.dtypes.float32, name=None)
    except:
        print('dddddddddddddddd')

    return data, label

file_path = PATH + "\\original\\ImA00001.csv"  
data, label = process_path(file_path)
print('data ',data, ' label ', label)

下面是一个数据示例

0.00044 0.00233 0.00572 0.00190 0.13761 0.42304 0.00027 0.00286

输出为

dddddddddddddddd tf.Tensor(b'0.0004401633\r\n0.0023351652\r\n0.0057266317\r\n0.0019061912\r\n0.13761024\r\n0.42304015\r\n0.0002711446\r\n0.0028613438\r\n',shape=(1,),dtype=string) label tf.Tensor(b‘’original‘,shape=(),dtype=string)

EN

回答 1

Stack Overflow用户

发布于 2020-03-09 19:54:02

请根据您的说明参考工作代码

代码语言:javascript
复制
import numpy as np
import tensorflow as tf 
print("Tensorflow Version:", tf.__version__)

您可以使用tf.string_split和tf.string_to_number来完成此操作:

代码语言:javascript
复制
line = tf.constant("0.00044 0.00233 0.00572 0.00190 0.13761 0.42304 0.00027 0.00286", shape=(1,))
b = tf.compat.v1.string_split(line, delimiter=" ").values
c = tf.strings.to_number(b, tf.float32)
a = np.asarray(b)

print("Given Input:",line)
print("Desired Output:",a)

输出:

代码语言:javascript
复制
Tensorflow Version: 2.1.0

Given Input: tf.Tensor([b'0.00044 0.00233 0.00572 0.00190 0.13761 0.42304 0.00027 0.00286'], shape=(1,), dtype=string)

Desired Output: [b'0.00044' b'0.00233' b'0.00572' b'0.00190' b'0.13761' b'0.42304'
 b'0.00027' b'0.00286']
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60584773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档