Keras模型架构模型model.add参数

时间:2019-12-22 15:32:25

标签: python tensorflow keras neural-network

我对神经网络还很陌生,我正在尝试建立一个模型,该模型将获得2个形状不同的输​​入(3668、688和3668、300),火车输出形状为(3668、988) )。

我需要预测35个不同的班级。

我创建了这种模型,但没有对精度进行任何合理的改进。我知道我对放在model.add函数中的参数肯定做错了,但是我不确定应该在那放什么。

model = Sequential()
model.add(Dense(3668, input_dim=300, activation='tanh'))
model.add(Dense(988, activation= K.exp))

offset = Sequential()
offset.add(Dense(988, input_dim=688, activation='linear'))

input1 = Input(shape=(300,))
input2 = Input(shape=(688,))
x1 = model(input1)
x2 = offset(input2)

from keras.layers import Multiply, add
output = Multiply()([x1, x2])

from keras.models import Model
modelnew = Model(inputs=[input1, input2], outputs=output)

modelnew.compile(optimizer='rmsprop',loss='mse',metrics=['accuracy'])

modelnew.fit([categorical_train_data, numerical_train_data], train_outputs, epochs=10, steps_per_epoch=5)

您能帮我弄清楚吗?

1 个答案:

答案 0 :(得分:0)

我使用以下模型解决了该问题:

# define two sets of inputs
inputA = Input(shape=(300,))
inputB = Input(shape=(688,))

# the first branch operates on the first input
x = Dense(300, activation="relu", use_bias=True)(inputA)
x = Dense(50, activation="relu", use_bias=True)(x)
x = Model(inputs=inputA, outputs=x)

# the second branch opreates on the second input
y = Dense(688, activation="relu", use_bias=True)(inputB)
y = Dense(300, activation="relu", use_bias=True)(y)
y = Dense(50, activation="relu", use_bias=True)(y)
y = Model(inputs=inputB, outputs=y)

# combine the output of the two branches
combined = Concatenate()([x.output, y.output])

# apply a FC layer and then a regression prediction on the
# combined outputs
z = Dense(100, input_dim=100, activation='relu', use_bias=True)(combined)
z = Dense(35, activation='sigmoid')(z)

# our model will accept the inputs of the two branches and
# then output a single value
model2 = Model(inputs=[x.input, y.input], outputs=z)
model2.summary()

我从这里带走的: https://www.pyimagesearch.com/2019/02/04/keras-multiple-inputs-and-mixed-data/

,然后将其与发现的其他模型结合在一起。效果很好。

相关问题