2017-07-18 31 views
0

我做出來的核心learning.I都創建一個大小3的n_gram模型我用sklearn的HashingVectorizer這個purpose.Then我不得不使用keras建立神經網絡。但是我不知道怎麼養活輸入形狀n_gram模型HashingVectorizer,並使用它與keras

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) 

我的第二個編輯: 這裏幾乎每一點是相同的還是它拋出錯誤。 的代碼是:

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) 

它拋出的錯誤如下:

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 

我僅取得了一些調整但如果指定input_shape = (1,X.shape[1]),模型我得到這個錯誤

+0

嘗試'input_shape =(X.shape [1],)'。還發布完整的堆棧跟蹤錯誤。 –

+0

此錯誤某種程度上與RNN的內部狀態。 [文檔](https://keras.io/layers/recurrent/)指出input_shape應該是一個'三維張量與形狀(的batch_size,時間步長,input_dim)'。您可以省略'batch_size'並將'None'用於'timesteps'。所以在這種情況下,'嘗試input_shape =(無,X.shape [1])' –

回答

1

預計有尺寸輸入[n_samples, 1, 1048576]。這是3個維度。但是你的實際數據只有2個維度。所以你應該從input_shape中刪除1

嘗試input_shape = (X.shape[1],)

看那documentation理解它。

+0

正如你所說,我改變了輸入的形狀和它worked.Should我還是發佈完整的堆棧跟蹤誤差? – BATspock

+0

@Bspspock好。還有一件事,我仍然懷疑在第二個密集層中指定「y.shape [1]」。你用這個設置獲得了好的結果嗎?我見過的文檔和例子說,輸出形狀將被自動推斷出來。你是否正在遵循你的代碼的任何教程?如果是,請張貼。 –

+0

請看看我的第二個編輯。您的解決方案無法正常工作,這裏 – BATspock