简体   繁体   English

密集输出列表

[英]list of dense as outputs

I'm making a model whose output I want to be of dims (A,B). 我正在制作一个模型,其输出要为暗淡(A,B)。 So I'm making a list of denses (A elements, B outputs each) and I wanted my output to be (No_samples, A,B). 因此,我正在创建一个密集列表(A元素,每个B输出),并且我希望输出为(No_samples,A,B)。 It is a list of A elements with (No_samples,B). 它是带有(No_samples,B)的A元素的列表。 The method of having one dense with output AxB does not help because for every row I want to softmax accross only that 使用输出AxB密集的方法无济于事,因为对于每一行,我只想在整个行上使用softmax

I've attempted to use tf.concatenate, tf.reshape, but there is always either an error or the same undesirable output. 我尝试使用tf.concatenate,tf.reshape,但始终会出现错误或相同的不良输出。 My difficulty is that in order to proceed I have to do some really weird reshaping and I wish to avoid that by 我的困难在于,为了继续进行,我必须进行一些非常奇怪的重塑,我希望避免这样

for i in range(0, A):
    outputs.append(Dense(B, activation="softmax")(out))

And I've tried everything below (separately): 而且我已经尝试了以下所有内容(单独进行):

outputs = tf.stack(outputs)
outputs = Reshape(self.output_shape)(outputs)

outputs = tf.convert_to_tensor(outputs)

The expected outcome is that the output has shape of (A,?,B) instead of (?, A, B). 预期的结果是输出的形状为(A,?,B),而不是(?,A,B)。 Is there another method that I can have multiple denses in parralel with the above behaviour? 是否有另一种方法可以使上述行为与parralel中的多个密集对象并存?

Simple example with A=3, B=1. 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM