是否tf.data.Dataset.map()保留输入元素的顺序?
特别是什么时候num_parallel_calls > 1?
2 年前,我在 TensorFlow 中编写代码,作为数据加载的一部分,我使用了函数“load_csv_without_header”。现在,当我运行代码时,我收到消息:
WARNING:tensorflow:From C:\Users\Roi\Desktop\Code_Win_Ver\code_files\Tensor_Flow\version1\build_database_tuple.py:124: load_csv_without_header (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.data instead.
Run Code Online (Sandbox Code Playgroud)
如何使用 'tf.data' 而不是当前函数?如果没有带有 tf.data 的 csv 标头,我如何才能以相同的格式使用相同的 dtype?我在 Python 3.5 上使用 TF 版本 1.8.0。
感谢你的帮助!
我有多个输入层(20 个输入层),我想使用 atf.dataset来为模型提供数据。batch_size 是 16。不幸的model.fit(train_dataset, epochs=5)是抛出以下错误:
ValueError:检查模型输入时出错:传递给模型的 numpy 数组列表不是模型预期的大小。对于输入 ['input_2', ... , 'input_21'] 预计会看到 20 个数组,但得到以下 1 个数组的列表: [<tf.Tensor 'args_0:0' shape=(None, 20 , 512, 512, 3) dtype=int32>]...
我认为 keras 想要一个像(20,None,512,512,3)这样的形状。有人对这个问题有想法,或者如何为具有多个输入层的模型正确使用 tf.datasets 吗?
def read_tfrecord(bin_data):
for i in feature_map_dict:
label_seq[i] = tf_input_feature_selector(feature_map_dict[i])
img_seq = {'images': tf.io.FixedLenSequenceFeature([], dtype=tf.string)}
cont, seq = tf.io.parse_single_sequence_example(serialized=bin_data, context_features=label_seq, sequence_features=img_seq)
image_raw = seq['images']
images = decode_image_raw(image_raw)
images = tf.reshape(images, [20,512,512,3])
images = preprocess_input(images)
label = cont["label"]
return images, label
def get_dataset(tfrecord_path):
dataset …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 TF2 模型中发挥作用batch_size。call()但是,我无法得到它,因为我知道的所有方法都返回None或张量而不是维度元组。
这是一个简短的例子
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Model
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
def call(self, x):
print(len(x))
print(x.shape)
print(tf.size(x))
print(np.shape(x))
print(x.get_shape())
print(x.get_shape().as_list())
print(tf.rank(x))
print(tf.shape(x))
print(tf.shape(x)[0])
print(tf.shape(x)[1])
return tf.random.uniform((2, 10))
m = MyModel()
m.compile(optimizer="Adam", loss="sparse_categorical_crossentropy", metrics=['accuracy'])
m.fit(np.array([[1,2,3,4], [5,6,7,8]]), np.array([0, 1]), epochs=1)
Run Code Online (Sandbox Code Playgroud)
输出是:
Tensor("my_model_26/strided_slice:0", shape=(), dtype=int32)
(None, 4)
Tensor("my_model_26/Size:0", shape=(), dtype=int32)
(None, 4)
(None, 4)
[None, 4]
Tensor("my_model_26/Rank:0", shape=(), …Run Code Online (Sandbox Code Playgroud) keras tensorflow tensorflow-datasets batchsize tensorflow2.0
我想通过向现有数据集添加随机噪声来将我用来动态训练张量流中的神经网络的现有数据集的大小加倍。因此,当我完成后,我将拥有所有现有示例以及添加了噪音的所有示例。我还想在转换它们时将它们交错,因此它们按以下顺序出现:示例 1 无噪声,示例 1 有噪声,示例 2 无噪声,示例 2 有噪声,等等。我正在努力实现这一点使用数据集 API。我尝试使用 unbatch 来完成此操作,如下所示:
def generate_permutations(features, labels):
return [
[features, labels],
[add_noise(features), labels]
]
dataset.map(generate_permutations).apply(tf.contrib.data.unbatch())
Run Code Online (Sandbox Code Playgroud)
但我收到一条错误消息说Shapes must be equal rank, but are 2 and 1. 我猜测张量流正在尝试从我返回的批次中生成张量,但是features和labels是不同的形状,所以这是行不通的。我可能可以通过制作两个数据集并将它们连接在一起来做到这一点,但我担心这会导致非常倾斜的训练,我在一半的时期内训练得很好,突然所有数据都在第二个时期进行了这种新的转换一半。在输入张量流之前,如何在不将这些转换写入磁盘的情况下即时完成此操作?
我正在使用 TensorFlow Dataset API 来解析 CSV 文件并运行逻辑回归。我下面从TF文件的例子在这里。
以下代码片段显示了我如何设置模型:
def input_fn(path, num_epochs, batch_size):
dataset = tf.data.TextLineDataset(path)
dataset = dataset.map(parse_table, num_parallel_calls=12)
dataset = dataset.repeat(num_epochs)
dataset.batch(batch_size)
iterator = dataset.make_one_shot_iterator()
features, labels = iterator.get_next()
return features, labels
def parse_table(value):
cols = tf.decode_csv(value, record_defaults=TAB_COLUMN_DEFAULTS)
indep_vars = dict(zip(CSV_COLS, cols))
y = indep_vars.pop('y')
return indep_vars, y
def build_indep_vars():
continuous_vars = [
tf.feature_column.numeric_column(x, shape=1) for x in CONT_COLS]
categorical_vars = [
tf.feature_column.categorical_column_with_hash_bucket(
x, hash_bucket_size=100) for x in CAT_COLS]
return categorical_vars + continuous_vars
Run Code Online (Sandbox Code Playgroud)
调用时lr.train(input_fn = lambda: …
我正在使用多个 tfRecord 文件并希望从中读取以创建数据集。我正在尝试使用来自_tensor_slices 的路径并使用该数据集进一步读取 TFRecords
(多个 tfRecords 的优点:https ://datascience.stackexchange.com/questions/16318/what-is-the-benefit-of-splitting-tfrecord-file-into-shards )
我想知道是否有更简单且行之有效的方法来做到这一点。
file_names_dataset = tf.data.Dataset.from_tensor_slices(filenames_full)
def read(inp):
return tf.data.TFRecordDataset(inp)
file_content = file_names.map(read)
Run Code Online (Sandbox Code Playgroud)
我的下一步是使用 tf.io.parse_single_example 解析数据集。
我正在通过加载图像
data = keras.preprocessing.image_dataset_from_directory(
'./data',
labels='inferred',
label_mode='binary',
validation_split=0.2,
subset="training",
image_size=(img_height, img_width),
batch_size=sz_batch,
crop_to_aspect_ratio=True
)
Run Code Online (Sandbox Code Playgroud)
我也想在非张量流例程中使用获得的数据。因此,我想将数据提取到 numpy 数组中。我怎样才能实现这个目标?我不能使用tfds