我的数据由许多csv文件组成,每个csv文件包含一行和许多浮点数。
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)
发布于 2020-03-09 19:54:02
请根据您的说明参考工作代码
import numpy as np
import tensorflow as tf
print("Tensorflow Version:", tf.__version__)您可以使用tf.string_split和tf.string_to_number来完成此操作:
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)输出:
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']https://stackoverflow.com/questions/60584773
复制相似问题