我正在制作一个模型,我希望它的输出是dims (A,B)。所以我列出了密度(A个元素,B个输出),我希望我的输出是(No_samples,A,B)。它是带有(No_samples,B)的A元素的列表。使用带有输出AxB的dense的方法没有帮助,因为对于我想要softmax的每一行,只需要
我尝试使用tf.concatenate、tf.reshape,但总是出现错误或相同的不受欢迎的输出。我的困难是,为了继续下去,我必须做一些非常奇怪的重塑,我希望通过以下方式避免这种情况
for i in range(0, A):
outputs.append(Dense(B, activation="softmax")(out))
我已经尝试了下面的所有方法(分别):
outputs = tf.stack(outputs)
outputs = Reshape(self.output_shape)(outputs)
outputs = tf.convert_to_tensor(outputs)
预期的结果是输出的形状是(A,?,B)而不是(?,A,B)。有没有其他方法可以让我在parralel中拥有多个密度,并具有上述行为?
发布于 2019-08-04 13:41:00
简单的A=3例子,B=1。
from keras import backend as K
from keras.layers import Concatenate, Dense, Input, Lambda
from keras.models import Model
import numpy as np
def expand_dims(x):
return K.expand_dims(x, axis=-2) #expand (None, 1) to (None, 1, 1)
x = Input((2,))
A = 3
B = 1
y = Lambda(expand_dims)(Dense(B, activation="softmax")(x))
for i in range(0, A-1):
# Concatenate on the newly added dimension
y = Concatenate(axis=-2)([y,Lambda(expand_dims)(Dense(B, activation="softmax")(x))])
model = Model(x, y)
print(model.predict(np.ones((4,2))).shape)
(4, 3, 1) # Output shape is (No_samples, A,B)
https://stackoverflow.com/questions/57343116
复制相似问题