我正在尝试在 tensorflow 1.10 中使用Keras的Model.fit_generator
。
简化的可复制代码:
import tensorflow as tf
import numpy as np
class TestNet(tf.keras.Model):
def __init__(self, class_count, name='TestNet', **kwargs):
super(TestNet, self).__init__(name=name, **kwargs)
self.convolution = tf.keras.layers.Conv1D(class_count, kernel_size=1, input_shape=(None, 3))
def call(self, points):
return self.convolution(points)
def segmentation_loss(labels, logits):
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=labels, logits=logits)
return tf.reduce_mean(cross_entropy)
def generate():
while True:
yield (np.zeros(shape=(100,3)), np.zeros(shape=(100)))
if __name__ == "__main__":
test_net = TestNet(class_count=5)
optimizer = tf.keras.optimizers.Adam()
test_net.compile(optimizer, loss=segmentation_loss)
history = test_net.fit_generator(generate, steps_per_epoch=1000, epochs=10)
虽然这在tensorflow 1.14中有效,但在1.10中执行将在标题中产生NotImplementedError
:
NotImplementedError:尚未为未构建的Model子类启用
fit_generator
有人知道如何解决这个问题吗?
答案 0 :(得分:0)
我在fit_generator
中看到一个错误,请对此进行更新:
history = test_net.fit_generator(generate(), steps_per_epoch=1000, epochs=10)
现在,我不知道Tensorflow 1.10是如何工作的,但是这种建模是相当新的,通常是这样构建Keras模型的:
#model's inputs
inputs = Input((None,3))
#model's layers
convolution = Conv1D(class_count, kernel_size=1)
#model's call
outputs = convolution(inputs)
#model finish
test_net = Model(inputs, outputs)
为您解决的方法:
inputs = Input(shape)
test_net = TestNet(...)
outputs = test_net.call(inputs)
test_net_model = Model(inputs, outputs)