调用Keras.backend.clear_session()

时间:2019-08-06 10:00:49

标签: tensorflow keras

我正在尝试在keras循环中的每次迭代中使用新数据样本训练模型(使用tensorflow后端)。由于一些迭代后GPU内存错误,我附加了K.clear_session()。但是,经过一轮迭代后,代码将引发错误:

'Cannot interpret feed_dict key as Tensor: ' + e.args[0])
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", shape=(7, 7, 3, 64), dtype=float32) is not an element of this graph.

如果我最后删除了K.clear_session(),则没有错误。有谁能解释为什么这个错误会在第二次迭代中出现?

我尝试了其他方法(用于gpu发布),但没有一个起作用,这是我的最后选择。但这会引发错误。我粘贴了一个示例代码,它会产生错误。请注意,这不是实际的代码,我只是举了一个例子来重现我在实际代码中遇到的错误。

from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
import tensorflow as tf
import random
seed_value= 0
import os
import keras
os.environ['PYTHONHASHSEED']=str(seed_value)
random.seed(0)
np.random.seed(0)
from keras import backend as K
from keras.datasets import cifar10

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
for i in range(3):
    base_model = tf.keras.applications.resnet50.ResNet50(weights='imagenet', input_shape=(32, 32, 3),
                                               include_top=False)
    x = base_model.output
    x = tf.keras.layers.GlobalAveragePooling2D()(x)
    output = tf.keras.layers.Dense(10, activation='softmax',
                               kernel_initializer=tf.keras.initializers.RandomNormal(seed=4))(x)
    model = tf.keras.Model(inputs=base_model.input, outputs=output)
    y_train = keras.utils.to_categorical(y_train, 10)
    y_test = keras.utils.to_categorical(y_test, 10)

    for layer in base_model.layers:
        layer.trainable = False
    optimizer = tf.train.AdamOptimizer(learning_rate=0.0001)
    model.compile(optimizer=optimizer, loss='categorical_crossentropy',
              metrics=['accuracy'])
    model.fit(x_train,y_train,batch_size=1024,epochs=1,verbose=1)
    K.clear_session()
Traceback (most recent call last):
  File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1092, in _run
    subfeed, allow_tensor=True, allow_operation=False)
  File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3490, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3569, in _as_graph_element_locked
    raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("Placeholder:0", shape=(7, 7, 3, 64), dtype=float32) is not an element of this graph.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:/codes/experiments-AL/breakhis/40X-M-B/codes-AL/error_debug.py", line 22, in <module>
    include_top=False)
  File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\applications\__init__.py", line 70, in wrapper
    return base_fun(*args, **kwargs)
  File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\applications\resnet50.py", line 32, in ResNet50
    return resnet50.ResNet50(*args, **kwargs)
  File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\keras_applications\resnet50.py", line 291, in ResNet50
    model.load_weights(weights_path)
  File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\network.py", line 1544, in load_weights
    saving.load_weights_from_hdf5_group(f, self.layers)
  File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\saving.py", line 806, in load_weights_from_hdf5_group
    K.batch_set_value(weight_value_tuples)
  File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\backend.py", line 2784, in batch_set_value
    get_session().run(assign_ops, feed_dict=feed_dict)
  File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 929, in run
    run_metadata_ptr)
  File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1095, in _run
    'Cannot interpret feed_dict key as Tensor: ' + e.args[0])
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", shape=(7, 7, 3, 64), dtype=float32) is not an element of this graph.

Process finished with exit code 1

1 个答案:

答案 0 :(得分:0)

我能够通过将imagenet预先训练的模型保存到磁盘中,然后在调用tf.keras.backend.clear_session()之后每次循环加载来解决此问题。因此,将基本模型保存到文件,然后加载即可。但是我仍然感到困惑,为什么它在

之前不起作用
  

base_model = tf.keras.applications.resnet50.ResNet50