[英]How to make tensorflow CNN return probability of a sample for each possible class?
Is there a way to make my tensorflow CNN return the probability of a sample for each of the possible classes?有没有办法让我的 tensorflow CNN 返回每个可能类的样本概率? (Eg sample x has a 83% chance of belonging to class 0, a 7% chance of class 1, a 10% chance of class 2).
(例如样本 x 有 83% 的机会属于 class 0,有 7% 的机会属于 class 1,有 10% 的机会属于 class 2)。
My model:我的 model:
model_0 = keras.Sequential()
model_0.add(Conv1D(32, kernel_size=3, strides=1, activation='relu', input_shape=(X_train.shape[1], 1)))
model_0.add(Dropout(0.1))
model_0.add(Conv1D(64, kernel_size=3, strides=1, activation='relu'))
model_0.add(Dropout(0.2))
model_0.add(Conv1D(32, kernel_size=3, strides=1, activation='relu'))
model_0.add(Flatten())
model_0.add(Dense(3, activation='softmax'))
model_0.compile(optimizer=Adam(learning_rate = 0.001), loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
model_0.summary()
history_0 = model_0.fit(X_train, y_train, epochs=30, validation_data=(X_val, y_val), verbose=1)
y_pred_0 = np.argmax(model_0.predict(X_test_pad), axis=-1)
Currently, by default, y_pred_0 is just a vector containing the index of the predicted class for each test sample.目前,默认情况下,y_pred_0 只是一个向量,其中包含每个测试样本的预测 class 的索引。 What would I have to change in my model in order to get probabilities?
为了获得概率,我必须在 model 中进行哪些更改?
Just simply archive output in probabilities by只需简单地将 output 归档为概率
y_pred_0 = model_0.predict(X_test_pad)
You would get as your example is你会得到你的例子是
[0.83, 0.07, 0.1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.