Keras模型具有两个输入,不同的形状,一个未填充的

时间:2019-03-10 19:31:56

标签: keras

以下是错误的但可以运行:

import tensorflow as tf
import tensorflow.keras.activations as ka
import tensorflow.keras.backend as K
import tensorflow.keras.layers as kl
import tensorflow.keras.models as km
import numpy as np

class Model(km.Model):
    def __init__(self, dim):
        X_input = kl.Input(shape=(None, dim))
        Z_input = kl.Input(shape=(None, None, 1))
        super().__init__()
        self.dim = dim
    def call(self, inputs):
        X, Z = inputs
        X_layer = kl.Dense(16, activation='linear')(X)
        Z_dense = kl.Dense(16, activation='linear')
        combined = list()
        for i in range(Z.shape[1]):
            z = Z_dense(Z[:,i])
            l = X_layer + z
            l = K.expand_dims(l, axis=1)
            combined.append(l)
        combined = kl.concatenate(combined, axis=1)
        # combined is now shape (batch_size, z_size, 16)
        l = ka.relu(combined)
        l = kl.Dense(16, activation='relu')(l)
        l = kl.Dense(16, activation='relu')(l)
        l = kl.Dense(16, activation='linear')(l)
        return l

dim = 6
m = 100
X = np.random.randn(m, dim)
Z = np.random.randn(m, 10, 1) # THIS IS WRONG WANT (1, 10, 1) or something

model = Model(dim)

out = model.predict([X, Z])

我希望Z变量为1d,而不是常规批处理大小。这只是评估事物的一些输入。正在获得有关暗处的神秘错误,并尝试了许多不同的形状猜测。

0 个答案:

没有答案