您所要做的就是将tensorflow 张量转换为numpy 并继续使用numpy 操作
代码
def get_variation_uncertainty(prediction_score_vectors, matrix_size):
prediction_score_vectors = torch.stack(tuple(prediction_score_vectors))
wt_var = np.var(np.sum(prediction_score_vectors[:, :, 1:].cpu().numpy(), axis=2), axis=0).reshape(matrix_size) * 100
tc_var = np.var(np.sum(prediction_score_vectors[:, :, [1, 3]].cpu().numpy(), axis=2), axis=0).reshape( matrix_size) *100
et_var = np.var(prediction_score_vectors[:, :, 3].cpu().numpy(), axis=0).reshape(matrix_size) * 100
return wt_var.astype(np.uint8), tc_var.astype(np.uint8), et_var.astype(np.uint8)
def get_variation_uncertainty_tf(prediction_score_vectors, matrix_size):
prediction_score_vectors = tf.stack(prediction_score_vectors).numpy()
wt_var = np.var(np.sum(prediction_score_vectors[:, :, 1:], axis=2), axis=0).reshape(matrix_size) * 100
tc_var = np.var(np.sum(prediction_score_vectors[:, :, [1, 3]], axis=2), axis=0).reshape( matrix_size) *100
et_var = np.var(prediction_score_vectors[:, :, 3], axis=0).reshape(matrix_size) * 100
return wt_var.astype(np.uint8), tc_var.astype(np.uint8), et_var.astype(np.uint8)
print (get_variation_uncertainty(prediction_score_vectors, (4,4)))
print (get_variation_uncertainty_tf(prediction_score_vectors, (4,4)))
输出:
(array([[121, 121, 131, 117],
[120, 103, 126, 135],
[112, 125, 114, 112],
[137, 109, 123, 154]], dtype=uint8), array([[18, 15, 19, 20],
[17, 13, 14, 17],
[15, 19, 15, 16],
[18, 17, 15, 17]], dtype=uint8), array([[8, 8, 8, 8],
[8, 8, 6, 8],
[7, 8, 7, 7],
[9, 8, 8, 7]], dtype=uint8))
(array([[121, 121, 131, 117],
[120, 103, 126, 135],
[112, 125, 114, 112],
[137, 109, 123, 154]], dtype=uint8), array([[18, 15, 19, 20],
[17, 13, 14, 17],
[15, 19, 15, 16],
[18, 17, 15, 17]], dtype=uint8), array([[8, 8, 8, 8],
[8, 8, 6, 8],
[7, 8, 7, 7],
[9, 8, 8, 7]], dtype=uint8))