gpt4 book ai didi

python - 尝试导入 Tensorflow 数据集时出错

转载 作者:行者123 更新时间:2023-12-01 08:15:28 26 4
gpt4 key购买 nike

我正在关注本教程: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

我已经用 pip 安装了 Tensorflow 和 Tensorflow-Datasets API。不知道发生了什么。

最佳答案

您忘记定义datalabels变量。

正如教程所述:

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

关于python - 尝试导入 Tensorflow 数据集时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55009650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com