应在至少2个输入的列表上调用“连接"层 [英] A 'Concatenate' layer should be called on a list of at least 2 inputs
问题描述
我正在尝试在Keras中实现一个conv-net,在这里我计划将层分成用于不同参数激活功能的单元,然后使用连接层重新组合这些单元.但是,在测试层的分离/重组时,我在网络的第一层遇到了以上错误.
I am trying to implement a conv-net in Keras, where I am planning to separate layers into units for different parametric activation functions, and then recombine these units using the concatenate layer. However, I run into the above error at the first layer in the network during testing of separation/recombination of the layers.
使用的代码:
#Import statements.
import random
import numpy as np
from tensorflow import keras
import tensorflow as tf
from tensorflow.keras import layers as L
from collections import deque
#Op function.
def op(x, units, kernel, stride, activation):
x_list = []
for i in range(units):
x_list += L.Conv2D(1, kernel, stride, activation=activation)(x)
x = L.Concatenate(-1)(x_list)
return x
#build_model method
def build_model(self):
inputx = L.Input(shape=self.state_size)
goalx = L.Input(shape=self.state_size)
x = L.Concatenate(1)([goalx, inputx])
x = op(x, 4, (5,5), (1,1), activation=swish)
x = op(x, 4, (5,5), (1,1), activation=swish)
x = op(x, 16, (5,5), (2,2), activation=swish)
x = op(x, 16, (5,5), (2,2), activation=swish)
x = op(x, 16, (5,5), (1,1), activation=swish)
x = op(x, 16, (5,5), (1,1), activation=swish)
x = op(x, 16, (5,5), (1,1), activation=swish)
x = op(x, 16, (5,5), (1,1), activation=swish)
x = op(x, 16, (5,5), (1,1), activation=swish)
x = op(x, 16, (5,5), (1,1), activation=swish)
x = L.Flatten()(x)
outp = L.Dense(self.action_size, activation='softmax')(x)
valp = L.Dense(1)(x)
model = keras.models.Model([inputx, goalx], outp)
critic = keras.models.Model([inputx, goalx], valp)
model.compile(loss='msle', optimizer='adam')
critic.compile(loss='msle', optimizer='adam')
return model, critic
跟踪:
Traceback (most recent call last):
File "thoughtform.py", line 76, in <module>
main_loop()
File "thoughtform.py", line 50, in main_loop
dqn = DQN(frame.shape, 5000)
File "/home/ai/Projects/Thoughtforms/dqn.py", line 27, in __init__
self.model, self.critic = self.build_model()
File "/home/ai/Projects/Thoughtforms/dqn.py", line 33, in build_model
x = op(x, 4, (5,5), (1,1), activation=swish)
File "/home/ai/Projects/Thoughtforms/dqn.py", line 16, in op
x = L.Concatenate(-1)(x)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 817, in __call__
self._maybe_build(inputs)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 2141, in _maybe_build
self.build(input_shapes)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/tf_utils.py", line 306, in wrapper
output_shape = fn(instance, input_shape)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/merge.py", line 378, in build
raise ValueError('A `Concatenate` layer should be called '
ValueError: A `Concatenate` layer should be called on a list of at least 2 inputs
推荐答案
像这样重写您的函数:
def op(x, units, kernel, stride, activation):
x_list = []
for i in range(units):
x_list.append(L.Conv2D(1, kernel, stride, activation=activation)(x))
x = L.Concatenate(-1)(x_list)
return x
列表中的+=
运算符并没有您认为的那样,最后是将所有张量串联在一起,而不是将它们添加到列表中.使用append
获得预期效果.
The +=
operator on a list does not do what you think it does, in the end it is kind of concatenating all tensors instead of adding them to a list. Use append
for the intended effect.
这篇关于应在至少2个输入的列表上调用“连接"层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!