Theano,TypeError:未知参数类型:<class' tensorflow.python.framework.ops.tensor'=“”>

时间:2017-11-05 17:27:03

标签: python tensorflow keras theano

当我运行以下代码(来自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可能需要修改为使用符号变量而不是张量/矩阵。但是,我不知道如何做到这一点,以及为什么它甚至会出错,因为GitHub上的IPython笔记本似乎没有包含任何错误消息。我无法找到有关tensorflow.python.framework.ops.Tensor错误消息的更具体建议。

1 个答案:

答案 0 :(得分:1)

您似乎有一个tensorflow后端 - 而不是theano后端。这就是使用theano.function生成错误的原因。尝试使用keras.backend.function