首页 文章

将Keras模型加载到TensorFlow时的输入和输出张量的名称

提问于
浏览
2

我试图在“纯粹的”TensorFlow中使用Keras的模型(我想在Android应用程序中使用它) . 我已成功将Keras模型导出到protobuf并将其导入Tensorflow . 然而,运行张量流模型需要提供输入和输出张量的名称,我不知道如何找到它们 . 我的模型看起来像这样:

seq = Sequential()
seq.add(Convolution2D(32, 3, 3, input_shape=(3, 15, 15), name="Conv1"))
....
seq.add(Activation('softmax', name="Act4"))
seq.compile()

当我在TensorFlow中打印张量时,我会发现:

Tensor("Conv1_W/initial_value:0", shape=(32, 3, 3, 3), dtype=float32)
Tensor("Conv1_W:0", dtype=float32_ref)
Tensor("Conv1_W/Assign:0", shape=(32, 3, 3, 3), dtype=float32_ref)
Tensor("Conv1_W/read:0", dtype=float32)

Tensor("Act4_sample_weights:0", dtype=float32)
Tensor("Act4_target:0", dtype=float32)

Hovewer,没有形状的张量(3,15,15) .

我见过here,我可以添加"my_input_tensor"作为输入,但是我没有尝试过TensorFlow 's and Keras'占位符,他们给了我这个错误:

/XXXXXXXXX/lib/python2.7/site-packages/keras/engine/topology.pyc in __init__(self, input, output, name)
   1599             # check that x is an input tensor
   1600             layer, node_index, tensor_index = x._keras_history
-> 1601             if len(layer.inbound_nodes) > 1 or (layer.inbound_nodes and layer.inbound_nodes[0].inbound_layers):
   1602                 cls_name = self.__class__.__name__
   1603                 warnings.warn(cls_name + ' inputs must come from '

AttributeError: 'NoneType' object has no attribute 'inbound_nodes'

1 回答

  • 1

    在Keras中调用 model.summary() 以查看所有图层 .

    输入张量通常称为 input_1input_2 等 . 在摘要中查看正确的名称 .


    当您在Keras中使用 input_shape=(3,15,15) 时,您实际上使用的形状为 (None, 3, 15, 15) 的张量 . 其中无将被培训或预测中的批量大小替换 .

    通常,对于这些未知的维度,您使用 -1 ,例如 (-1, 3, 15, 15) . 但我无法向你保证它会像这样工作 . 它非常适合重塑张量,但是为了创造我从未测试过 .

相关问题