ValueError:应在输入列表上调用合并层 [英] ValueError: A merge layer should be called on a list of inputs

查看:130
本文介绍了ValueError:应在输入列表上调用合并层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Keras中创建一个简单的回归模型.

I am trying to create a simple regression model in Keras.

我的模型有2个具有 ReLU 激活的隐藏层和具有线性激活的输出层.

My model has 2 hidden layers with ReLU activations and the output layer with linear activation.

我假设输入数据将具有32个功能.

I assume that the input data will have 32 features.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras import layers

model = tf.keras.Sequential()
layers.add(Dense(64, activation='relu', input_shape=(32,)))
layers.add(Dense(64, activation='relu'))
layers.add(Dense(1, activation = 'linear'))
model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
              loss='mean_absolute_percentage_error',
              metrics=['mean_absolute_percentage_error'])

这引发了以下异常:

ValueError                                Traceback (most recent call last)
<ipython-input-44-1a6555f9a2ae> in <module>()
      9     model = tf.keras.Sequential()
     10 
---> 11     layers.add(Dense(64, activation='relu', input_shape=(32,)))
     12 
     13     layers.add(Dense(64, activation='relu'))

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tensorflow\python\keras\layers\merge.py in add(inputs, **kwargs)
    586   ```
    587   """
--> 588   return Add(**kwargs)(inputs)
    589 
    590 

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
    655         # Eager execution on data tensors.
    656         with ops.name_scope(self._name_scope()):
--> 657           self._maybe_build(inputs)
    658           with base_layer_utils.autocast_context_manager(
    659               input_list, self._mixed_precision_policy.should_cast_variables):

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _maybe_build(self, inputs)
   1711     # Only call `build` if the user has manually overridden the build method.
   1712     if not hasattr(self.build, '_is_default'):
-> 1713       self.build(input_shapes)
   1714     # We must set self.built since user defined build functions are not
   1715     # constrained to set self.built.

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tensorflow\python\keras\utils\tf_utils.py in wrapper(instance, input_shape)
    288     if input_shape is not None:
    289       input_shape = convert_shapes(input_shape, to_tuples=True)
--> 290     output_shape = fn(instance, input_shape)
    291     # Return shapes from `fn` as TensorShapes.
    292     if output_shape is not None:

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tensorflow\python\keras\layers\merge.py in build(self, input_shape)
     88     # Used purely for shape validation.
     89     if not isinstance(input_shape, list):
---> 90       raise ValueError('A merge layer should be called on a list of inputs.')
     91     if len(input_shape) < 2:
     92       raise ValueError('A merge layer should be called '

ValueError: A merge layer should be called on a list of inputs.

我不确定是什么引发了该异常以及错误消息的含义是什么?

I am not sure what provokes this exception and what the error message implies.​

推荐答案

我认为您要这样做:

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense, Activation

from tensorflow.keras import layers

model = tf.keras.Sequential()

model.add(Dense(64, activation='relu', input_shape=(32,)))

model.add(Dense(64, activation='relu'))

model.add(Dense(1, activation = 'linear'))

model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
              loss='mean_absolute_percentage_error',
              metrics=['mean_absolute_percentage_error'])

将图层添加到模型中(您的代码尝试调用keras图层 add ).

Which adds layers to your model (your code tries to call the keras layer add instead).

这篇关于ValueError:应在输入列表上调用合并层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆