尝试导入Tensorflow数据集时出错

时间:2019-03-05 18:53:07

标签: python tensorflow tensorflow-datasets

我正在学习本教程:https://www.tensorflow.org/guide/keras,并且在尝试使用tf.data.Dataset时遇到错误。

import tensorflow as tf
import tensorflow.data
import numpy as np
from tensorflow.keras import layers

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'])

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

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

我收到此错误:

Colocations handled automatically by placer.
Traceback (most recent call last):
  File "tutorial.py", line 19, in <module>
    dataset = tensorflow.data.Dataset.from_tensor_slices((data, labels))
NameError: name 'data' is not defined

我已经同时安装了Tensorflow和Tensorflow-Datasets API。不知道发生了什么,我们将不胜感激!

2 个答案:

答案 0 :(得分:1)

您忘记定义d = {'a':'1', 'b':'2', 'c':'3', 'd':'2'} {v1: [k1 for k1, v2 in d.items() if v1 == v2] for v1 in d.values()} # {'1': ['a'], '2': ['b', 'd'], '3': ['c']} data变量。

如本教程所述:

labels

答案 1 :(得分:0)

此处datalabels未定义。

您可以通过添加

来初始化datalabels
data = tf.random_uniform([1000, 32])
labels = tf.random_uniform([1000, 10])

dataset = tf.data.Dataset.from_tensor_slices((data, labels))之前