我已经制作了一个没有残余连接的模型,它编译和拟合没有任何错误[使用 Keras Sequential API ]
我希望测试一个修改版本,只需添加一个残留连接,如SPEECH ENHANCEMENT BASED ON DEEP NEURAL NETWORKS WITH SKIP CONNECTIONS。所以,我需要改用功能 API。
我的问题是在输入中间提取一块。我试过了。
INPUT_SIZE = N*OUTPUT_SIZE # N must be odd
HIDDEN_SIZE = N*OUTPUT_SIZE # N must be odd
modelInputs = Input(shape=(INPUT_SIZE,))
x = Dense(HIDDEN_SIZE, activation='relu', kernel_initializer=INPUT_KERNEL_INITIALIZER)(modelInputs)
for _ in np.arange(1,N_HIDDEN):
x = Dense(HIDDEN_SIZE, activation='relu', kernel_initializer=INPUT_KERNEL_INITIALIZER)(x)
Y = Dense(OUTPUT_SIZE, activation='relu', kernel_initializer=INPUT_KERNEL_INITIALIZER)(x)
# --------------------------------------------------------
# Here, 4 options I tried to get "modelInputs_selected"
# --------------------------------------------------------
# Try 1
modelInputs_selected = Lambda(lambda x: x[int(N/2)*OUTPUT_SIZE:(int(N/2)+1)*OUTPUT_SIZE])(modelInputs)
# Try 2 [Try 1 with 'output_shape' filled]
modelInputs_selected = Lambda(lambda x: x[int(N/2)*OUTPUT_SIZE:(int(N/2)+1)*OUTPUT_SIZE, :], output_shape=(OUTPUT_SIZE,))(modelInputs)
# Try 3
modelInputs_selected = K.transpose(K.gather(K.transpose(modelInputs), K.arange(int(N/2)*OUTPUT_SIZE, (int(N/2)+1)*OUTPUT_SIZE)))
# Try 4 [Try 3 unwrapped]
toto1 = K.transpose(modelInputs)
toto2 = K.gather(toto1, K.arange(int(N/2)*OUTPUT_SIZE, (int(N/2)+1)*OUTPUT_SIZE))
modelInputs_selected = K.transpose(toto2)
# --------------------------------------------------------
# End of option tried
# --------------------------------------------------------
predictions = add([modelInputs_selected, Y])
model = Model(inputs=modelInputs, outputs=predictions)
结果是:
- 尝试 1 和尝试 2:
- 错误 = add() 期间的形状不连贯
- 尝试 3 和尝试 4:
- add() 的好形状
- 模型错误(...)
- 我一步一步进入了 Model() 。我们从最后一层开始向后退
- 输出 add() 确定
- 上一个 K.transpose(): Error AttributeError: 'Tensor' object has no attribute '_keras_history' in "build_map_of_graph"
模型构建失败是因为我使用了后端的函数(TensorFlow,对我来说)?
任何人都可以帮忙,好吗?
也许如果我使用乘法()?
- modelInputs 是 (m, N*OUTPUT_SIZE) 和 modelInputs_selected 是 (m,OUTPUT_SIZE)
- 使用好的矩阵 A (N*OUTPUT_SIZE, OUTPUT_SIZE):modelInputs_selected = multiply(modelInputs, A)