我正在使用带有以下图层的keras模型来预测输入标签(4个标签中)
embedding_layer = keras.layers.Embedding(MAX_NB_WORDS,
EMBEDDING_DIM,
weights=[embedding_matrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
sequence_input = keras.layers.Input(shape = (MAX_SEQUENCE_LENGTH,),
dtype = 'int32')
embedded_sequences = embedding_layer(sequence_input)
hidden_layer = keras.layers.Dense(50, activation='relu')(embedded_sequences)
flat = keras.layers.Flatten()(hidden_layer)
preds = keras.layers.Dense(4, activation='softmax')(flat)
model = keras.models.Model(sequence_input, preds)
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['acc'])
model.fit(X_train, Y_train, batch_size=32, epochs=100)
但是,softmax函数返回4的输出数(因为我有4个标签)
当我使用预测函数使用相同的模型获得预测的Y时,我得到的每个X的数组为4,而不是一个标签决定输入的标签。
model.predict(X_test, batch_size = None, verbose = 0, steps = None)
如何使keras模型的输出层或model.predict函数决定一个标签,而不是为每个标签输出权重?
答案 0 :(得分:0)
以下是从概率向量中抽样的常用函数
@model Model.khachhang
@using RaovatThuCung.Common
@{
ViewBag.Title = "KetQua";
Layout = "~/Views/Shared/_Layout.cshtml";
var session = (UserLogin)Session[CommonConstants.USER_SESSION];
}
<h2>KetQua</h2>
@using (Html.BeginForm("KetQua","MuaVip",FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Thanh toán thành công</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.Hidden("session.UserID",session.UserID)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Kích hoạt vip" class="btn btn-default" />
</div>
</div>
</div>
}
取自here。
温度参数决定概率权重之间的差异加权多少。温度为1正在考虑每个重量“按原样”,大于1的温度会减小重量之间的差异,小于1的温度会增加它们。
这是一个在3个标签上使用概率向量的例子:
def sample(preds, temperature=1.0):
# helper function to sample an index from a probability array
preds = np.asarray(preds).astype('float64')
preds = np.log(preds) / temperature
exp_preds = np.exp(preds)
preds = exp_preds / np.sum(exp_preds)
probas = np.random.multinomial(1, preds, 1)
return np.argmax(probas)
要查看新的向量,请p = np.array([0.1, 0.7, 0.2]) # The first label has a probability of 10% of being chosen, the second 70%, the third 20%
print(sample(p, 1)) # sample using the input probabilities, unchanged
print(sample(p, 0.1)) # the new vector of probabilities from which to sample is [ 3.54012033e-09, 9.99996371e-01, 3.62508322e-06]
print(sample(p, 10)) # the new vector of probabilities from which to sample is [ 0.30426696, 0.36962778, 0.32610526]
返回sample
。