我正在使用带有 tensorflow 后端的 keras,并且在为我的模型确定图层的正确形状时遇到了问题。
我已经阅读了关于各种 keras 层属性差异的有用解释。
这是我的模型的架构:
我正在尝试使用分类标签进行二元分类(逻辑回归),因此最后一层是具有 1 个单元的 Dense 层,我认为对于正类评估为 1,对于负类评估为 0。
这是我的模型的总结:
我在网络一侧的输入是 10158,另一侧是 20316。我总共有 1370 个样本。我的 train_data 的形状是 (1370, 1, 10158),标签的形状是 (1, 1370),批量大小是 100。
input_layer = Input(shape=(1,no_terms), name='docs')
s = Lambda(lambda x: x+1)(input_layer)
log_layer = Lambda(log, name='tf_output')(input_layer)
tpr_fpr = np.zeros((2, no_terms))
tpr_fpr[0,:] = np.sum(train_docs[np.where(train_label>0), :]>0, axis=1
)/np.sum(train_label>0) * (1000)
tpr_fpr[1,:] = np.sum(train_docs[np.where(train_label>0), :]>0, axis=1
)/np.sum(train_label <= 0) * (1000)
k_constants = backend.constant(np.reshape(tpr_fpr.T, (1,2*no_terms)))
fixed_input = Input(tensor=k_constants, shape=(1, 2*no_terms), name='tpr_fpr')
h = Dense(int(300), activation='relu', name='hidden', input_shape=(1, 2*no_terms),
trainable=True)(fixed_input)
h = Dropout(0.2, name="D")(h)
cd = Dense(units=no_terms, activation='relu', name='cd', trainable=True)(h)
prod = Multiply()([log_layer, cd])
o = Lambda(lambda x:(x/backend.sqrt(backend.sum(x * x,axis=1,keepdims=True))))(prod)
o = ReLU(max_value=None, negative_slope=0.0, threshold=0.0)(o)
o = Dense(1, activation='sigmoid', input_shape=(no_terms,))(o)
model_const = Model(fixed_input,cd)
model = Model([input_layer, fixed_input], o)
op = optimizers.RMSprop(learning_rate=.1, rho=0.9)
model.compile(optimizer=op, loss=mean_squared_error, metrics=['accuracy'])
plot_model(model, to_file='model.png')
model.summary()
batchSize = 100
这是我得到的错误:“ValueError:检查目标时出错:预期dense_1有3个维度,但得到了形状为(1430、2)的数组”
我不知道 (1430, 2) 形状指的是什么以及为什么会出现此错误。
繁星coding
婷婷同学_
莫回无
相关分类