【问题标题】:Add SVM to last layer将 SVM 添加到最后一层
【发布时间】:2019-03-31 20:07:23
【问题描述】:

我做了什么:

我使用 Keras 实现了以下模型:

train_X, test_X, train_Y, test_Y = train_test_split(X, Y, test_size=0.2, random_state=np.random.seed(7), shuffle=True)

train_X = np.reshape(train_X, (train_X.shape[0], 1, train_X.shape[1]))
test_X = np.reshape(test_X, (test_X.shape[0], 1, test_X.shape[1]))

inp = Input((train_X.shape[1], train_X.shape[2]))
lstm = LSTM(1, return_sequences=False)(inp)
output = Dense(train_Y.shape[1], activation='softmax')(lstm)

model = Model(inputs=inp, outputs=output)
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
model.fit(train_X, train_Y, validation_split=.20, epochs=2, batch_size=50)

我想要什么:

我想将 SVM 添加到模型的最后一层,但我不知道怎么做?有什么想法吗?

【问题讨论】:

  • @UpasanaMittal 你能给我举个例子吗?不知道怎么弄?

标签: python neural-network keras svm


【解决方案1】:

使用 keras 2.2.4 将 softmax 更改为 linear 并添加 kernel_regularizer=l2(1e-4) 而不是 W_regularizer=l2(0.01)。使用loss = categorical_hinge

【讨论】:

    【解决方案2】:

    这应该适用于将 svm 添加为最后一层。

    inp = Input((train_X.shape[1], train_X.shape[2]))
    lstm = LSTM(1, return_sequences=False)(inp)
    output = Dense(train_Y.shape[1], activation='softmax', W_regularizer=l2(0.01)))(lstm)
    
    model = Model(inputs=inp, outputs=output)
    model.compile(loss='hinge', optimizer='adam', metrics=['accuracy'])
    model.fit(train_X, train_Y, validation_split=.20, epochs=2, batch_size=50)
    

    在这里,考虑到二进制分类目标,我使用hinge 作为损失。但如果不止于此,那么可以考虑使用categorical_hinge

    【讨论】:

      猜你喜欢
      • 2022-06-15
      • 2020-06-02
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多