input_shape = Input(shape=(1,128,6))
a = input_shape[:,:,:,0:3]
b = input_shape[:,:,:,3:]
x1 = Conv2D(6,(1,1),activation='relu',padding='same')(a)
my_concat = Lambda(lambda x:keras.layers.concatenate([x[0],x[1]],axis=-1))
x2 = my_concat([input_shape,x1])
x3 = Conv2D(6,(1,1),activation='relu',padding='same')(b)
x = my_concat([x2,x3])
x=Conv2D(40, kernel_size=(1,7),padding='valid',
activation='relu',kernel_initializer='glorot_uniform',)(x)
x = MaxPooling2D(pool_size=(1, 2))(x)
x = Dropout(0.8)(x)
x = Flatten()(x)
output = Dense(num_classes, activation='softmax',kernel_initializer='glorot_uniform',)(x)
adam = keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
model = Model(inputs=input_shape,outputs=output)
model.summary()
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=adam,metrics=['accuracy'])
我想使用keras来构建一个cnn模型,但是我发现了一个错误,'Tensor'对象没有属性'_keras_history',我测试了lambda但它不起作用,所以我不知道应该怎么做我知道,我希望有人可以帮助我,非常感谢你。
> traceback (most recent call last): File "testdata.py", line 151, in
> <module>
> model = Model(inputs=input_shape,outputs=output) File "/home/percomp/.local/lib/python3.5/site-packages/keras/legacy/interfaces.py",
> line 91, in wrapper
> return func(*args, **kwargs) File "/home/percomp/.local/lib/python3.5/site-packages/keras/engine/topology.py",
> line 1734, in __init__
> build_map_of_graph(x, finished_nodes, nodes_in_progress) File "/home/percomp/.local/lib/python3.5/site-packages/keras/engine/topology.py",
> line 1724, in build_map_of_graph
> layer, node_index, tensor_index) File "/home/percomp/.local/lib/python3.5/site-packages/keras/engine/topology.py",
> line 1724, in build_map_of_graph
> layer, node_index, tensor_index) File "/home/percomp/.local/lib/python3.5/site-packages/keras/engine/topology.py",
> line 1724, in build_map_of_graph
> layer, node_index, tensor_index) File "/home/percomp/.local/lib/python3.5/site-packages/keras/engine/topology.py",
> line 1724, in build_map_of_graph
> layer, node_index, tensor_index) File "/home/percomp/.local/lib/python3.5/site-packages/keras/engine/topology.py",
> line 1724, in build_map_of_graph
> layer, node_index, tensor_index) File "/home/percomp/.local/lib/python3.5/site-packages/keras/engine/topology.py",
> line 1724, in build_map_of_graph
> layer, node_index, tensor_index) File "/home/percomp/.local/lib/python3.5/site-packages/keras/engine/topology.py",
> line 1724, in build_map_of_graph
> layer, node_index, tensor_index) File "/home/percomp/.local/lib/python3.5/site-packages/keras/engine/topology.py",
> line 1724, in build_map_of_graph
> layer, node_index, tensor_index) File "/home/percomp/.local/lib/python3.5/site-packages/keras/engine/topology.py",
> line 1724, in build_map_of_graph
> layer, node_index, tensor_index) File "/home/percomp/.local/lib/python3.5/site-packages/keras/engine/topology.py",
> line 1724, in build_map_of_graph
> layer, node_index, tensor_index) File "/home/percomp/.local/lib/python3.5/site-packages/keras/engine/topology.py",
> line 1695, in build_map_of_graph
> layer, node_index, tensor_index = tensor._keras_history AttributeError: 'Tensor' object has no attribute '_keras_history'
答案 0 :(得分:0)
您是否尝试过keras在keras中提供的Concatenate()函数?
像这样的keras.layers.Concatenate(axis=-1)
在我的模型中已经可以使用了。