tf.keras.models.load_model() error: TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

huangapple go评论97阅读模式
英文:

tf.keras.models.load_model() error: TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

问题

以下是您提供的代码的中文翻译:

  1. 我正在尝试使用TensorFlow 2.9.2训练一个模型我的模型定义如下
  2. import tensorflow as tf
  3. encoder_layers = 1
  4. encoder_bidirectional = False
  5. def get_model():
  6. model = tf.keras.Sequential(name='model')
  7. model.add(tf.keras.layers.Dropout(0.5))
  8. for _ in range(encoder_layers):
  9. rnn = tf.keras.layers.LSTM(2**6, return_sequences=True)
  10. if encoder_bidirectional:
  11. rnn = tf.keras.layers.Bidirectional(rnn)
  12. model.add(rnn)
  13. model.add(tf.keras.layers.Dense(2, activation='softmax'))
  14. return model
  15. def build_model():
  16. model = get_model()
  17. model.build(input_shape=(None, None, 25))
  18. model.compile(
  19. loss='sparse_categorical_crossentropy',
  20. optimizer=tf.keras.optimizers.Adam(0.001),
  21. metrics=['accuracy']
  22. )
  23. model.summary()
  24. return model
  25. 然后我使用以下方式训练模型
  26. # 训练模型
  27. train, dev, test = get_datasets()
  28. model = build_model()
  29. es = EarlyStopping(
  30. monitor='val_accuracy',
  31. mode='max',
  32. verbose=1,
  33. patience=10)
  34. mc = ModelCheckpoint(
  35. 'model.h5',
  36. monitor='val_accuracy',
  37. mode='max',
  38. verbose=1,
  39. save_best_only=True)
  40. with tf.device("/GPU:0"):
  41. model.fit(
  42. train,
  43. epochs=500,
  44. steps_per_epoch=32,
  45. validation_data=dev,
  46. callbacks=[es, mc])
  47. best_model = load_model('model.h5')
  48. best_model.evaluate(test)
  49. `best_model = load_model('model.h5')`我收到以下错误
  50. Traceback (most recent call last):
  51. File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
  52. return _run_code(code, main_globals, None,
  53. File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
  54. exec(code, run_globals)
  55. File "/experiments/train.py", line 76, in <module>
  56. app.run(main)
  57. File "/usr/local/lib/python3.8/dist-packages/absl/app.py", line 308, in run
  58. _run_main(main, args)
  59. File "/usr/local/lib/python3.8/dist-packages/absl/app.py", line 254, in _run_main
  60. sys.exit(main(argv))
  61. File "/experiments/train.py", line 70, in main
  62. best_model = load_model(FLAGS.model_path)
  63. File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
  64. raise e.with_traceback(filtered_tb) from None
  65. File "/usr/local/lib/python3.8/dist-packages/keras/initializers/initializers_v2.py", line 1056, in _compute_fans
  66. return int(fan_in), int(fan_out)
  67. TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
  68. 在找到此帖子后我检查了我的`model.h5`文件实际上它具有`batch_input_shape=[null,null,null]`但是如何防止我的检查点模型保存具有输入形状的null是否有任何方法可以解决这个问题
  69. EDIT:
  70. 我刚刚在这个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

  1. import tensorflow as tf
  2. encoder_layers = 1
  3. encoder_bidirectional = False
  4. def get_model():
  5. model = tf.keras.Sequential(name=&#39;model&#39;)
  6. model.add(tf.keras.layers.Dropout(0.5))
  7. for _ in range(encoder_layers):
  8. rnn = tf.keras.layers.LSTM(2**6, return_sequences=True)
  9. if encoder_bidirectional:
  10. rnn = tf.keras.layers.Bidirectional(rnn)
  11. model.add(rnn)
  12. model.add(tf.keras.layers.Dense(2, activation=&#39;softmax&#39;))
  13. return model
  14. def build_model():
  15. model = get_model()
  16. model.build(input_shape=(None, None, 25))
  17. model.compile(
  18. loss=&#39;sparse_categorical_crossentropy&#39;,
  19. optimizer=tf.keras.optimizers.Adam(0.001),
  20. metrics=[&#39;accuracy&#39;]
  21. )
  22. model.summary()
  23. return model

Then I train the model using

  1. # train model
  2. train, dev, test = get_datasets()
  3. model = build_model()
  4. es = EarlyStopping(
  5. monitor=&#39;val_accuracy&#39;,
  6. mode=&#39;max&#39;,
  7. verbose=1,
  8. patience=10)
  9. mc = ModelCheckpoint(
  10. &#39;model.h5&#39;,
  11. monitor=&#39;val_accuracy&#39;,
  12. mode=&#39;max&#39;,
  13. verbose=1,
  14. save_best_only=True)
  15. with tf.device(&quot;/GPU:0&quot;):
  16. model.fit(
  17. train,
  18. epochs=500,
  19. steps_per_epoch=32,
  20. validation_data=dev,
  21. callbacks=[es, mc])
  22. best_model = load_model(&#39;model.h5&#39;)
  23. best_model.evaluate(test)

At best_model = load_model(&#39;model.h5&#39;) I get the following error

  1. Traceback (most recent call last):
  2. File &quot;/usr/lib/python3.8/runpy.py&quot;, line 194, in _run_module_as_main
  3. return _run_code(code, main_globals, None,
  4. File &quot;/usr/lib/python3.8/runpy.py&quot;, line 87, in _run_code
  5. exec(code, run_globals)
  6. File &quot;/experiments/train.py&quot;, line 76, in &lt;module&gt;
  7. app.run(main)
  8. File &quot;/usr/local/lib/python3.8/dist-packages/absl/app.py&quot;, line 308, in run
  9. _run_main(main, args)
  10. File &quot;/usr/local/lib/python3.8/dist-packages/absl/app.py&quot;, line 254, in _run_main
  11. sys.exit(main(argv))
  12. File &quot;/experiments/train.py&quot;, line 70, in main
  13. best_model = load_model(FLAGS.model_path)
  14. File &quot;/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py&quot;, line 67, in error_handler
  15. raise e.with_traceback(filtered_tb) from None
  16. File &quot;/usr/local/lib/python3.8/dist-packages/keras/initializers/initializers_v2.py&quot;, line 1056, in _compute_fans
  17. return int(fan_in), int(fan_out)
  18. TypeError: int() argument must be a string, a bytes-like object or a number, not &#39;NoneType&#39;

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:

  1. 在您的模型代码中在开头添加以下层次
  2. ```python
  3. def get_model():
  4. model = tf.keras.Sequential(name='model')
  5. model.add(tf.keras.layers.InputLayer(input_shape=(None, 25)))
  6. ...
  7. best_model = keras.models.load_model('/content/model.h5') # OK
  8. best_model.evaluate(test)
  1. 请注意,代码部分没有进行翻译。
  2. <details>
  3. <summary>英文:</summary>
  4. In your model code, add the following layers at the beginning.
  5. ```python
  6. def get_model():
  7. model = tf.keras.Sequential(name=&#39;model&#39;)
  8. model.add(tf.keras.layers.InputLayer(input_shape=(None, 25)))
  9. ...
  10. best_model = keras.models.load_model(&#39;/content/model.h5&#39;) # OK
  11. best_model.evaluate(test)

huangapple
  • 本文由 发表于 2023年5月18日 06:22:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定