- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 Tensorflow 的数据集 API 从多个 csv 文件中读取数据。
以下版本的代码工作正常:
record_defaults = [[""], [0.0], [0.0], [0.0], [0.0], [0.0], [0.]]
def decode_csv(line):
col1, col2, col3, col4, col5, col6, col7 = tf.decode_csv(line, record_defaults)
features = tf.stack([col2, col3, col4, col5, col6])
labels = tf.stack([col7])
return features, labels
filenames = tf.placeholder(tf.string, shape=[None])
dataset5 = tf.data.Dataset.from_tensor_slices(filenames)
dataset5 = dataset5.flat_map(lambda filename: tf.data.TextLineDataset(filename).skip(1).map(decode_csv))
dataset5 = dataset5.shuffle(buffer_size=1000)
dataset5 = dataset5.batch(7)
iterator5 = dataset5.make_initializable_iterator()
但我想让它更具动态性,因为#columns(#features)可能会在不同的项目中发生变化。但是当我如下更改代码时,它就不起作用了。在这个问题上花费大量时间也无济于事..
record_defaults = [[""], [0.0], [0.0], [0.0], [0.0], [0.0], [0.]]
def decode_csv(line):
csv_columns = tf.decode_csv(line, record_defaults)
labels = csv_columns[-1] # last column is the label
del csv_columns[-1] # delete the last column
del csv_columns[0] # delete the first column bcz not a feature
features = csv_columns
return features, labels
filenames = tf.placeholder(tf.string, shape=[None])
dataset5 = tf.data.Dataset.from_tensor_slices(filenames)
dataset5 = dataset5.flat_map(lambda filename: tf.data.TextLineDataset(filename).skip(1).map(decode_csv))
dataset5 = dataset5.shuffle(buffer_size=1000)
dataset5 = dataset5.batch(7)
iterator5 = dataset5.make_initializable_iterator()
当我运行上面的第二个版本时出现以下错误..也许更有经验的人在这里立即看到问题..?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-21-92ea8cc44da0> in <module>()
18 filenames = tf.placeholder(tf.string, shape=[None])
19 dataset5 = tf.data.Dataset.from_tensor_slices(filenames)
---> 20 dataset5 = dataset5.flat_map(lambda filename: tf.data.TextLineDataset(filename).skip(1).map(decode_csv))
21 dataset5 = dataset5.shuffle(buffer_size=1000)
22 dataset5 = dataset5.batch(7)
~/.local/lib/python3.5/site-packages/tensorflow/python/data/ops/dataset_ops.py in flat_map(self, map_func)
799 Dataset: A `Dataset`.
800 """
--> 801 return FlatMapDataset(self, map_func)
802
803 def interleave(self, map_func, cycle_length, block_length=1):
~/.local/lib/python3.5/site-packages/tensorflow/python/data/ops/dataset_ops.py in __init__(self, input_dataset, map_func)
1676
1677 self._map_func = tf_map_func
-> 1678 self._map_func.add_to_graph(ops.get_default_graph())
1679
1680 def _as_variant_tensor(self):
~/.local/lib/python3.5/site-packages/tensorflow/python/framework/function.py in add_to_graph(self, g)
484 def add_to_graph(self, g):
485 """Adds this function into the graph g."""
--> 486 self._create_definition_if_needed()
487
488 # Adds this function into 'g'.
~/.local/lib/python3.5/site-packages/tensorflow/python/framework/function.py in _create_definition_if_needed(self)
319 """Creates the function definition if it's not created yet."""
320 with context.graph_mode():
--> 321 self._create_definition_if_needed_impl()
322
323 def _create_definition_if_needed_impl(self):
~/.local/lib/python3.5/site-packages/tensorflow/python/framework/function.py in _create_definition_if_needed_impl(self)
336 # Call func and gather the output tensors.
337 with vs.variable_scope("", custom_getter=temp_graph.getvar):
--> 338 outputs = self._func(*inputs)
339
340 # There is no way of distinguishing between a function not returning
~/.local/lib/python3.5/site-packages/tensorflow/python/data/ops/dataset_ops.py in tf_map_func(*args)
1664 dataset = map_func(*nested_args)
1665 else:
-> 1666 dataset = map_func(nested_args)
1667
1668 if not isinstance(dataset, Dataset):
<ipython-input-21-92ea8cc44da0> in <lambda>(filename)
18 filenames = tf.placeholder(tf.string, shape=[None])
19 dataset5 = tf.data.Dataset.from_tensor_slices(filenames)
---> 20 dataset5 = dataset5.flat_map(lambda filename: tf.data.TextLineDataset(filename).skip(1).map(decode_csv))
21 dataset5 = dataset5.shuffle(buffer_size=1000)
22 dataset5 = dataset5.batch(7)
~/.local/lib/python3.5/site-packages/tensorflow/python/data/ops/dataset_ops.py in map(self, map_func, num_parallel_calls)
784 """
785 if num_parallel_calls is None:
--> 786 return MapDataset(self, map_func)
787 else:
788 return ParallelMapDataset(self, map_func, num_parallel_calls)
~/.local/lib/python3.5/site-packages/tensorflow/python/data/ops/dataset_ops.py in __init__(self, input_dataset, map_func)
1587
1588 self._map_func = tf_map_func
-> 1589 self._map_func.add_to_graph(ops.get_default_graph())
1590
1591 def _as_variant_tensor(self):
~/.local/lib/python3.5/site-packages/tensorflow/python/framework/function.py in add_to_graph(self, g)
484 def add_to_graph(self, g):
485 """Adds this function into the graph g."""
--> 486 self._create_definition_if_needed()
487
488 # Adds this function into 'g'.
~/.local/lib/python3.5/site-packages/tensorflow/python/framework/function.py in _create_definition_if_needed(self)
319 """Creates the function definition if it's not created yet."""
320 with context.graph_mode():
--> 321 self._create_definition_if_needed_impl()
322
323 def _create_definition_if_needed_impl(self):
~/.local/lib/python3.5/site-packages/tensorflow/python/framework/function.py in _create_definition_if_needed_impl(self)
336 # Call func and gather the output tensors.
337 with vs.variable_scope("", custom_getter=temp_graph.getvar):
--> 338 outputs = self._func(*inputs)
339
340 # There is no way of distinguishing between a function not returning
~/.local/lib/python3.5/site-packages/tensorflow/python/data/ops/dataset_ops.py in tf_map_func(*args)
1575 self._output_classes = sparse.get_classes(ret)
1576 self._output_shapes = nest.pack_sequence_as(
-> 1577 ret, [t.get_shape() for t in nest.flatten(ret)])
1578 self._output_types = nest.pack_sequence_as(
1579 ret, [t.dtype for t in nest.flatten(ret)])
~/.local/lib/python3.5/site-packages/tensorflow/python/data/ops/dataset_ops.py in <listcomp>(.0)
1575 self._output_classes = sparse.get_classes(ret)
1576 self._output_shapes = nest.pack_sequence_as(
-> 1577 ret, [t.get_shape() for t in nest.flatten(ret)])
1578 self._output_types = nest.pack_sequence_as(
1579 ret, [t.dtype for t in nest.flatten(ret)])
AttributeError: 'list' object has no attribute 'get_shape'
附录:
以下也适用。
feature_names = ['f0','f1','f2','f3','f4','f5']
record_defaults = [[""], [0.0], [0.0], [0.0], [0.0], [0.0], [0.]]
def decode_csv(line):
parsed_line = tf.decode_csv(line, record_defaults) # => tensor
label = parsed_line[-1]
del parsed_line[-1]
features = parsed_line
d = dict(zip(feature_names,features)),label
return d
filenames = tf.placeholder(tf.string, shape=[None])
dataset5 = tf.data.Dataset.from_tensor_slices(filenames)
dataset5 = dataset5.flat_map(lambda filename: tf.data.TextLineDataset(filename).skip(1).map(decode_csv))
dataset5 = dataset5.shuffle(buffer_size=1000)
dataset5 = dataset5.batch(7)
iterator5 = dataset5.make_initializable_iterator()
但现在 decode_csv 函数正在返回一个 (feature_name,feature_value) 对的字典。为什么有人喜欢从这个函数返回一个字典?向量化计算(如前向传播等)不会变得非常困难吗?
最佳答案
已解决。以下是工作版本。我不是为了节省一些空间而复制整个东西。在excel文件中,第一列不是特征,只是训练样例ID。最后一列只是标签。使用 tf.stack(...) 函数堆叠特征解决了这个问题。
feature_names = ['f1','f2','f3','f4','f5']
record_defaults = [[""], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]]
def decode_csv(line):
parsed_line = tf.decode_csv(line, record_defaults)
label = parsed_line[-1]
del parsed_line[-1]
del parsed_line[0]
features = tf.stack(parsed_line) # ADDED LINE
d = features, label
return d
filenames = tf.placeholder(tf.string, shape=[None])
dataset5 = tf.data.Dataset.from_tensor_slices(filenames)
dataset5 = dataset5.flat_map(lambda filename: tf.data.TextLineDataset(filename).skip(1).map(decode_csv))
dataset5 = dataset5.shuffle(buffer_size=1000)
dataset5 = dataset5.batch(7)
iterator5 = dataset5.make_initializable_iterator()
关于python - Tensorflow 'list' 对象没有属性 'get_shape',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49545570/
我正在尝试使用 Tensorflow 的数据集 API 从多个 csv 文件中读取数据。 以下版本的代码工作正常: record_defaults = [[""], [0.0], [0.0], [0.
当我运行时 beginner.ipynb来自 google's intro to tensorflow在本地,执行在 predictions = model(x_train[:1]).numpy()
我正在尝试创建两个顺序模型(每个模型都在不同的数据集 - 不同的图像上进行训练)。然后我想取它们输出的平均值,并添加一个 softmax 层以根据两个顺序模型给我一个单一的分类输出。我的代码在下面,但
我试图了解嵌入层如何与掩蔽一起使用(用于序列到序列回归)。 这个简单的代码失败并出现错误:AttributeError:“Embedding”对象没有属性“get_shape”。这似乎是真的,但我不知
我必须编写自己的自定义损失函数,这些损失函数可以采用 Keras 中 y_true 和 y_pred 参数以外的不同输入。在阅读了一些解决方法后,我决定按如下方式使用内部函数: from keras
我正在使用数据集 API 创建输入管道。我以类似于以下的模式使用 tf.data.Dataset.map() 方法: def mapped_fn(_): X = tf.random_unifo
我正在尝试使用 Amazon Reviews 数据集进行文本摘要。我在构建模型时遇到错误。 AttributeError: ‘LSTMStateTuple’ object has no attribu
def conv_net(x, weights, biases, dropout): # Layer 1 - 28*28*1 to 14*14*32 conv1 = conv2d(x,
简介 我正在使用 Tensorflow 教程“专家深度 MNIST”的修改版本和 Python API,用于使用卷积网络的医学图像分类项目。 我想通过对训练集的图像应用随机修改来人为地增加训练集的大小
我是一名优秀的程序员,十分优秀!