Python 我无法获得正确的输入形状
下面的代码给了我一个输入错误,我无法找出它Python 我无法获得正确的输入形状,python,machine-learning,keras,tensorflow2.0,nsl,Python,Machine Learning,Keras,Tensorflow2.0,Nsl,下面的代码给了我一个输入错误,我无法找出它 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, acti
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_列车具有形状(5002206,2)
(5002个大小样本(2206,2)
)。我尝试在开始时添加一个flant()
层,但是它给了我一个类型为'NoneType'的对象没有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()
如果要使用密集层,输入应为
(5002206*2)
,即矩阵。
也许最简单的解决方案是在“适合”之前重塑输入x_序列
或者,您可以使用TimeDistributed
层(),但这种层的使用取决于输入维度背后的物理含义。基本上,TimeDistributed
多次应用某个操作,在您的情况下是两次
希望这能对你有所帮助 要使用输入词典训练NSL模型(如
{“feature”:x_train,“label”:y}
),基础模型必须知道词典中要查看的特征
指定要素名称的一种方法是添加输入
图层:
m = tf.keras.Sequential()
m.add(tf.keras.Input(name="feature", shape=(2206, 2)))
同样如前所述,输入特征在进入密集层之前必须变平:
m.add(tf.keras.layers.Flatten())
m.add(tf.keras.layers.Dense(...))
当我尝试使用keras训练模型时,它确实可以使用shape
(2206*2,)
和重塑x\u train
(但在beggining处添加一个扁平层就足以处理keras中的输入形状),但当我尝试使用nsl的对抗性正则化时,错误关键错误:“密集的115\u输入”
会弹出。而且我无法计算出时间分布层。