本文整理汇总了Python中tensorflow.decode_csv方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.decode_csv方法的具体用法?Python tensorflow.decode_csv怎么用?Python tensorflow.decode_csv使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.decode_csv方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _decode_csv
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def _decode_csv(line):
"""Takes the string input tensor and returns a dict of rank-2 tensors."""
# Takes a rank-1 tensor and converts it into rank-2 tensor
# Example if the data is ['csv,line,1', 'csv,line,2', ..] to
# [['csv,line,1'], ['csv,line,2']] which after parsing will result in a
# tuple of tensors: [['csv'], ['csv']], [['line'], ['line']], [[1], [2]]
row_columns = tf.expand_dims(line, -1)
columns = tf.decode_csv(
row_columns, record_defaults=constants.CSV_COLUMN_DEFAULTS)
features = dict(zip(constants.CSV_COLUMNS, columns))
# Remove unused columns
unused_columns = set(constants.CSV_COLUMNS) - {col.name for col in
featurizer.INPUT_COLUMNS} - {
constants.LABEL_COLUMN}
for col in unused_columns:
features.pop(col)
return features
示例2: get_input_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def get_input_fn(csv_path, mode=tf.estimator.ModeKeys.TRAIN, batch_size=32, cutoff=5):
def input_fn():
def parse_csv(value):
columns = tf.decode_csv(value, DEFAULTS)
features = dict(zip(COLUMNS, columns))
label = features.pop(LABEL_COL)
label = tf.math.greater_equal(label, cutoff)
return features, label
# read, parse, shuffle and batch dataset
dataset = tf.data.TextLineDataset(csv_path).skip(1) # skip header
if mode == tf.estimator.ModeKeys.TRAIN:
# shuffle and repeat
dataset = dataset.shuffle(16 * batch_size).repeat()
dataset = dataset.map(parse_csv, num_parallel_calls=8)
dataset = dataset.batch(batch_size)
return dataset
return input_fn
示例3: tf_csv_dataset
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def tf_csv_dataset(csv_path, label_col, col_defaults, shuffle=False, batch_size=32):
df = dd.read_csv(csv_path)
# use col_defaults if specified for col, else use defaults base on col type
type_defaults = {np.int64: 0, np.float64: 0.0, np.object_: ""}
record_defaults = [[col_defaults.get(col_name, type_defaults.get(col_type.type, ""))]
for col_name, col_type in df.dtypes.items()]
def parse_csv(value):
columns = tf.decode_csv(value, record_defaults)
features = dict(zip(df.columns.tolist(), columns))
label = features[label_col]
return features, label
# read, parse, shuffle and batch dataset
dataset = tf.data.TextLineDataset(csv_path).skip(1) # skip header
if shuffle:
dataset = dataset.shuffle(buffer_size=1024)
dataset = dataset.map(parse_csv, num_parallel_calls=8)
dataset = dataset.batch(batch_size)
return dataset
示例4: load_train_dataset
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def load_train_dataset(dataset_location, batch_size, num_epochs):
"""Load the training data using TF Dataset API"""
with tf.name_scope('train_dataset_loading'):
record_defaults = [[1], [1], [0.]] # Sets the type of the resulting tensors and default values
# Dataset is in the format - UserID ProductID Rating
dataset = tf.data.TextLineDataset(dataset_location).map(lambda line: tf.decode_csv(line, record_defaults=record_defaults))
dataset = dataset.shuffle(buffer_size=10000)
dataset = dataset.batch(batch_size)
dataset = dataset.prefetch(5)
dataset = dataset.cache()
dataset = dataset.repeat(num_epochs)
iterator = dataset.make_one_shot_iterator()
user_batch, product_batch, label_batch = iterator.get_next()
label_batch = tf.expand_dims(label_batch, 1)
return user_batch, product_batch, label_batch
示例5: my_input_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def my_input_fn(file_path, perform_shuffle=False, repeat_count=1):
def decode_csv(line):
parsed_line = tf.decode_csv(line, [[0.], [0.], [0.], [0.], [0]])
label = parsed_line[-1] # Last element is the label
del parsed_line[-1] # Delete last element
features = parsed_line # Everything but last elements are the features
d = dict(zip(feature_names, features)), label
return d
dataset = (tf.data.TextLineDataset(file_path) # Read text file
.skip(1) # Skip header row
.map(decode_csv)) # Transform each elem by applying decode_csv fn
if perform_shuffle:
# Randomizes input using a window of 256 elements (read into memory)
dataset = dataset.shuffle(buffer_size=256)
dataset = dataset.repeat(repeat_count) # Repeats dataset this # times
dataset = dataset.batch(32) # Batch size to use
iterator = dataset.make_one_shot_iterator()
batch_features, batch_labels = iterator.get_next()
return batch_features, batch_labels
示例6: my_input_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def my_input_fn(file_path, repeat_count=1, shuffle_count=1):
def decode_csv(line):
parsed_line = tf.decode_csv(line, [[0.], [0.], [0.], [0.], [0]])
label = parsed_line[-1] # Last element is the label
del parsed_line[-1] # Delete last element
features = parsed_line # Everything but last elements are the features
d = dict(zip(feature_names, features)), label
return d
dataset = (tf.data.TextLineDataset(file_path) # Read text file
.skip(1) # Skip header row
.map(decode_csv, num_parallel_calls=4) # Decode each line
.cache() # Warning: Caches entire dataset, can cause out of memory
.shuffle(shuffle_count) # Randomize elems (1 == no operation)
.repeat(repeat_count) # Repeats dataset this # times
.batch(32)
.prefetch(1) # Make sure you always have 1 batch ready to serve
)
iterator = dataset.make_one_shot_iterator()
batch_features, batch_labels = iterator.get_next()
return batch_features, batch_labels
示例7: _voc_seg_load_file
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def _voc_seg_load_file(path, epochs=None, shuffle=True, seed=0):
PASCAL_ROOT = os.environ['VOC_DIR']
filename_queue = tf.train.string_input_producer([path],
num_epochs=epochs, shuffle=shuffle, seed=seed)
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
image_path, seg_path = tf.decode_csv(value, record_defaults=[[''], ['']], field_delim=' ')
image_abspath = PASCAL_ROOT + image_path
seg_abspath = PASCAL_ROOT + seg_path
image_content = tf.read_file(image_abspath)
image = decode_image(image_content, channels=3)
image.set_shape([None, None, 3])
imgshape = tf.shape(image)[:2]
imgname = image_path
seg_content = tf.read_file(seg_abspath)
seg = tf.cast(tf.image.decode_png(seg_content, channels=1), tf.int32)
return image, seg, imgshape, imgname
示例8: _imagenet_load_file
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def _imagenet_load_file(path, epochs=None, shuffle=True, seed=0, subset='train', prepare_path=True):
IMAGENET_ROOT = os.environ.get('IMAGENET_DIR', '')
if not isinstance(path, list):
path = [path]
filename_queue = tf.train.string_input_producer(path,
num_epochs=epochs, shuffle=shuffle, seed=seed)
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
image_path, label_str = tf.decode_csv(value, record_defaults=[[''], ['']], field_delim=' ')
if prepare_path:
image_abspath = IMAGENET_ROOT + '/images/' + subset + image_path
else:
image_abspath = image_path
image_content = tf.read_file(image_abspath)
image = decode_image(image_content, channels=3)
image.set_shape([None, None, 3])
imgshape = tf.shape(image)[:2]
label = tf.string_to_number(label_str, out_type=tf.int32)
return image, label, imgshape, image_path
示例9: _relpath_no_label_load_file
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def _relpath_no_label_load_file(path, root_path, epochs=None, shuffle=True, seed=0):
filename_queue = tf.train.string_input_producer([path],
num_epochs=epochs, shuffle=shuffle, seed=seed)
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
#image_path, = tf.decode_csv(value, record_defaults=[['']], field_delim=' ')
image_path = value
image_abspath = root_path + '/' + image_path
image_content = tf.read_file(image_abspath)
image = decode_image(image_content, channels=3)
image.set_shape([None, None, 3])
imgshape = tf.shape(image)[:2]
return image, imgshape, image_path
示例10: _abspath_no_label_load_file
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def _abspath_no_label_load_file(path, epochs=None, shuffle=True, seed=0):
filename_queue = tf.train.string_input_producer([path],
num_epochs=epochs, shuffle=shuffle, seed=seed)
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
#image_path, = tf.decode_csv(value, record_defaults=[['']], field_delim=' ')
image_path = value
image_abspath = image_path
image_content = tf.read_file(image_abspath)
image = decode_image(image_content, channels=3)
image.set_shape([None, None, 3])
imgshape = tf.shape(image)[:2]
return image, imgshape, image_path
示例11: _test
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def _test(self, args, expected_out=None, expected_err_re=None):
with self.test_session() as sess:
decode = tf.decode_csv(**args)
if expected_err_re is None:
out = sess.run(decode)
for i, field in enumerate(out):
if field.dtype == np.float32:
self.assertAllClose(field, expected_out[i])
else:
self.assertAllEqual(field, expected_out[i])
else:
with self.assertRaisesOpError(expected_err_re):
sess.run(decode)
示例12: testManagedEndOfInputOneQueue
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def testManagedEndOfInputOneQueue(self):
# Tests that the supervisor finishes without an error when using
# a fixed number of epochs, reading from a single queue.
logdir = _test_dir("managed_end_of_input_one_queue")
os.makedirs(logdir)
data_path = self._csv_data(logdir)
with tf.Graph().as_default():
# Create an input pipeline that reads the file 3 times.
filename_queue = tf.train.string_input_producer([data_path], num_epochs=3)
reader = tf.TextLineReader()
_, csv = reader.read(filename_queue)
rec = tf.decode_csv(csv, record_defaults=[[1], [1], [1]])
sv = tf.train.Supervisor(logdir=logdir)
with sv.managed_session("") as sess:
while not sv.should_stop():
sess.run(rec)
示例13: testManagedEndOfInputTwoQueues
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def testManagedEndOfInputTwoQueues(self):
# Tests that the supervisor finishes without an error when using
# a fixed number of epochs, reading from two queues, the second
# one producing a batch from the first one.
logdir = _test_dir("managed_end_of_input_two_queues")
os.makedirs(logdir)
data_path = self._csv_data(logdir)
with tf.Graph().as_default():
# Create an input pipeline that reads the file 3 times.
filename_queue = tf.train.string_input_producer([data_path], num_epochs=3)
reader = tf.TextLineReader()
_, csv = reader.read(filename_queue)
rec = tf.decode_csv(csv, record_defaults=[[1], [1], [1]])
shuff_rec = tf.train.shuffle_batch(rec, 1, 6, 4)
sv = tf.train.Supervisor(logdir=logdir)
with sv.managed_session("") as sess:
while not sv.should_stop():
sess.run(shuff_rec)
示例14: testManagedMainErrorTwoQueues
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def testManagedMainErrorTwoQueues(self):
# Tests that the supervisor correctly raises a main loop
# error even when using multiple queues for input.
logdir = _test_dir("managed_main_error_two_queues")
os.makedirs(logdir)
data_path = self._csv_data(logdir)
with self.assertRaisesRegexp(RuntimeError, "fail at step 3"):
with tf.Graph().as_default():
# Create an input pipeline that reads the file 3 times.
filename_queue = tf.train.string_input_producer([data_path],
num_epochs=3)
reader = tf.TextLineReader()
_, csv = reader.read(filename_queue)
rec = tf.decode_csv(csv, record_defaults=[[1], [1], [1]])
shuff_rec = tf.train.shuffle_batch(rec, 1, 6, 4)
sv = tf.train.Supervisor(logdir=logdir)
with sv.managed_session("") as sess:
for step in range(9):
if sv.should_stop():
break
elif step == 3:
raise RuntimeError("fail at step 3")
else:
sess.run(shuff_rec)
示例15: read_image_and_label
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import decode_csv [as 别名]
def read_image_and_label(image_label_q):
# Returns three Tensors: the decoded PNG image, the hour, and the minute.
filename, hour_str, minute_str = tf.decode_csv(
image_label_q.dequeue(), [[""], [""], [""]], " ")
file_contents = tf.read_file(filename)
# Decode image from PNG, and cast it to a float.
example = tf.image.decode_png(file_contents, channels=image_channels)
image = tf.cast(example, tf.float32)
# Set the tensor size manually from the image.
image.set_shape([image_size, image_size, image_channels])
# Do per-image whitening (zero mean, unit standard deviation). Without this,
# the learning algorithm diverges almost immediately because the gradient is
# too big.
image = tf.image.per_image_whitening(image)
# The label should be an integer.
hour = tf.string_to_number(hour_str, out_type=tf.int32)
minute = tf.string_to_number(minute_str, out_type=tf.int32)
return image, hour, minute