Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Code to question on StackOverflow
- # https://stackoverflow.com/questions/51421885/expected-dense-to-have-shape-but-got-array-with-shape/51432523#51432523
- #
- data = ''' query tags
- hi intro
- how are you wellb
- hello intro
- what's up wellb
- how's life wellb
- bye gb
- see you later gb
- good bye gb
- thanks gratitude
- thank you gratitude
- that's helpful gratitude
- I am great revertfine
- fine revertfine
- I am fine revertfine
- good revertfine'''
- import pandas as pd
- from io import StringIO
- import numpy as np
- data = pd.read_table(StringIO(data), sep='\s{2,}')
- print(df.columns)
- print(df)
- #-------------------------------------------------------
- from keras.preprocessing.text import Tokenizer
- from sklearn.preprocessing import LabelBinarizer
- from keras.models import Sequential
- import pandas as pd
- from keras.layers import Dense, Activation
- #data = pd.read_csv('text_class.csv')
- train_text = data['query']
- train_labels = data['tags']
- tokenize = Tokenizer(num_words=100)
- tokenize.fit_on_texts(train_text)
- x_data = tokenize.texts_to_matrix(train_text)
- encoder = LabelBinarizer()
- encoder.fit(train_labels)
- y_data = encoder.transform(train_labels)
- model = Sequential()
- model.add(Dense(512, input_shape=(100,)))
- model.add(Activation('relu'))
- model.add(Dense(5))
- model.add(Activation('softmax'))
- model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
- model.fit(x_data, y_data, batch_size=8, epochs=10)
- print('x_data :', type(x_data), x_data.shape)
- print('x_data[0] :', type(x_data[0]), x_data[0].shape)
- print('x_data[0:1]:', type(x_data[0:1]), x_data[0:1].shape)
- predictions = model.predict(x_data[0:1]) # <-- need x_data[0:1] instead of x_data[0]
- tag_labels = encoder.classes_
- predicted_tags = tag_labels[np.argmax(predictions)]
- print (predicted_tags)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement