【发布时间】:2017-08-02 11:33:23
【问题描述】:
我有一个由 9 个2000 维向量组成的序列,作为来自 2 个双向 lstms 的 o/p。我正在合并它们以获得九个4000 dim 向量。
我需要获取这 4000 维向量中的每一个,并将它们中的每一个输入共享的全连接层。 我怎样才能做到这一点? 现在我正在重塑合并 o/p 以馈入共享的全连接层。但是不知道有没有这个必要?
当我尝试对整个网络进行建模以采用多个 i/p 并产生多个 o/p 时,如link 中所述,我遇到了这个错误
代码可以在here找到。
# we can then concatenate the two vectors:
N=3
merge_cv = merge([top_out, btm_out], mode='concat')#concat_axis=2 or -1 (last dim axis)
cv = Reshape((9,1, 4000))(merge_cv) # we want 9 vectors of dimension 4000 each for sharedfc_out below
#number of output classes per cell
n_classes = 80
sharedfc_out= Dense(output_dim=n_classes,input_dim=4000,activation='relu')
#partial counts
#pc = np.ndarray(shape=(1,n_classes), dtype=float)
#cells_pc = np.array([[pc for j in range(N)] for i in range(N)])
outpc=[]
for i in range(N):
for j in range(N):
# cells_pc[i][j] = sharedfc_out(cv[N*i+j])
outpc.append(sharedfc_out(cv[0][N*i+j]))
# out=merge(outpc,mode='concat')
# out2=Reshape(720)(out)
model = Model(input=cells_in, output=outpc)
bi=lstm o/p 的维度
>>> merge_cv.shape
TensorShape([Dimension(1), Dimension(None), Dimension(4000)])
>>> cv.shape
TensorShape([Dimension(None), Dimension(9), Dimension(1), Dimension(4000)])
对于最后一行,我遇到了类型错误。
TypeError Traceback (most recent call last)
in ()
----> 1 model = Model(input=cells_in, output=outpc)
/home/jkl/anaconda3/lib/python3.5/site-packages/keras/engine/topology.py in __init__(self, input, output, name)
1814 cls_name = self.__class__.__name__
1815 raise TypeError('Output tensors to a ' + cls_name + ' must be '
-> 1816 'Keras tensors. Found: ' + str(x))
1817 # Build self.output_layers:
1818 for x in self.outputs:
TypeError: Output tensors to a Model must be Keras tensors. Found: Tensor("Relu_9:0", shape=(1, 80), dtype=float32)
【问题讨论】:
-
试试
outpc.append(sharedfc_out(cv.get_output()[:,:,0,N*i+j])) -
仍然遇到同样的错误 - TypeError: Output tensors to a Model must be Keras tensors。找到:Tensor("Relu_27:0", shape=(?, 80), dtype=float32)
但错误中的形状更改为 shape=(?, 80) 而不是之前的 shape=(1, 80) -
试试
aux_input = Lambda(lambda x: x[:,:,0, N * i + j], output_shape = (4000,))然后outpc.append(aux_input)` -
试过
aux_input = Lambda(lambda x: x[:,:,0, N * i + j], output_shape = (4000,)) outpc.append(sharedfc_out(aux_input))它给出了这个错误AttributeError: 'Lambda' object has no attribute 'get_shape' -
你认为问题出在我对 merge_cv
cv = Reshape((9,1, 4000))(merge_cv)的重塑吗?我也尝试过重塑 outpc(以获得单个 o/p 向量 - 请参阅上面的最后 2 条注释行),但也没有用。
标签: python machine-learning neural-network keras keras-layer