2017-07-18 2 views
0

Ich mache aus dem Kern Lernen.Ich muss ein n_gram Modell der Größe 3 erstellen. Ich habe sklearn's HashingVectorizer für diesen Zweck verwendet. Dann muss ich Keras verwenden, um das neurale Netzwerk zu erstellen . Ich bin aber nicht sicher, wie der Eingang zu fütternn_gram Modell HashingVectorizer und es mit Keras verwenden

vec = HashingVectorizer(decode_error = 'ignore', n_features = 2**20, ngram_range = (3,3)) 
X = vec.fit_transform(tags) 
y = np_utils.to_categorical(tag) 


print(X.shape) 
print(y.shape) 


model = Sequential() 
model.add(Dense(1024, input_shape = (1,X.shape[1]), activation = 'softmax')) 
model.add(Dense(y.shape[1], activation = 'softmax')) 

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy']) 
model.fit(X, y, epochs = 10, batch_size = 200) 

meine zweite bearbeiten Form: Hier ist fast alles, was das gleiche es nach wie vor wirft Fehler. Der Code ist:

from sklearn.feature_extraction.text import HashingVectorizer 
from sklearn.neural_network import MLPClassifier 
from sklearn.feature_extraction import FeatureHasher 
from sklearn.preprocessing import OneHotEncoder, LabelEncoder 
from keras.models import Sequential 
from keras.layers.recurrent import LSTM, GRU, SimpleRNN 
from keras.layers import Dense 
from keras.utils import np_utils 
from numpy import array 
from scipy.sparse import csr_matrix 

text = open('eng.train').read().lower().split() 
X_train = [] 
y_train = [] 

for i in range (len(text)): 
    if i % 4 == 0: 
     X_train.append(text[i]) 
    if i % 4 == 1: 
     y_train.append(text[i]) 

unq_tags = [] 
for i in range(len(y_train)): 
    if y_train[i] not in unq_tags: 
     unq_tags.append(y_train[i]) 
#hashing X_train  
vec = HashingVectorizer(decode_error = 'ignore', n_features = 2**15) 
X = vec.fit_transform(X_train) 
X.toarray() 
#one hot encoding y_train 
values = array(y_train) 
label_encoder = LabelEncoder() 
integer_encoded = label_encoder.fit_transform(values) 
encoded = np_utils.to_categorical(integer_encoded) 

print(type(X)) 
print(X.shape) 
print(type(encoded)) 
print(encoded.shape) 

model = Sequential() 
model.add(SimpleRNN(1024, input_shape = (X.shape[1],), activation = 'softmax')) 
model.add(Dense(y.shape[1], activation = 'softmax')) 
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy']) 
model.fit(X, y, epochs = 20, batch_size = 200) 

Der Fehler wirft es ist wie folgt:

class 'scipy.sparse.csr.csr_matrix'> 
(204567, 32768) 
<class 'numpy.ndarray'> 
(204567, 46) 

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-58-4ac701f1ade4> in <module>() 
    47 ''' 
    48 model = Sequential() 
---> 49 model.add(SimpleRNN(1024, input_shape = (X.shape[1],), activation = 'softmax')) 
    50 model.add(Dense(y.shape[1], activation = 'softmax')) 
    51 model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy']) 

/home/manish/anaconda3/lib/python3.6/site-packages/keras/models.py in add(self, layer) 
    420     # and create the node connecting the current layer 
    421     # to the input layer we just created. 
--> 422     layer(x) 
    423 
    424    if len(layer.inbound_nodes) != 1: 

/home/manish/anaconda3/lib/python3.6/site-packages/keras/layers/recurrent.py in __call__(self, inputs, initial_state, **kwargs) 
    250    else: 
    251     kwargs['initial_state'] = initial_state 
--> 252   return super(Recurrent, self).__call__(inputs, **kwargs) 
    253 
    254  def call(self, inputs, mask=None, initial_state=None, training=None 

): 

/home/manish/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs) 
    509     # Raise exceptions in case the input is not compatible 
    510     # with the input_spec specified in the layer constructor. 
--> 511     self.assert_input_compatibility(inputs) 
    512 
    513     # Collect input shapes to build layer. 

home/manish/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py in assert_input_compatibility(self, inputs) 
    411          self.name + ': expected ndim=' + 
    412          str(spec.ndim) + ', found ndim=' + 
--> 413          str(K.ndim(x))) 
    414    if spec.max_ndim is not None: 
    415     ndim = K.ndim(x) 

ValueError: Input 0 is incompatible with layer simple_rnn_8: expected ndim=3, found ndim=2 

Ich habe nur ein paar Veränderungen gemacht aber ich habe diesen Fehler

+0

Versuchen Sie 'input_shape = (X.shape [1],)'. Veröffentlichen Sie auch die vollständige Stack-Ablaufverfolgung des Fehlers. –

+0

Dieser Fehler hängt irgendwie mit dem internen Zustand von RNN zusammen. Die [Dokumentation] (https://keras.io/layers/recurrent/) besagt, dass input_shape ein '3D Tensor mit Shape (Batch_size, timesteps, input_dim)' sein sollte. Sie können die 'batch_size' weglassen und 'none' für' timesteps' verwenden. In diesem Fall habe ich 'input_shape = (None, X.shape [1],)' –

Antwort

1

Wenn Sie input_shape = (1,X.shape[1]) angeben, das Modell erwartet eine Eingabe der Dimensionen [n_samples, 1, 1048576]. Das sind 3 Dimensionen. Aber Ihre tatsächlichen Daten haben nur 2 Dimensionen. Sie sollten also die 1 aus der input_shape entfernen.

Versuchen input_shape = (X.shape[1],)

Blick auf die documentation für sie zu verstehen.

+0

geändert. Ich habe die Eingabeform wie erwähnt verändert und es hat funktioniert. Soll ich noch den vollen Stack-Trace-Fehler posten? – BATspock

+0

@BATspock Gut. Noch eine Sache, ich bin immer noch im Zweifel über die Angabe von y.shape [1] 'in Ihrer zweiten dichten Schicht. Erhalten Sie mit diesem Setup gute Ergebnisse? Die Dokumentation und Beispiele, die ich gesehen habe, sagten, dass die Ausgabeform automatisch abgeleitet wird. Verfolgen Sie ein Tutorial für Ihren Code? Wenn ja, bitte posten Sie es auch. –

+0

Bitte werfen Sie einen Blick auf meine zweite Bearbeitung. Ihre Lösung konnte hier nicht richtig funktionieren – BATspock