【发布时间】:2020-11-14 19:21:35
【问题描述】:
调用 keras model.fit() 时出现以下错误。
AttributeError: 'RepeatDataset' object has no attribute 'ndim'
我正在使用 TensorFlow 1.7 和 Keras。不幸的是,我必须使用 TF 1.7。知道发生了什么吗?代码,改编来自 tensorflow 演示:
import tensorflow as tf
from IPython import embed
from tensorflow.python import keras
from tensorflow.python.keras import layers
model = tf.keras.Sequential()
model.add(layers.Dense(64, input_shape=(32,), activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.compile(
optimizer=tf.train.AdamOptimizer(0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
import numpy as np
# Generate random data using numpy
def random_one_hot_labels(shape):
n, n_class = shape
classes = np.random.randint(0, n_class, n)
labels = np.zeros((n, n_class))
labels[np.arange(n), classes] = 1
return labels
data = np.random.random((1000, 32))
labels = random_one_hot_labels((1000, 10))
datasetA = tf.data.Dataset.from_tensor_slices((data, labels))
datasetB = datasetA.batch(32)
dataset = datasetB.repeat()
model.fit(
dataset,
epochs=10,
steps_per_epoch=30
)
【问题讨论】:
-
您可以尝试将参数传递给
repeat()作为repeat(count=2) -
另外,你能在创建
repeat()之后批量处理吗 -
删除
datasetB = datasetA.batch(32)并添加dataset = datasetA.repeat(<epochs>).batch(32) -
@AshwinGeetD'Sa 感谢您的想法。不,他们给出了同样的错误(再次使用 TF 1.7)
标签: python tensorflow keras tensorflow-datasets