【发布时间】:2018-11-22 12:14:51
【问题描述】:
我正在尝试使用model.predict 的输出作为另一个模型的输入。
这实际上是出于调试目的,以及为什么我不使用get_layer.output 或使用统一这两个模型的全局模型。
我遇到了这个错误:
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("input_1:0", shape=(?, 10, 10, 2048), dtype=float32) is not an element of this graph.
这是我当前的代码:
我正在使用下面的函数作为生成器的瓶颈
def test_model_predictions(params):
batch_size = params['batch_size'] #params just contains model parameters
base_model = create_model() #this just creates an instance of Inception model with final layers cutoff
train,_ = create_generators(params) #creats a generator for image files
while True:
for x, y in train:
predict1 = base_model.predict(x)
yield (predict1, y)
def training_bottleneck():
bottleneck_train = test_model_predictions(params) #generator
def bottleneck_model_1():
bottleneck_input = Input(shape = (10, 10, 2048))
x = GlobalAveragePooling2D()(bottleneck_input)
x = Dense(1024, activation='relu')(x)
predictions = Dense(params['classes'], activation='softmax')(x)
model = Model(inputs=bottleneck_input, outputs=predictions)
return model
model2 = bottleneck_model_1()
model2.compile(optimizer= optimizers.SGD(lr=0.0001, momentum=0.99),
loss='categorical_crossentropy', metrics = ['accuracy'])
print('Starting model training')
history = model2.fit_generator(bottleneck_train,
steps_per_epoch = 1000,
epochs = 85,
shuffle= False,
verbose = 2, )
return history
关于如何进行这项工作的任何线索?
谢谢。
编辑:对于我为什么这样做似乎有些困惑,所以我将添加更多信息。
我专门使用predict,因为在将model.predict值(瓶颈值)保存到hdf5文件,然后将这些值加载到另一个模型(原始模型的后半部分)时,我注意到了一个差异,
与仅加载整个模型并仅冻结上半部分(无法训练前半部分)相比。尽管使用相同的超参数,但我注意到的差异,
本质上是相同的模型是完整的模型训练正确并收敛,而加载瓶颈值的模型并没有真正改善。
因此,我试图看到我融合model.predict 以保存瓶颈值是两个模型之间差异的原因。
【问题讨论】:
-
基本上,这意味着该变量未在与您现在正在运行的图形范围相同的图形范围内定义。尝试在相同的图形范围内定义它们。
-
我对如何正确执行此操作感到有些困惑,同时仍专门使用 model.predict 作为第二个模型的输入(请参阅更新的 OP)。我们不能让两个单独的图表按顺序工作吗?第一个 Model.predict 输出一个 numpy 数组,应该是一个单独的图。由于输出是一个数组而不是张量,我认为它不应该与第二个模型有任何联系,它应该是一个单独的图。很困惑为什么一切都需要在一个图表中。有没有一种方法可以创建单个图形并仍然使用 model.predict 作为第二个模型的输入?
标签: python-3.x tensorflow neural-network keras deep-learning