Keras(Tensorflow)如何计算。最后一层Tensor的形状?

时间:2018-05-03 18:26:57

标签: python tensorflow neural-network keras

我目前正致力于生成Conv NN的图像和生成Recurrent NN的音频。我为这两个生成器构建但由于某种原因, build_audio_generator 模型在其最后一层中有一个Tensor( Tensor(&#34; model_4 / sequential_4 / activation_4 / Tanh:0&#34; < / em>)形状(?,1)而不是(?,28,28,1)。我的问题是,如何更改代码 build_audio_generator ,以便它与 build_generator 具有相同的形状(?,28,28,1 )?

代码:

def build_generator(latent_dim, channels, num_classes):

    model = Sequential()

    model.add(Dense(128 * 7 * 7, activation="relu", input_dim=latent_dim))
    model.add(Reshape((7, 7, 128)))
    model.add(BatchNormalization(momentum=0.8))
    model.add(UpSampling2D())
    model.add(Conv2D(128, kernel_size=3, padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(momentum=0.8))
    model.add(UpSampling2D())
    model.add(Conv2D(64, kernel_size=3, padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(momentum=0.8))
    model.add(Conv2D(channels, kernel_size=3, padding='same'))
    model.add(Activation("tanh"))

    model.summary()

    noise = Input(shape=(latent_dim,))
    label = Input(shape=(1,), dtype='int32')


    label_embedding = Flatten()(Embedding(num_classes, 100)(label))

    model_input = multiply([noise, label_embedding])

    img = model(model_input)

    return Model([noise, label], img)

def build_audio_generator(latent_dim, num_classes):

    model = Sequential()
    model.add(LSTM(512, input_dim=latent_dim, return_sequences=True))
    model.add(Dropout(0.3))
    model.add(LSTM(512, return_sequences=True))
    model.add(Dropout(0.3))
    model.add(LSTM(512))
    model.add(Dense(256))
    model.add(Dropout(0.3))
    model.add(Dense(num_classes))
    model.add(Activation('tanh'))

    model.summary()

    noise = Input(shape=(None, latent_dim,))
    label = Input(shape=(1,), dtype='int32')
    label_embedding = Flatten()(Embedding(num_classes, 100)(label))
    model_input = multiply([noise, label_embedding])

    sound = model(model_input)

    return Model([noise, label], sound)

# Build the generator
generator = build_generator(100, 3, 1)
audio_generator = build_audio_generator(100, 1)

# The generator takes noise and the target label as input
# and generates the corresponding digit of that label
noise = Input(shape=(None, 100,))
label = Input(shape=(1,))

img = generator([noise, label])

audio = audio_generator([noise, label])

print('Audio: '+ str(audio))
print('Audio shape: ' + str(audio.shape))

print('IMG: '+str(img))
print('IMG shape: ' + str(img.shape))

控制台输出:

Audio: Tensor("model_4/sequential_4/activation_4/Tanh:0", shape=(?, 1), dtype=float32)
Audio shape: (?, 1)
IMG: Tensor("model_3/sequential_3/activation_3/Tanh:0", shape=(?, 28, 28, 1), dtype=float32)
IMG shape: (?, 28, 28, 1)

1 个答案:

答案 0 :(得分:0)

我认为你会想要3D音频而不是吗?

只需在所有LSTM中保留return_sequences=True