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

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

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=&#39;model&#39;)
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=&#39;softmax&#39;))
return model
def build_model():
model = get_model()
model.build(input_shape=(None, None, 25))
model.compile(
loss=&#39;sparse_categorical_crossentropy&#39;,
optimizer=tf.keras.optimizers.Adam(0.001),
metrics=[&#39;accuracy&#39;]
)
model.summary()
return model

Then I train the model using

# train model
train, dev, test = get_datasets()
model = build_model()
es = EarlyStopping(
monitor=&#39;val_accuracy&#39;,
mode=&#39;max&#39;,
verbose=1,
patience=10)
mc = ModelCheckpoint(
&#39;model.h5&#39;,
monitor=&#39;val_accuracy&#39;,
mode=&#39;max&#39;,
verbose=1,
save_best_only=True)
with tf.device(&quot;/GPU:0&quot;):
model.fit(
train,
epochs=500,
steps_per_epoch=32,
validation_data=dev,
callbacks=[es, mc])
best_model = load_model(&#39;model.h5&#39;)
best_model.evaluate(test)

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

Traceback (most recent call last):
File &quot;/usr/lib/python3.8/runpy.py&quot;, line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File &quot;/usr/lib/python3.8/runpy.py&quot;, line 87, in _run_code
exec(code, run_globals)
File &quot;/experiments/train.py&quot;, line 76, in &lt;module&gt;
app.run(main)
File &quot;/usr/local/lib/python3.8/dist-packages/absl/app.py&quot;, line 308, in run
_run_main(main, args)
File &quot;/usr/local/lib/python3.8/dist-packages/absl/app.py&quot;, line 254, in _run_main
sys.exit(main(argv))
File &quot;/experiments/train.py&quot;, line 70, in main
best_model = load_model(FLAGS.model_path)
File &quot;/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py&quot;, line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File &quot;/usr/local/lib/python3.8/dist-packages/keras/initializers/initializers_v2.py&quot;, 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 &#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:

在您的模型代码中在开头添加以下层次

```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=&#39;model&#39;)
model.add(tf.keras.layers.InputLayer(input_shape=(None, 25)))
...
best_model = keras.models.load_model(&#39;/content/model.h5&#39;) # OK
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:

确定