下面的代码给了我一个输入错误,我无法找出它
import tensorflow as tf
import neural_structured_learning as nsl
.
.
.
b_size = 132
m = tf.keras.Sequential()
m.add(tf.keras.layers.Dense(980, activation = 'relu', input_shape = (2206,2,)))
m.add(tf.keras.layers.Dense(560, activation = 'relu'))
m.add(tf.keras.layers.Dense(10, activation = 'softmax'))
adv_config = nsl.configs.make_adv_reg_config(multiplier=0.2, adv_step_size=0.5)
adv_model = nsl.keras.AdversarialRegularization(m, adv_config=adv_config)
adv_model.compile(optimizer = "adam",
loss = "sparse_categorical_crossentropy",
metrics = ['accuracy'])
adv_model.fit({"feature" : x_Train, "label" : y}, epochs = 50, batch_size=b_size)
我的x_系列具有(5002, 2206, 2)
形状(大小为(2206,2)
的5002个样本)。我试图在开始时添加一个Flatten()
层,但它给了我一个object of type 'NoneType' has no len()
错误,即使这在tf.keras中非常有效。我也尝试了不同的输入形状,但都不起作用。因此,它向我抛出了以下错误之一
KeyError: 'dense_115_input'
ValueError: Input 0 of layer sequential_40 is incompatible with the layer: expected axis -1 of input shape to have value 2206 but received input with shape [None, 2206, 2]
TypeError: object of type 'NoneType' has no len()
要使用输入字典(如
{"feature" : x_Train, "label" : y}
)训练NSL模型,基础模型必须知道字典中要查看的功能指定要素名称的一种方法是添加
Input
层:正如this answer所指出的,输入特征在传递到密集层之前必须被展平:
如果要使用密集层,则输入应为
(5002, 2206*2)
,即矩阵。 也许最简单的解决方案是在“适合”之前重塑输入x_序列或者,您可以使用
TimeDistributed
层(see here),但是这种层的使用取决于输入维度背后的物理含义。基本上,TimeDistributed
多次应用某个操作,在您的情况下是两次希望这能对你有所帮助
相关问题 更多 >
编程相关推荐