gpt4 book ai didi

python - Keras:使用 fit_generator 时出现 notImplementedError/RuntimeError

转载 作者:太空宇宙 更新时间:2023-11-04 04:25:47 24 4
gpt4 key购买 nike

我在使用 keras 和 tensorflow 时遇到问题,使用以下代码:

from tensorflow.keras.layers import Activation, Conv2D
from tensorflow.keras import Model

from data import DataGenerator
from config import train_datapath, test_datapath


training_generator = DataGenerator(train_datapath)
validation_generator = DataGenerator(test_datapath)

class model(Model):
def __init__(self):
super(model, self).__init__()
self.conv1 = Conv2D(filters=2, kernel_size=1, strides=1, padding='same', input_shape=(256, 256, 1))
self.act1 = Activation('relu')

def call(self, input):
"""Run the model."""
return self.act1(self.conv1(input))

model = model()
model.compile(optimizer='adam', loss='mean_squared_error')
history = model.fit_generator(training_generator, epochs=5000, verbose=1, validation_data=(validation_generator),
use_multiprocessing=False)

运行该代码会出现以下错误:

Using TensorFlow backend.
Traceback (most recent call last):
File "C:/Users/...py", line 23, in <module>
use_multiprocessing=False)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\training.py", line 2161, in fit_generator
'`fit_generator` is not yet enabled for unbuilt Model subclasses')
NotImplementedError: `fit_generator` is not yet enabled for unbuilt Model subclasses

我试图找到一些提示,如果我真的尝试做某事,那不是故意的,但对我来说这似乎不合逻辑,因为这种方式对于定义更复杂的网络来说非常舒服,而且我已经准备好了与 pytorch 一起工作,我很确定,这也应该与 tf 和 keras 一起工作。

有一个thread有同样的错误,但对于顺序网络实现,这显然不是我的目标。

如果我对 keras 使用直接导入,我会收到不同的错误:

Using TensorFlow backend.
Traceback (most recent call last):
File "C:/Users/...py", line 23, in <module>
use_multiprocessing=False)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1418, in fit_generator
initial_epoch=initial_epoch)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training_generator.py", line 40, in fit_generator
model._make_train_function()
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 496, in _make_train_function
raise RuntimeError('You must compile your model before using it.')
RuntimeError: You must compile your model before using it.

Process finished with exit code 1

但是我正在编译上面一行的模型,调用 model.fit_generator() 函数...当我没有完全错的时候,我也给出了正确的输入形状,所以这也不应该是问题......

使用 windows、python 3.6、tensorflow-gpu 1.12。

最佳答案

您不是在任何地方创建模型。

你需要:

model = Model(inputTensors, outputTensors)

或者至少在你的类中的某个时刻super(model,self).__init__(inputTensors,outputTensors)

理想情况下:

def createModel():
inputs = Input((256,256,1))
outputs = Conv2D(filters=2, kernel_size=1, strides=1,
padding='same', activation='relu')(inputs)
return Model(inputs,outputs)

如果出于某种原因你确实想要一个子类:

class MyModel(Model):
def init(self):
inputs = Input((256,256,1))
self.conv1 = Conv2D(filters=2, kernel_size=1, strides=1, padding='same')
self.act1 = Activation('relu')
outputs = self.act1(self.conv1(inputs))

super(MyModel,self).__init__(inputs,outputs)

关于python - Keras:使用 fit_generator 时出现 notImplementedError/RuntimeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53531820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com