【发布时间】:2018-11-29 04:25:21
【问题描述】:
我在使用 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。
【问题讨论】:
标签: python tensorflow keras