简体   繁体   English

无法从“keras.layers”导入名称“Merge”

[英]Cannot import name 'Merge' from 'keras.layers'

I have try run a code but I find a problem with merge layers of Keras .我尝试运行代码,但我发现Keras合并层有问题。 I'm using python 3 and keras 2.2.4我正在使用 python 3 和keras 2.2.4

This is de code part of code这是代码的 de code 部分


import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import LSTM, Embedding, TimeDistributed, Dense, RepeatVector, Merge, Activation
from keras.preprocessing import image, sequence
import cPickle as pickle


    def create_model(self, ret_model = False):

        image_model = Sequential()
        image_model.add(Dense(EMBEDDING_DIM, input_dim = 4096, activation='relu'))
        image_model.add(RepeatVector(self.max_length))

        lang_model = Sequential()
        lang_model.add(Embedding(self.vocab_size, 256, input_length=self.max_length))
        lang_model.add(LSTM(256,return_sequences=True))
        lang_model.add(TimeDistributed(Dense(EMBEDDING_DIM)))

        model = Sequential()
        model.add(Merge([image_model, lang_model], mode='concat'))
        model.add(LSTM(1000,return_sequences=False))
        model.add(Dense(self.vocab_size))
        model.add(Activation('softmax'))

        print ("Model created!")

This is the message of error这是错误信息

from keras.layers import LSTM, Embedding, TimeDistributed, Dense, RepeatVector, Merge, Activation
ImportError: cannot import name 'Merge' from 'keras.layers'

Merge is not supported in Keras +2. Keras +2 不支持Merge Instead, you need to use Concatenate layer:相反,您需要使用Concatenate层:

merged = Concatenate()([x1, x2]) # NOTE: the layer is first constructed and then it's called on its input

or it's equivalent functional interface concatenate (starting with lowercase c ):或者它是等效的功能接口concatenate (以小写c开头):

merged = concatenate([x1,x2]) # NOTE: the input of layer is passed as an argument, hence named *functional interface*

If you are interested in other forms of merging, eg addition, subtration, etc., then you can use the relevant layers.如果您对其他形式的合并感兴趣,例如加法、减法等,那么您可以使用相关层。 See the documentation for a comprehensive list of merge layers.有关合并图层的完整列表,请参阅文档

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

相关问题 from keras.layers import Dense -- 无法导入名称“Dense” - from keras.layers import Dense -- cannot import name 'Dense' 无法从“keras.layers”导入名称“Deconvolution2D” - cannot import name 'Deconvolution2D' from 'keras.layers' 如何从 Keras.layers 实现 Merge - How to implement Merge from Keras.layers ImportError:无法从“keras.layers”(/home/mona/venv/fall/lib/python3.8/site-packages/keras/layers/__init__.py)导入名称“Deconvolution3D” - ImportError: cannot import name 'Deconvolution3D' from 'keras.layers' (/home/mona/venv/fall/lib/python3.8/site-packages/keras/layers/__init__.py) 导入错误:无法从“keras.layers.normalization”导入名称“BatchNormalization” - ImportError: cannot import name 'BatchNormalization' from 'keras.layers.normalization' 无法从“tensorflow.python.keras.layers”导入名称“规范化” - cannot import name 'normalization' from 'tensorflow.python.keras.layers' 无法从“keras.layers.normalization”导入名称“BatchNormalization” - cannot import name 'BatchNormalization' from 'keras.layers.normalization' 从 keras.layers 导入 LayerNormalization 时出错 - Error when importing LayerNormalization from keras.layers keras.layers 中 model 架构中的无效语法 - Invalid syntax in model architecture in keras.layers 模块 'keras.layers' 没有属性 'experimental' - module 'keras.layers' has no attribute 'experimental'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM