2017-04-14 1 views
0

J'utilise cet ensemble de données IGN pour trouver des rétroactions positives et négatives sur les jeux, Fondamentalement il y a deux colonnes, sentiment qui contient 0 ou 1 (mauvais ou bon) et title qui pourrait être perdu 'ne peut pas prédire le résultat d'une nouvelle phrase avec tflearn

Principalement j'utilise tflearn pour faire le noyau dur. Tout se passe bien, seul le problème est de prédire le nouvel exemple.

#lets load the ign dataset 
dataframe = pd.read_csv('ign.csv') 

# Convert score_phrase to binary sentiments and add a new column called sentiment 
bad_phrases = ['Bad', 'Awful', 'Painful', 'Unbearable', 'Disaster'] 
dataframe['sentiment'] = dataframe.score_phrase.isin(bad_phrases).map({True: 0, False: 1}) 

# lets remove everything besides title and score_phrase 
dataframe = dataframe.drop(["score_phrase","Unnamed: 0","url","platform", "score", "genre", "editors_choice", "release_year", "release_month","release_day"], axis=1) 

#lets fill in any empty space with random spaces 
dataframe.fillna(value='', inplace=True) 

#preprocessing 
word_processor = VocabularyProcessor(100) 
#converting all the title as input featurres X 
trainX = np.array(list(word_processor.fit_transform(dataframe["title"]))) 
#converting the score_pharse to trainY since its counted as the label 
trainY = dataframe.loc[:, ["sentiment"]].as_matrix() 

# Network building 
def build_model(): 
    # This resets all parameters and variables, 
    tf.reset_default_graph() 
    net = tflearn.input_data([None, 100])  # Input 

    net = tflearn.fully_connected(net, 200, activation='ReLU')  # Hidden 
    net = tflearn.fully_connected(net, 200, activation='ReLU') 

    net = tflearn.fully_connected(net, 1, activation='softmax') # Output 
    net = tflearn.regression(net, optimizer='sgd', learning_rate=0.01, loss='categorical_crossentropy') 

    model = tflearn.DNN(net) 
    return model 
model = build_model() 
# Training 
model.fit(trainX, trainY, validation_set=0.1, show_metric=True, batch_size=128, n_epoch=10) 

Ici, tout va à l'enfer, tout en prédisant.

example = 'Little Big Planet' 
text = np.array(word_processor.fit(example)) 
pred_class = np.argmax(model.predict([text])) 

L'erreur que je reçois est

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-59-14b2aecc0f49> in <module>() 
     1 example = 'Little Big Planet' 
     2 text = np.array(word_processor.fit(example)) 
----> 3 pred_class = np.argmax(model.predict([text])) 
     4 pred_class 

/anaconda/envs/MLHardCore/lib/python3.5/site-packages/tflearn/models/dnn.py in predict(self, X) 
    229   """ 
    230   feed_dict = feed_dict_builder(X, None, self.inputs, None) 
--> 231   return self.predictor.predict(feed_dict) 
    232 
    233  def predict_label(self, X): 

/anaconda/envs/MLHardCore/lib/python3.5/site-packages/tflearn/helpers/evaluator.py in predict(self, feed_dict) 
    67    prediction = [] 
    68    for output in self.tensors: 
---> 69     o_pred = self.session.run(output, feed_dict=feed_dict).tolist() 
    70     for i, val in enumerate(o_pred): # Reshape pred per sample 
    71      if len(self.tensors) > 1: 

/anaconda/envs/MLHardCore/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 
    765  try: 
    766  result = self._run(None, fetches, feed_dict, options_ptr, 
--> 767       run_metadata_ptr) 
    768  if run_metadata: 
    769   proto_data = tf_session.TF_GetBuffer(run_metadata_ptr) 

/anaconda/envs/MLHardCore/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 
    936     ' to a larger type (e.g. int64).') 
    937 
--> 938   np_val = np.asarray(subfeed_val, dtype=subfeed_dtype) 
    939 
    940   if not subfeed_t.get_shape().is_compatible_with(np_val.shape): 

/anaconda/envs/MLHardCore/lib/python3.5/site-packages/numpy/core/numeric.py in asarray(a, dtype, order) 
    529 
    530  """ 
--> 531  return array(a, dtype, copy=False, order=order) 
    532 
    533 

ValueError: setting an array element with a sequence. 

Répondre

0

Je ne sais pas si cela aidera tout mais je suis en mesure d'obtenir une prédiction.

Ce sont mes importations (manquants à partir de votre code ci-dessus):

import pandas as pd 
from tflearn.data_utils import VocabularyProcessor 
import numpy as np 
import tensorflow as tf 
import tflearn 

Exécution model.predict([text]) RECRÉE erreur.

solution potentielle:

example = 'Little Big Planet' 
text = list(word_processor.transform([example]))[0].reshape(1, 100) 
model.predict(text) 

Sortie:

[[1.0]] 

Edit:

Pour voir les correspondances de mots:

>>> vocab_dict = word_processor.vocabulary_._mapping 
>>> vocab_dict['Little'] 
1 
>>> vocab_dict['Big'] 
2 
>>> vocab_dict['Planet'] 
3 
>>> vocab_dict['World'] 
75 

Pour voir si ces chiffres ont un sens, nous vérifions par:

>>> example = 'Little Big Planet' 
>>> text = list(word_processor.transform([example]))[0].reshape(1, 100) 
>>> text 
array([[1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int64) 
>>> example = 'Little Big World' 
>>> text = list(word_processor.transform([example]))[0].reshape(1, 100) 
>>> text 
array([[ 1, 2, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int64) 
+0

Jarad, cela fonctionne vraiment, mais toutes les sorties sont [1.0] :(J'ai changé les noms de jeu et ils ne changent toujours pas :(Si Vous voulez que je puisse vous envoyer le lien IGN: D –

+0

J'ai trouvé le lien IGN aux données. J'ai édité ma réponse et inspecté l'objet 'word_processor' et le tableau' text' sur lequel nous prévoyons semble fonctionner comme prévu. Je ne suis pas sûr pourquoi toutes les sorties seraient 1.0. – Jarad

+0

Je veux dire qu'il y a des commentaires positifs et négatifs, certains résultats devraient également être différents. –