python - “NoneType”对象没有属性“形状”Tensorflow
问题描述
我正在尝试训练一个模型,但每次它都会抛出这个错误。该错误通常发生在在 Google Colab 上进行训练时,但在我的本地 PC 上进行训练时,它运行正常。
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-25-3435b262f1ae> in <module>()
----> 1 trainer.train()
8 frames
/content/lstm_chem/trainer.py in train(self)
43 # use_multiprocessing=True,
44 shuffle=True,
---> 45 callbacks=self.callbacks)
46
47 last_weight_file = glob(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
64 def _method_wrapper(self, *args, **kwargs):
65 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
---> 66 return method(self, *args, **kwargs)
67
68 # Running inside `run_distribute_coordinator` already.
/usr/local/lib/python3.6/dist-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_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
813 workers=workers,
814 use_multiprocessing=use_multiprocessing,
--> 815 model=self)
816
817 # Container that configures and calls `tf.keras.Callback`s.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model)
1110 use_multiprocessing=use_multiprocessing,
1111 distribution_strategy=ds_context.get_strategy(),
-> 1112 model=model)
1113
1114 strategy = ds_context.get_strategy()
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, shuffle, workers, use_multiprocessing, max_queue_size, model, **kwargs)
906 max_queue_size=max_queue_size,
907 model=model,
--> 908 **kwargs)
909
910 @staticmethod
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, workers, use_multiprocessing, max_queue_size, model, **kwargs)
790 return tensor_shape.TensorShape([None for _ in shape.as_list()])
791
--> 792 output_shapes = nest.map_structure(_get_dynamic_shape, peek)
793 output_types = nest.map_structure(lambda t: t.dtype, peek)
794
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in map_structure(func, *structure, **kwargs)
615
616 return pack_sequence_as(
--> 617 structure[0], [func(*x) for x in entries],
618 expand_composites=expand_composites)
619
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in <listcomp>(.0)
615
616 return pack_sequence_as(
--> 617 structure[0], [func(*x) for x in entries],
618 expand_composites=expand_composites)
619
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in _get_dynamic_shape(t)
784
785 def _get_dynamic_shape(t):
--> 786 shape = t.shape
787 # Unknown number of dimensions, `as_list` cannot be called.
788 if shape.rank is None:
AttributeError: 'NoneType' object has no attribute 'shape'
这是代码,任何帮助将不胜感激
from glob import glob
import os
from tensorflow.keras.callbacks import ModelCheckpoint, TensorBoard
class LSTMChemTrainer(object):
def __init__(self, modeler, train_data_loader, valid_data_loader):
self.model = modeler.model
self.config = modeler.config
self.train_data_loader = train_data_loader
self.valid_data_loader = valid_data_loader
self.callbacks = []
self.init_callbacks()
def init_callbacks(self):
self.callbacks.append(
ModelCheckpoint(
filepath=os.path.join(
self.config.checkpoint_dir,
'%s-{epoch:02d}-{val_loss:.2f}.hdf5' %
self.config.exp_name),
monitor=self.config.checkpoint_monitor,
mode=self.config.checkpoint_mode,
save_best_only=self.config.checkpoint_save_best_only,
save_weights_only=self.config.checkpoint_save_weights_only,
verbose=self.config.checkpoint_verbose,
))
self.callbacks.append(
TensorBoard(
log_dir=self.config.tensorboard_log_dir,
write_graph=self.config.tensorboard_write_graph,
))
def train(self):
history = self.model.fit(
self.train_data_loader,
steps_per_epoch=self.train_data_loader.__len__(),
epochs=self.config.num_epochs,
verbose=self.config.verbose_training,
validation_data=self.valid_data_loader,
validation_steps=self.valid_data_loader.__len__(),
shuffle=True,
callbacks=self.callbacks)
last_weight_file = glob(
os.path.join(
f'{self.config.checkpoint_dir}',
f'{self.config.exp_name}-{self.config.num_epochs:02}*.hdf5')
)[0]
assert os.path.exists(last_weight_file)
self.config.model_weight_filename = last_weight_file
with open(os.path.join(self.config.exp_dir, 'config.json'), 'w') as f:
f.write(self.config.toJSON(indent=2))
解决方案
推荐阅读
- java - 如何使用 lambda 在构造函数参数中实现> 1个接口方法?
- android - 将视频缩放为圆形 - Android
- javascript - Mongoose 事务 - MongoError: 承诺中止
- xml - 有没有一种简单的方法可以通过 bash 命令验证 xsd 中的 schematron
- javascript - Tabulator.js 的 id 可访问性不起作用
- html - 无法在 Firefox 中测量 Html-Image 的 Clientwidth
- python - NLP:为什么在 sklearn 管道中使用两个向量器(词袋/TFIDF)?
- c++ - 哪个 DLL 有 PathCchAppend?
- javascript - 设置间隔仅在第一次工作。有什么解决办法吗?
- javascript - Html.BeginForm 处理多个按钮和 Javascript 验证