在keras文本分类模型中进行预测时如何循环

时间:2019-02-07 23:58:21

标签: json python-3.x text keras classification

我是keras的新手。我写了一个文本分类模型,对一个输入进行预测时,我得到了正确的预测,如下所示:

text=["Cancelling insurance cover that is in excess of your needs"]
one_test = tokenize.texts_to_matrix(text)
text_array=np.array([one_test[0]])
preds = model.predict(text_array)
yhat1 = model.predict_classes(text_array)
yhat2 = model.predict_proba(text_array)
print(preds)
print(yhat1)
print(yhat2)
prediction1=np.argmax(preds)
print(prediction1)

输出: [[0.21625464 0.17296328 0.17964244 0.27282426 0.15831545]]

[3]

[[0.21625464 0.17296328 0.17964244 0.27282426 0.15831545]]

3

但是,想要发送输入列表以进行预测

prediction_list=[]
Actionlist= ["Cancelling insurance cover that is in excess of your 
needs","Decrease loan payment","use your surplus cash reserves to pay for 
holiday expense or travel"]
for text in Actionlist:
    print(text)
    one_test = tokenize.texts_to_matrix(text)
    text_array=np.array([one_test[0]])
    preds = model.predict(text_array)
    print(preds)
    yhat1 = model.predict_classes(text_array)
    print(yhat1)
    prediction=np.argmax(preds)
    print(prediction)
    prediction_list.append(prediction)
print(prediction_list)

我得到以下输出,而不是得到三个预测。

取消超出您需求的保险

[[0.20537896 0.20620751 0.1970055 0.1982517 0.19315639]]

[1]

1

减少贷款付款

[[0.20537896 0.20620751 0.1970055 0.1982517 0.19315639]]

[1]

1

使用您的剩余现金储备来支付度假费用或旅行

[[0.20537896 0.20620751 0.1970055 0.1982517 0.19315639]]

[1]

1

[1,1,1]

请帮助 预先感谢

1 个答案:

答案 0 :(得分:0)

问题是您需要text_to_matrix()的列表。因此,只需将 <?php if ( is_active_sidebar( 'above-related-1' ) ) : ?> <div class="above-related-widget-area" role="complementary"> <?php dynamic_sidebar( 'above-related-1' ); ?> </div> <?php endif; ?> <?php // Get the current post id $post_id = get_queried_object_id(); // Get the post categories $categories = get_the_category( $post_id ); // Lets build our array // If we don't have categories, bail if ( !$categories ) return false; foreach ( $categories as $category ) { if ( $category->parent == 0 ) { $term_ids[] = $category->term_id; } else { $term_ids[] = $category->parent; $term_ids[] = $category->term_id; } } // Remove duplicate values from the array $unique_array = array_unique( $term_ids ); $args = [ 'post__not_in' => [$post_id], 'posts_per_page' => get_theme_mod('post_page_related_num_results', $abcd_defaults['post_page_related_num_results']), 'ignore_sticky_posts' => 1, 'orderby' => 'rand', 'no_found_rows' => true, 'tax_query' => [ [ 'taxonomy' => 'category', 'terms' => $unique_array, 'include_children' => false, ], ], ]; $wp_query = new WP_Query($args); echo '<h3 id="post-page-related-title">'.esc_html(get_theme_mod('post_page_related_label', $abcd_defaults['post_page_related_label'])).'</h3>'; ?> 设置为text_to_matrix()即可。