张量流运行时重塑问题

时间:2020-08-09 17:47:06

标签: tensorflow deep-linking

我正在尝试创建一个时空图卷积网络,其中gcn层夹在两个断层cnn层之间。代码如下:

inputs = Input(shape=(train_x.shape[1],train_x.shape[2],train_x.shape[3]), batch_size=None)
# temporal convolution
y = tf.keras.layers.Conv1D(128, 9, activation='relu')(inputs)
#graph convolution
y = tf.keras.layers.Conv2D(32, (1,1), activation='relu')(y) 
n, v, t, kc = y.shape
y = tf.reshape(y,(n, 1, kc//1, t, v))
y = tf.einsum('nkctv,kvw->nwtc', y, AD_tensor)
#temporal convolution
y = tf.keras.layers.Conv1D(16, 9, activation='relu')(y)

concat = Flatten()(y)

fc = Dense(units=80, activation='relu')(concat)
fc1 = Dense(units=40, activation='relu')(fc)
fc2 = Dense(units=40, activation='relu')(fc1)
fc3 = Dense(units=80, activation='relu')(fc2)
out = Dense(1, activation = 'sigmoid')(fc3)

model = Model(inputs, out)
model.compile(loss='mse', optimizer= Adam(lr=0.0001))
model.fit(train_x, train_y, validation_data = (valid_x,valid_y), epochs=300, batch_size=2)

当我运行此代码时,它显示出此类型错误:

    TypeError: Failed to convert object of type <class 'tuple'> to Tensor. 
Contents: (None, 1, 32, 72, 25). Consider casting elements to a supported type.

1 个答案:

答案 0 :(得分:1)

要将Tensorflow操作与Keras层一起使用,应将它们这样包装在Lambda层中。 Lambda层将函数作为参数。

y = tf.keras.layers.Lambda(lambda x: tf.reshape(n, v, t, kc))(y)

但是,为了进行重塑,Keras已经为此操作提供了一层,因此您可以这样做

y = tf.keras.layers.Reshape(shape=(v, t, kc))(y)

重塑的图层版本已经考虑了批次尺寸,因此您只需要指定其他尺寸即可。

对于einsum操作,您可以使用

y = tf.keras.layers.Lambda(lambda x: tf.einsum('nkctv,kvw->nwtc', x[0], x[1]))([y, AD_tensor])