Tensorflow: 使用神经网络对积极或消极短语进行分类

我正在按照这个教程进行学习:https://pythonprogramming.net/train-test-tensorflow-deep-learning-tutorial/

我已经能够训练神经网络并打印出准确率。

然而,我不知道如何使用神经网络进行预测。

这是我的尝试。具体问题在于这一行 – 我认为我的问题是我无法将输入字符串转换为模型期望的格式:

features = get_features_for_input("This was the best store i've ever seen.")result = (sess.run(tf.argmax(prediction.eval(feed_dict={x:features}),1)))

这里是更大的代码列表:

def train_neural_network(x):    prediction = neural_network_model(x)    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))     optimizer = tf.train.AdamOptimizer().minimize(cost)    with tf.Session() as sess:        sess.run(tf.global_variables_initializer())        for epoch in range(hm_epochs):            epoch_loss = 0            i = 0            while i < len(train_x):                start = i                end = i + batch_size                batch_x = np.array(train_x[start:end])                batch_y = np.array(train_y[start:end])                _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})                epoch_loss += c                 i+=batch_size            print('Epoch', epoch, 'completed out of', hm_epochs, 'loss:', epoch_loss)        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y,1))                accuracy = tf.reduce_mean(tf.cast(correct,'float'))        print('Accuracy', accuracy.eval({x:test_x, y:test_y}))        # pos: [1,0] , argmax: 0        # neg: [0,1] , argmax: 1        features = get_features_for_input("This was the best store i've ever seen.")        result = (sess.run(tf.argmax(prediction.eval(feed_dict={x:features}),1)))        if result[0] == 0:            print('Positive:',input_data)        elif result[0] == 1:            print('Negative:',input_data)def get_features_for_input(input):    current_words = word_tokenize(input.lower())    current_words = [lemmatizer.lemmatize(i) for i in current_words]    features = np.zeros(len(lexicon))    for word in current_words:        if word.lower() in lexicon:            index_value = lexicon.index(word.lower())            # OR DO +=1, test both            features[index_value] += 1    features = np.array(list(features))train_neural_network(x)

回答:

根据你上面的评论,你的错误ValueError: Cannot feed value of shape ()似乎是因为featuresNone,因为你的函数get_features_for_input没有返回任何内容。

我添加了return features行,并将features调整为正确的形状[1, len(lexicon)]以匹配占位符的形状。

def get_features_for_input(input):    current_words = word_tokenize(input.lower())    current_words = [lemmatizer.lemmatize(i) for i in current_words]    features = np.zeros((1, len(lexicon)))    for word in current_words:        if word.lower() in lexicon:            index_value = lexicon.index(word.lower())            # OR DO +=1, test both            features[0, index_value] += 1    return features

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注