我试图学习tensorflow 2.0数据管道,我从将tf.data.Dataset管道线的MNIST数据加载到keras模型的基础开始。但是我遇到了错误:
ValueError: TypeError: len() of unsized object
我已经尝试按照以下代码对数据进行规范化和整形: https://www.tensorflow.org/tutorials。
我从链接中获取了输入管道示例: https://www.tensorflow.org/alpha/guide/keras/overview#train_from_tfdata_datasets
import tensorflow as tf
import numpy as np
import matplotlib
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train = x_train.astype(np.float64)
y_train = y_train.astype(np.float64)
assert len(x_train) == len(y_train), "Train data and Label Dosent match"
dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
dataset = dataset.repeat(1)
dataset = dataset.shuffle(int(len(x_train) * 0.5))
dataset = dataset.batch(100)
dataset = dataset.prefetch(2)
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
for x,y in dataset.take(1):
print("X shape is: {} \ny shape: {}".format(x.shape, y.shape))
model.fit(dataset, epochs=10, steps_per_epoch=30)
它应该开始训练模型,但出现错误。
-
--------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-63-84e016c0725f> in <module>
33 print("X shape is: {} \ny shape: {}".format(x.shape, y.shape))
34
---> 35 model.fit(dataset, epochs=10, steps_per_epoch=30)
~\AppData\Local\Continuum\miniconda3\envs\deeplearning\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
789 workers=0,
790 shuffle=shuffle,
--> 791 initial_epoch=initial_epoch)
792
793 # Case 3: Symbolic tensors or Numpy array-like.
~\AppData\Local\Continuum\miniconda3\envs\deeplearning\lib\site-packages\tensorflow\python\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1513 shuffle=shuffle,
1514 initial_epoch=initial_epoch,
-> 1515 steps_name='steps_per_epoch')
1516
1517 def evaluate_generator(self,
~\AppData\Local\Continuum\miniconda3\envs\deeplearning\lib\site-packages\tensorflow\python\keras\engine\training_generator.py in model_iteration(model, data, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch, mode, batch_size, steps_name, **kwargs)
196
197 # Setup work for each epoch.
--> 198 model.reset_metrics()
199 epoch_logs = {}
200 if mode == ModeKeys.TRAIN:
~\AppData\Local\Continuum\miniconda3\envs\deeplearning\lib\site-packages\tensorflow\python\keras\engine\training.py in reset_metrics(self)
1171 if hasattr(self, 'metrics'):
1172 for m in self.metrics:
-> 1173 m.reset_states()
1174
1175 # Reset the state of loss metric wrappers.
~\AppData\Local\Continuum\miniconda3\envs\deeplearning\lib\site-packages\tensorflow\python\keras\metrics.py in reset_states(self)
197 when a metric is evaluated during training.
198 """
--> 199 K.batch_set_value([(v, 0) for v in self.variables])
200
201 @abc.abstractmethod
~\AppData\Local\Continuum\miniconda3\envs\deeplearning\lib\site-packages\tensorflow\python\keras\backend.py in batch_set_value(tuples)
2878 if ops.executing_eagerly_outside_functions():
2879 for x, value in tuples:
-> 2880 x.assign(np.asarray(value, dtype=dtype(x)))
2881 else:
2882 with get_graph().as_default():
~\AppData\Local\Continuum\miniconda3\envs\deeplearning\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py in assign(self, value, use_locking, name, read_value)
1051 # initialize the variable.
1052 with _handle_graph(self.handle):
-> 1053 value_tensor = ops.convert_to_tensor(value, dtype=self.dtype)
1054 self._shape.assert_is_compatible_with(value_tensor.shape)
1055 assign_op = gen_resource_variable_ops.assign_variable_op(
~\AppData\Local\Continuum\miniconda3\envs\deeplearning\lib\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor(value, dtype, name, preferred_dtype, dtype_hint)
1048 preferred_dtype = deprecation.deprecated_argument_lookup(
1049 "dtype_hint", dtype_hint, "preferred_dtype", preferred_dtype)
-> 1050 return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
1051
1052
~\AppData\Local\Continuum\miniconda3\envs\deeplearning\lib\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor_v2(value, dtype, dtype_hint, name)
1106 name=name,
1107 preferred_dtype=dtype_hint,
-> 1108 as_ref=False)
1109
1110
~\AppData\Local\Continuum\miniconda3\envs\deeplearning\lib\site-packages\tensorflow\python\framework\ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx, accept_symbolic_tensors)
1184
1185 if ret is None:
-> 1186 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1187
1188 if ret is NotImplemented:
~\AppData\Local\Continuum\miniconda3\envs\deeplearning\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
302 as_ref=False):
303 _ = as_ref
--> 304 return constant(v, dtype=dtype, name=name)
305
306
~\AppData\Local\Continuum\miniconda3\envs\deeplearning\lib\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name)
243 """
244 return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 245 allow_broadcast=True)
246
247
~\AppData\Local\Continuum\miniconda3\envs\deeplearning\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
251 ctx = context.context()
252 if ctx.executing_eagerly():
--> 253 t = convert_to_eager_tensor(value, ctx, dtype)
254 if shape is None:
255 return t
~\AppData\Local\Continuum\miniconda3\envs\deeplearning\lib\site-packages\tensorflow\python\framework\constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
112 return t
113 else:
--> 114 return ops.EagerTensor(value, handle, device, dtype)
115
116
ValueError: TypeError: len() of unsized object