python 3.x - ValueError: setting an array element with a sequence in keras using nltk ngram -
from keras.utils import np_utils keras.models import sequential keras.layers import dense keras.layers.recurrent import simplernn sklearn.feature_extraction.text import hashingvectorizer sklearn.feature_extraction.text import countvectorizer sklearn.preprocessing import labelencoder import numpy np text = open('eng.train').read().split() words = [] tags_1 = [] tags_2 = [] in range(len(text)): if % 4 == 0: words.append(text[i]) if % 4 == 1: tags_1.append(text[i]) if % 4 == 3: tags_2.append(text[i]) hashing_vectorizer = hashingvectorizer(decode_error = 'ignore', n_features = 2 **15) x_v = hashing_vectorizer.fit_transform(words) label_encoder = labelencoder() y1 = label_encoder.fit_transform(tags_1) y2 = label_encoder.fit_transform(tags_2) y1 = np_utils.to_categorical(y1) y2 = np_utils.to_categorical(y2) import nltk trigram_x = list(nltk.trigrams(x_v)) #trigram_x = list(trigram_x) print(len(trigram_x)) x = numpy.array(trigram_x) print(x.shape) y = numpy.reshape(y1, (204567, 1, 46)) trigram_tags = list(nltk.trigrams(y)) #trigram_y = list(trigram_tags) print (len(trigram_y)) target = numpy.array(trigram_y) y = numpy.reshape(target, (204565, 3, 46)) x = numpy.reshape(x, (204565, 3, 1)) x_final = numpy.dstack((x, y)) print(x_final.shape) x_input = x_final[: -1, :, :] print(x_input.shape) y_final = label_encoder.fit_transform(tags_1) y_target = np_utils.to_categorical(y_final[3:]) print(y_target.shape) keras.layers import dense keras.models import sequential keras.layers.recurrent import simplernn
feature hashig used here. problem requires give generated hashed vector corresponding 1 hot encoded vector input. keras program throwing following error:
model = sequential() model.add(simplernn(100,input_shape = (x_input.shape[1], x_input.shape[2]))) model.add(dense(y_target.shape[1], activation = 'softmax')) model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy']) model.fit(x_input, y_target, epochs = 20, batch_size = 200) valueerror: setting array element sequence.
please explain reason error , possible solution
edit 1
i have attached full error stack below
valueerror traceback (most recent call last) <ipython-input-3-4d9a4c1d9885> in <module>() 62 model.add(dense(y_target.shape[1], activation = 'softmax')) 63 model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy']) ---> 64 model.fit(x_input, y_target, epochs = 20, batch_size = 200) 65 /home/aditya/anaconda3/lib/python3.6/site-packages/keras/models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs) 843 class_weight=class_weight, 844 sample_weight=sample_weight, --> 845 initial_epoch=initial_epoch) 846 847 def evaluate(self, x, y, batch_size=32, verbose=1, /home/aditya/anaconda3/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs) 1483 val_f=val_f, val_ins=val_ins, shuffle=shuffle, 1484 callback_metrics=callback_metrics, -> 1485 initial_epoch=initial_epoch) 1486 1487 def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=none): /home/aditya/anaconda3/lib/python3.6/site-packages/keras/engine/training.py in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch) 1138 batch_logs['size'] = len(batch_ids) 1139 callbacks.on_batch_begin(batch_index, batch_logs) -> 1140 outs = f(ins_batch) 1141 if not isinstance(outs, list): 1142 outs = [outs] /home/aditya/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs) 2071 session = get_session() 2072 updated = session.run(self.outputs + [self.updates_op], -> 2073 feed_dict=feed_dict) 2074 return updated[:len(self.outputs)] 2075 /home/aditya/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 787 try: 788 result = self._run(none, fetches, feed_dict, options_ptr, --> 789 run_metadata_ptr) 790 if run_metadata: 791 proto_data = tf_session.tf_getbuffer(run_metadata_ptr) /home/aditya/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 966 feed_handles[subfeed_name] = subfeed_val 967 else: --> 968 np_val = np.asarray(subfeed_val, dtype=subfeed_dtype) 969 970 if (not is_tensor_handle_feed , /home/aditya/anaconda3/lib/python3.6/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 array element sequence
Comments
Post a Comment