Concatenation layer raise Value Error: as_list() is not defined on an unknown Tensor Shape
Concatenation layer raise Value Error: as_list() is not defined on an unknown Tensor Shape
我正在尝试使用 keras 使用 Wide Deep Nural Network 制作 DNN 以下代码在尝试实现后产生以下内容,我还制作了我的客户激活函数 Randomized Relu
这是激活代码:
class RRELU(keras.layers.Layer):
def __init__(self, lower, upper, **kwargs):
super().__init__(**kwargs)
self.lower = lower
self.upper = upper
def call(self, inputs, training=None):
if training:
return tf.where(inputs >= 0, inputs, inputs /
np.random.uniform(self.lower, self.upper, 1)[0])
return tf.where(inputs >= 0, inputs, 2*inputs/(self.lower+self.upper))
def compute_output_shape(self, batch_input_shape):
return tf.TensorShape(batch_input_shape.as_list())
def get_config(self):
base_config = super().get_config()
return {**base_config, 'lower': self.lower, 'upper': self.upper}
这是模型的代码。
layer = []
layer.append(keras.layers.Input(shape = X_train.toarray().shape[1:]))
for i in range(10):
layer.append(keras.layers.Dense(300))
layer.append(RRELU(3, 8))
layer.append(keras.layers.Concatenate()([layer[0], layer[-1]]))
layer.append(keras.layers.Dense(2, activation='softmax'))
model39 = keras.models.Sequential(layer)
此行产生错误:
layer.append(keras.layers.Concatenate()([layer[0], layer[-1]]))
错误信息:
ValueError Traceback (most recent call last)
<ipython-input-28-ad6c20fa06bd> in <module>()
4 layer.append(keras.layers.Dense(300))
5 layer.append(RRELU(3, 8))
----> 6 layer.append(keras.layers.Concatenate()([layer[0], layer[-1]]))
7 layer.append(keras.layers.Dense(2, activation='softmax'))
8 model39 = keras.models.Sequential(layer)
1 frames
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py in as_list(self)
1221 """
1222 if self._dims is None:
-> 1223 raise ValueError("as_list() is not defined on an unknown TensorShape.")
1224 return [dim.value for dim in self._dims]
1225
ValueError: as_list() is not defined on an unknown TensorShape.
提前感谢任何有用的评论。
出现此错误的原因在 tf.keras.Sequential
的文档中进行了解释:
“顺序模型适用于简单的层堆栈,其中 每个层只有一个输入张量和一个输出张量。”
这是因为当一个层添加到 Sequential
模型时,添加层的输入会自动设置为前一层的输出。像你这样的连接意味着第一个密集层的输出将被下一层和下游的连接层使用,这与顺序模型的想法背道而驰。
解决方案是使用 Functional API 代替:
import numpy as np
X_train = np.zeros((1, 100))
layer = []
layer.append(keras.layers.Input(shape=X_train.shape[1:]))
layer.append(keras.layers.Dense(300)(layer[-1]))
for i in range(10):
layer.append(keras.layers.Dense(300)(layer[-1]))
layer.append(RRELU(3, 8)(layer[-1]))
layer.append(keras.layers.Dense(2, activation='softmax')(layer[-1]))
layer.append(keras.layers.Concatenate()([layer[1], layer[-1]]))
model = tf.keras.Model(inputs=layer[0], outputs=layer[-1])
model.summary()
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 100)] 0 []
dense (Dense) (None, 300) 30300 ['input_1[0][0]']
dense_1 (Dense) (None, 300) 90300 ['dense[0][0]']
rrelu (RRELU) (None, 300) 0 ['dense_1[0][0]']
dense_2 (Dense) (None, 300) 90300 ['rrelu[0][0]']
rrelu_1 (RRELU) (None, 300) 0 ['dense_2[0][0]']
...
dense_10 (Dense) (None, 300) 90300 ['rrelu_8[0][0]']
rrelu_9 (RRELU) (None, 300) 0 ['dense_10[0][0]']
dense_11 (Dense) (None, 2) 602 ['rrelu_9[0][0]']
concatenate (Concatenate) (None, 302) 0 ['dense[0][0]',
'dense_11[0][0]']
==================================================================================================
Total params: 933,902
Trainable params: 933,902
Non-trainable params: 0
__________________________________________________________________________________________________
我正在尝试使用 keras 使用 Wide Deep Nural Network 制作 DNN 以下代码在尝试实现后产生以下内容,我还制作了我的客户激活函数 Randomized Relu 这是激活代码:
class RRELU(keras.layers.Layer):
def __init__(self, lower, upper, **kwargs):
super().__init__(**kwargs)
self.lower = lower
self.upper = upper
def call(self, inputs, training=None):
if training:
return tf.where(inputs >= 0, inputs, inputs /
np.random.uniform(self.lower, self.upper, 1)[0])
return tf.where(inputs >= 0, inputs, 2*inputs/(self.lower+self.upper))
def compute_output_shape(self, batch_input_shape):
return tf.TensorShape(batch_input_shape.as_list())
def get_config(self):
base_config = super().get_config()
return {**base_config, 'lower': self.lower, 'upper': self.upper}
这是模型的代码。
layer = []
layer.append(keras.layers.Input(shape = X_train.toarray().shape[1:]))
for i in range(10):
layer.append(keras.layers.Dense(300))
layer.append(RRELU(3, 8))
layer.append(keras.layers.Concatenate()([layer[0], layer[-1]]))
layer.append(keras.layers.Dense(2, activation='softmax'))
model39 = keras.models.Sequential(layer)
此行产生错误:
layer.append(keras.layers.Concatenate()([layer[0], layer[-1]]))
错误信息:
ValueError Traceback (most recent call last)
<ipython-input-28-ad6c20fa06bd> in <module>()
4 layer.append(keras.layers.Dense(300))
5 layer.append(RRELU(3, 8))
----> 6 layer.append(keras.layers.Concatenate()([layer[0], layer[-1]]))
7 layer.append(keras.layers.Dense(2, activation='softmax'))
8 model39 = keras.models.Sequential(layer)
1 frames
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py in as_list(self)
1221 """
1222 if self._dims is None:
-> 1223 raise ValueError("as_list() is not defined on an unknown TensorShape.")
1224 return [dim.value for dim in self._dims]
1225
ValueError: as_list() is not defined on an unknown TensorShape.
提前感谢任何有用的评论。
出现此错误的原因在 tf.keras.Sequential
的文档中进行了解释:
“顺序模型适用于简单的层堆栈,其中 每个层只有一个输入张量和一个输出张量。”
这是因为当一个层添加到 Sequential
模型时,添加层的输入会自动设置为前一层的输出。像你这样的连接意味着第一个密集层的输出将被下一层和下游的连接层使用,这与顺序模型的想法背道而驰。
解决方案是使用 Functional API 代替:
import numpy as np
X_train = np.zeros((1, 100))
layer = []
layer.append(keras.layers.Input(shape=X_train.shape[1:]))
layer.append(keras.layers.Dense(300)(layer[-1]))
for i in range(10):
layer.append(keras.layers.Dense(300)(layer[-1]))
layer.append(RRELU(3, 8)(layer[-1]))
layer.append(keras.layers.Dense(2, activation='softmax')(layer[-1]))
layer.append(keras.layers.Concatenate()([layer[1], layer[-1]]))
model = tf.keras.Model(inputs=layer[0], outputs=layer[-1])
model.summary()
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 100)] 0 []
dense (Dense) (None, 300) 30300 ['input_1[0][0]']
dense_1 (Dense) (None, 300) 90300 ['dense[0][0]']
rrelu (RRELU) (None, 300) 0 ['dense_1[0][0]']
dense_2 (Dense) (None, 300) 90300 ['rrelu[0][0]']
rrelu_1 (RRELU) (None, 300) 0 ['dense_2[0][0]']
...
dense_10 (Dense) (None, 300) 90300 ['rrelu_8[0][0]']
rrelu_9 (RRELU) (None, 300) 0 ['dense_10[0][0]']
dense_11 (Dense) (None, 2) 602 ['rrelu_9[0][0]']
concatenate (Concatenate) (None, 302) 0 ['dense[0][0]',
'dense_11[0][0]']
==================================================================================================
Total params: 933,902
Trainable params: 933,902
Non-trainable params: 0
__________________________________________________________________________________________________