简体   繁体   English

如何为每个可能的 class 制作样本的 tensorflow CNN 返回概率?

[英]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.

相关问题 TensorFlow 中 CNN 的样本权重 - Sample weighting for CNN in TensorFlow 如何对 tensorflow 中的 CNN kernel 进行限制? - How to make restrictions to the CNN kernel in tensorflow? 如何返回每个分类实例的概率? - How to return the probability of each classified instance? 如何获得每个 class 的概率和 label? - How to get the probability and label for each class? 如何获得属于 class 的每个图像的概率 - How to get probability of each image belonging to a class 输出一类 CNN 神经网络的置信度/概率 - Output the confiendence / probability for a class of a CNN neuronal network TensorFlow 二值图像分类:预测数据集中每个图像的每个 class 的概率 - TensorFlow Binary Image Classification: Predict Probability of each class for each image in data set 如何获得多项式情况下每个可能结果的概率? - How to get the probability of each possible outcome for a multinomial case? Output score/probability for all class for each object with Tensorflow object detection API - Output score/probability for all class for each object with Tensorflow object detection API 如何为张量流中每个值的给定概率采样张量值? - How to sample tensor values given probabilities for each value in tensorflow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM