我试图在 Keras 前馈神经网络的最后一步中包含一个变量。我似乎只有在包含 2 列而不是仅包含一列时才能使其正常工作。这是我的代码示例:
首先我准备主要输入数据集:
import pandas as pd
from keras.models import Model
from keras.layers import Dense, Input, Concatenate
from keras.optimizers import Adam
iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
iris.loc[:, 'target'] = (iris.species == "setosa").map(int)
train_x = iris.drop(columns=['target', 'species'])
train_y = iris['target'].map(int)
然后我将
train_x
分成两个单独的数据帧,并将它们输入到不同位置的网络中:
feature_x = train_x.drop(columns='petal_width')
single_feature_x = train_x[['petal_width']]
input_x = Input(shape=feature_x.shape, name='feature_input')
single_input_x = Input(shape=single_feature_x.shape, name='single_input')
x = Dense(4, activation='relu')(input_x)
concat_feat = Concatenate(axis=-1, name='concat_fc')([x, single_input_x])
outputs = Dense(1, activation='sigmoid')(concat_feat)
model = Model(inputs=[input_x, single_input_x], outputs=outputs)
model.compile(loss='binary_crossentropy',
optimizer=Adam(lr=0.001))
model.fit({'feature_input': feature_x,
'single_input': single_feature_x},
train_y,
epochs=100,
batch_size=512,
verbose=1)
这会引发错误:
ValueError: Shape must be rank 2 but is rank 3 for '{{node model_5/concat_fc/concat}} = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32](model_5/dense_10/Relu, model_5/Cast_1, model_5/concat_fc/concat/axis)' with input shapes: [?,4], [?,1,1], [].
但是,如果我添加这一行,它运行得很好:
feature_x = train_x.drop(columns='petal_width')
single_feature_x = train_x[['petal_width']]
# Add a constant column so the shape becomes (?,2)
single_feature_x.loc[:, 'constant'] = 0
为什么这适用于两列,但不适用于一列?
您只需正确指定输入形状即可。对于 2D 数据,您只需要传递特征 dim。样品尺寸不是必需的。您只需将输入更正为:
input_x = Input(shape=feature_x.shape[1], name='feature_input')
single_input_x = Input(shape=single_feature_x.shape[1], name='single_input')
嗨,我们如何将数据增强应用于此结构?你有尝试过吗?