【问题标题】:Is this a bug in using Keras with tensorflow Datasets?这是将 Keras 与 tensorflow 数据集一起使用的错误吗?
【发布时间】:2018-12-13 15:54:33
【问题描述】:

或者教程的某个地方有错别字。或者我的版本中可能存在错误(请参阅注释;无法通过pip 在线升级)。或者,也许我做错了什么。 我想知道该怎么做。

This tensorflow keras tutorial with datasets 是分段呈现的,但我已尽我所能为这个问题组装它们,如下所示(请注意,我必须进行一些更改,例如“import keras layers”语句并添加input_shape 到第一层,因为没有它代码就会爆炸):

import tensorflow as tf
from tensorflow.contrib.keras.api.keras import layers
import numpy as np

print(tf.VERSION)
print(tf.keras.__version__)

data = np.random.random((1000, 32))
labels = np.random.random((1000, 10))

# Instantiates a toy dataset instance:
dataset = tf.data.Dataset.from_tensor_slices((data, labels))
dataset = dataset.batch(32)
dataset = dataset.repeat()

model = tf.keras.Sequential([
        # Adds a densely-connected layer with 64 units to the model:
        layers.Dense(64, activation='relu', input_shape=(32,)),

        # Add another:
        layers.Dense(64, activation='relu'),

        # Add a softmax layer with 10 output units:
        layers.Dense(10, activation='softmax')])

model.compile(optimizer=tf.train.AdamOptimizer(0.001),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# Don't forget to specify `steps_per_epoch` when calling `fit` on a dataset.
model.fit(dataset, epochs=10, steps_per_epoch=30)

完整输出:

1.6.0
2.1.3-tf
Traceback (most recent call last):

  File "<ipython-input-9-47ae8a0a1bac>", line 1, in <module>
    runfile('C:/Users/1134400/Documents/Python/python3/test.py', wdir='C:/Users/1134400/Documents/Python/python3')

  File "C:\Users\1134400\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Users\1134400\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/1134400/Documents/Python/python3/test.py", line 31, in <module>
    model.fit(dataset, epochs=10, steps_per_epoch=30)

  File "C:\Users\1134400\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\keras\_impl\keras\models.py", line 920, in fit
    validation_steps=validation_steps)

  File "C:\Users\1134400\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\training.py", line 1681, in fit
    batch_size=batch_size)

  File "C:\Users\1134400\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\training.py", line 1508, in _standardize_user_data
    exception_prefix='input')

  File "C:\Users\1134400\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\training.py", line 105, in _standardize_input_data
    np.expand_dims(x, 1) if x is not None and x.ndim == 1 else x for x in data

  File "C:\Users\1134400\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\training.py", line 105, in <listcomp>
    np.expand_dims(x, 1) if x is not None and x.ndim == 1 else x for x in data

AttributeError: 'RepeatDataset' object has no attribute 'ndim'

为什么这不起作用?我的 tensorflow/keras 版本中是否存在错误?我的大部分复制粘贴的代码中有错误吗?

注意事项:

  1. 版本:

    • 教程:页面显示tf.VERSION 是 1.12.0,tf.keras.__version__ 是 2.1.6-tf。
    • 我的:输出显示tf.VERSION 是 1.6.0,tf.keras.__version__ 是 2.1.3-tf。
    • 我的版本落后了。
  2. These notes for Keras 2.2.0 这样说:

    使用 Keras 2.2.0 和 TensorFlow 1.8 或更高版本,您可以使用符号 TensorFlow 张量进行拟合、评估和预测

    教程的版本在此之前,所以对tensorflow数据集的支持显然是之前的。

  3. These notes for Keras 2.07 这样说:

    更好地支持从 TensorFlow 中的数据张量训练模型(例如数据集、TFRecords)。

    教程的版本比我的要新,所以数据集支持应该已经存在。

  4. 在 Windows 上为 CPU 使用 tensorflow。不是 GPU。

  5. 在我的 Internet 连接网络上有一个限制性防火墙,(1) 我无法控制和 (2) 不允许 pip 通过,所以简单的“使用 pip 升级你的版本”的方法不在桌面上。获取新版本离线安装需要花点力气,如果我犯了错误,我不会受此影响

  6. 删除此行:dataset = dataset.repeat()。异常更改为AttributeError: 'BatchDataset' object has no attribute 'ndim'。就好像它不能使用数据集一样。

  7. 我不知道model.fit(dataset, ...) 在做什么。在我的版本中,model.fit(...) 的前两个位置参数是xy。数据集是用一个元组构造的,所以是否将数据集作为单个位置参数传递,以隐式解包特征和标签?

【问题讨论】:

  • 我有 TF 1.10.1 和 Keras 2.1.6-tf,你的代码对我来说运行良好。所以我认为它取决于版本。您可以尝试的最简单的事情是在您的代码中添加一个迭代器。也许早期版本不支持没有迭代器的直接数据集对象被馈送到 model.fit。因此,不要将数据集提供给 model.fit,而是提供 dataset.make_one_shot_iterator() 并查看它是否有效。至于更新你的包,你有没有考虑过使用虚拟环境并安装新版本来测试你的代码?
  • “所以我认为这取决于版本。”很高兴知道。尝试过的迭代器:itr = dataset.make_one_shot_iterator() | features_itr, labels_itr = itr.get_next() | ... | model.fit(x=features_itr, y=labels_itr, epochs=10, steps_per_epoch=30)。新输出错误:AttributeError: 'Tensor' object has no attribute 'ndim'。它真的想要ndim。至于制作虚拟环境,这有什么帮助?我需要查找并请求安装虚拟化软件,但我仍在防火墙后面,所以我仍然无法获得更新。
  • 您无需使用 get next 将其分解为元组。直接提供定义的一次性迭代器也可以用于将来的目的。我的意思是用 virtualenv 创建一个虚拟 python 环境,然后做离线的事情,因为你说过不想通过犯错来影响任何事情!
  • 切换到简单的itr = dataset.make_one_shot_iterator()...model.fit(itr, epochs=10, steps_per_epoch=30)` 得到这个:AttributeError: 'Iterator' object has no attribute 'ndim'。相同的错误,不同的对象。我明白你对虚拟环境的看法。我想说我不想经历离线安装的过程(简而言之,这很麻烦,可能需要一段时间),但如果问题是拼写错误,那么我会继续前进。不过,看起来这不是一个选择,所以很麻烦:/。感谢您的检查。
  • 这很难。但是,是的,我认为这是必要的。尤其是 TF 2.0 版本即将推出。所以无论如何更新东西会很好:)

标签: python-3.x tensorflow keras


【解决方案1】:

我遇到了同样的问题,我将tensorflow 更新为2.1.0 并导入keras 而不是直接导入import tensorflow.keras。使用Dataset 执行相同的损坏代码现在可以工作了

【讨论】:

    猜你喜欢
    • 2019-10-01
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多