Theano, TypeError: Unknown parameter type: <class 'tensorflow.python.framework.ops.Tensor'>

Theano, TypeError: Unknown parameter type: <class 'tensorflow.python.framework.ops.Tensor'>

当我运行以下代码(来自this IPython notebook)时,出现错误:

import theano

def get_submodel(model, start, end):
    return theano.function([model.layers[start].input],
                          model.layers[end].get_output(train=False),
                          allow_input_downcast=True)

def get_encoder(ae):
    return get_submodel(ae, 0, (len(ae.layers) // 2) - 1)

ae_encoder = get_encoder(ae)

这是错误信息:

Traceback (most recent call last):
  File "Parametric t-SNE (Keras).py", line 432, in <module>
    ae_encoder = get_encoder(ae)
  File "Parametric t-SNE (Keras).py", line 424, in get_encoder
    return get_submodel(ae, 0, (len(ae.layers) // 2) - 1)
  File "Parametric t-SNE (Keras).py", line 422, in get_submodel
    allow_input_downcast=True)
  File "/usr/lib64/python3.4/site-packages/theano/compile/function.py", line 326, in function
    output_keys=output_keys)
  File "/usr/lib64/python3.4/site-packages/theano/compile/pfunc.py", line 397, in pfunc
    for p in params]
  File "/usr/lib64/python3.4/site-packages/theano/compile/pfunc.py", line 397, in <listcomp>
    for p in params]
  File "/usr/lib64/python3.4/site-packages/theano/compile/pfunc.py", line 496, in _pfunc_param_to_in
    raise TypeError('Unknown parameter type: %s' % type(param))
TypeError: Unknown parameter type: <class 'tensorflow.python.framework.ops.Tensor'>

作为参考,此处定义了 ae

n = X_train.shape[1]
ae = Sequential()
ae.add(Dense(500, activation='relu', weights=encoder.layers[0].get_weights(), input_shape=(n,)))
ae.add(Dense(500, activation='relu', weights=encoder.layers[1].get_weights()))
ae.add(Dense(2000, activation='relu', weights=encoder.layers[2].get_weights()))
ae.add(Dense(2, weights=encoder.layers[3].get_weights()))
ae.add(Dense(2000, activation='relu', weights=decoder.layers[0].get_weights()))
ae.add(Dense(500, activation='relu', weights=decoder.layers[1].get_weights()))
ae.add(Dense(500, activation='relu', weights=decoder.layers[2].get_weights()))
ae.add(Dense(n, weights=decoder.layers[3].get_weights()))
ae.compile(loss='mse', optimizer='rmsprop')
ae.fit(X_train, X_train, nb_epoch=100, verbose=2, batch_size=32)

根据对 a similar question 的回答,我怀疑 get_submodel 可能需要修改为使用符号变量而不是 tensor/matrix。但是,我不确定如何执行此操作,以及为什么它甚至会给出错误,因为 GitHub 上的 IPython 笔记本似乎没有包含任何错误消息。我无法找到有关 tensorflow.python.framework.ops.Tensor 错误消息的更具体的建议。

您似乎有一个 tensorflow 后端 - 而不是 theano 后端。这就是为什么使用 theano.function 会产生错误。尝试使用 keras.backend.function.