英文:
tf.keras.models.load_model() error: TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
问题
以下是您提供的代码的中文翻译:
我正在尝试使用TensorFlow 2.9.2训练一个模型。我的模型定义如下:
import tensorflow as tf
encoder_layers = 1
encoder_bidirectional = False
def get_model():
model = tf.keras.Sequential(name='model')
model.add(tf.keras.layers.Dropout(0.5))
for _ in range(encoder_layers):
rnn = tf.keras.layers.LSTM(2**6, return_sequences=True)
if encoder_bidirectional:
rnn = tf.keras.layers.Bidirectional(rnn)
model.add(rnn)
model.add(tf.keras.layers.Dense(2, activation='softmax'))
return model
def build_model():
model = get_model()
model.build(input_shape=(None, None, 25))
model.compile(
loss='sparse_categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(0.001),
metrics=['accuracy']
)
model.summary()
return model
然后,我使用以下方式训练模型:
# 训练模型
train, dev, test = get_datasets()
model = build_model()
es = EarlyStopping(
monitor='val_accuracy',
mode='max',
verbose=1,
patience=10)
mc = ModelCheckpoint(
'model.h5',
monitor='val_accuracy',
mode='max',
verbose=1,
save_best_only=True)
with tf.device("/GPU:0"):
model.fit(
train,
epochs=500,
steps_per_epoch=32,
validation_data=dev,
callbacks=[es, mc])
best_model = load_model('model.h5')
best_model.evaluate(test)
在`best_model = load_model('model.h5')`处,我收到以下错误:
Traceback (most recent call last):
File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/experiments/train.py", line 76, in <module>
app.run(main)
File "/usr/local/lib/python3.8/dist-packages/absl/app.py", line 308, in run
_run_main(main, args)
File "/usr/local/lib/python3.8/dist-packages/absl/app.py", line 254, in _run_main
sys.exit(main(argv))
File "/experiments/train.py", line 70, in main
best_model = load_model(FLAGS.model_path)
File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.8/dist-packages/keras/initializers/initializers_v2.py", line 1056, in _compute_fans
return int(fan_in), int(fan_out)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
在找到此帖子后,我检查了我的`model.h5`文件,实际上它具有`batch_input_shape=[null,null,null]`。但是如何防止我的检查点模型保存具有输入形状的null值?是否有任何方法可以解决这个问题?
EDIT:
我刚刚在这个Colab中使用我的一些数据示例复制了这个错误:https://colab.research.google.com/drive/1z63TN-P_WKtTWTZs2IhGBU0NjD7TE6m_#scrollTo=f1oD4G6QEq4k。
希望这个翻译对您有帮助。如果您有任何其他问题,请随时提出。
英文:
I'm trying to train a model using tensorflow 2.9.2. My model is defined as
import tensorflow as tf
encoder_layers = 1
encoder_bidirectional = False
def get_model():
model = tf.keras.Sequential(name='model')
model.add(tf.keras.layers.Dropout(0.5))
for _ in range(encoder_layers):
rnn = tf.keras.layers.LSTM(2**6, return_sequences=True)
if encoder_bidirectional:
rnn = tf.keras.layers.Bidirectional(rnn)
model.add(rnn)
model.add(tf.keras.layers.Dense(2, activation='softmax'))
return model
def build_model():
model = get_model()
model.build(input_shape=(None, None, 25))
model.compile(
loss='sparse_categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(0.001),
metrics=['accuracy']
)
model.summary()
return model
Then I train the model using
# train model
train, dev, test = get_datasets()
model = build_model()
es = EarlyStopping(
monitor='val_accuracy',
mode='max',
verbose=1,
patience=10)
mc = ModelCheckpoint(
'model.h5',
monitor='val_accuracy',
mode='max',
verbose=1,
save_best_only=True)
with tf.device("/GPU:0"):
model.fit(
train,
epochs=500,
steps_per_epoch=32,
validation_data=dev,
callbacks=[es, mc])
best_model = load_model('model.h5')
best_model.evaluate(test)
At best_model = load_model('model.h5')
I get the following error
Traceback (most recent call last):
File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/experiments/train.py", line 76, in <module>
app.run(main)
File "/usr/local/lib/python3.8/dist-packages/absl/app.py", line 308, in run
_run_main(main, args)
File "/usr/local/lib/python3.8/dist-packages/absl/app.py", line 254, in _run_main
sys.exit(main(argv))
File "/experiments/train.py", line 70, in main
best_model = load_model(FLAGS.model_path)
File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.8/dist-packages/keras/initializers/initializers_v2.py", line 1056, in _compute_fans
return int(fan_in), int(fan_out)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
After finding this post I checked my model.h5
file and in fact it has batch_input_shape=[null,null,null]
. But how do I prevent my checkpoint model to be saved with null values for the input shape? Is there any way I can solve this?
EDIT:
I just repoduced the error with a sample of my data in this colab: https://colab.research.google.com/drive/1z63TN-P_WKtTWTZs2IhGBU0NjD7TE6m_#scrollTo=f1oD4G6QEq4k.
答案1
得分: 1
Sure, here's the translated code:
在您的模型代码中,在开头添加以下层次。
```python
def get_model():
model = tf.keras.Sequential(name='model')
model.add(tf.keras.layers.InputLayer(input_shape=(None, 25)))
...
best_model = keras.models.load_model('/content/model.h5') # OK
best_model.evaluate(test)
请注意,代码部分没有进行翻译。
<details>
<summary>英文:</summary>
In your model code, add the following layers at the beginning.
```python
def get_model():
model = tf.keras.Sequential(name='model')
model.add(tf.keras.layers.InputLayer(input_shape=(None, 25)))
...
best_model = keras.models.load_model('/content/model.h5') # OK
best_model.evaluate(test)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论