我正在尝试在Keras中创建一个简单的回归模型。
我的模型有2个具有ReLU
激活的隐藏层和具有线性激活的输出层。
我假设输入数据将具有32个功能。
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.
我不确定是什么引发了该异常以及错误消息的含义是什么?
答案 0 :(得分:0)
我认为您想做的是这样
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
)。