Example 1
From numpy import arrayfrom keras.preprocessing.text import one_hotfrom keras.preprocessing.sequence import Pad_ Sequencesfrom keras.models Import sequentialfrom keras.layers import densefrom keras.layers import Flattenfrom Keras.layers.embeddings Import embedding# Define documentsdocs = [' Well done! ', ' good work ', ' great effort ', ' Nice work ', ' excellent! ', ' Weak ', ' Poor effort! ', ' no good ', ' Poor work ', ' Could has done better. ' # define Class Labelslabels = Array ([1,1,1,1,1,0,0,0,0,0]) # integer encode the documentsvocab_size = 50encoded_docs = [one _hot (d, vocab_size) for D in Docs]print (encoded_docs) # Pad documents to a max length of 4 wordsmax_length = 4padded_docs = Pad_sequences (Encoded_docs, maxlen=max_length, padding= ' post ') print (Padded_docs) # define the Modelmodel = Sequential ( ) Model.add (Embedding (vocab_size, 8, input_length=max_length)) Model.add (Flatten ()) Model.add (dense (1, activation= ') Sigmoid ') # Compile the modelModel.compile (optimizer= ' Adam ', loss= ' binary_crossentropy ', metrics=[' acc ']) # summarize the Modelprint ( Model.summary ()) # Fit the Modelmodel.fit (padded_docs, labels, epochs=50, verbose=0) # Evaluate the modelloss, accuracy = mo Del.evaluate (Padded_docs, labels, verbose=0) print (' accuracy:%f '% (accuracy*100))
Example 2
#-*-Coding:utf-8-*-from keras.layers.core import Activation, densefrom keras.layers.embeddings import embeddingfrom ke Ras.layers.recurrent Import lstmfrom keras.models import sequentialfrom keras.preprocessing import Sequencefrom Sklearn.model_selection Import train_test_splitimport collectionsimport nltknltk.download (' Punkt ') import numpy as np# # EDA MaxLen = 0word_freqs = Collections. Counter () Num_recs = 0with open (' train_data.txt ', ' r+ ') as F:for line in F:label, sentence = Line.strip (). Split ( "\ t") words = Nltk.word_tokenize (Sentence.lower ()) If Len (words) > Maxlen:maxlen = Len (words) For word in Words:word_freqs[word] + = 1 Num_recs + = 1print (' Max_len ', maxlen) print (' Nb_words ', Len (WORD_FREQS) # # Preparation Data Max_features = 2000max_sentence_length = 40vocab_size = Min (max_features, Len (word_freqs)) + 2wor D2index = {X[0]: i+2 for I, X in Enumerate (Word_freqs.most_common (max_features))}word2index["PAD"] = 0word2index["UNK"] = 1index2word = {v:k for k, V in word2index.items ()}x = Np.empty (num_recs,dtype=list) y = Np.zeros (num_recs) I=0with open (' tr Ain_data.txt ', ' r+ ') as F:for line in F:label, sentence = Line.strip (). Split ("\ t") words = Nltk.word_tok Enize (Sentence.lower ()) Seqs = [] for word in words:if word in WORD2INDEX:SEQS.A Ppend (Word2index[word]) else:seqs.append (word2index["UNK"]) x[i] = seqs y[i] = in T (label) i + = 1X = Sequence.pad_sequences (X, Maxlen=max_sentence_length) # # Data Divide xtrain, Xtest, ytrain, ytest = Train _test_split (X, y, test_size=0.2, random_state=42) # # network Build embedding_size = 128hidden_layer_size = 64batch_size = 32NUM_ epochs = 10model = Sequential () Model.add (Embedding (Vocab_size, embedding_size,input_length=max_sentence_length)) Model.add (LSTM (hidden_layer_size, dropout=0.2, recurrent_dropout=0.2)) Model.add (dense (1)) Model.add (Activation (" Sigmoid ") Model.compile (loss=" Binary_crossentropy "), optimizer= "Adam", metrics=["Accuracy") # # Network Training Model.fit (Xtrain, Ytrain, Batch_size=batch_size, Epochs=num_epochs, Validation_data= (Xtest, Ytest)) # # predictive score, acc = model.evaluate (Xtest, Ytest, batch_size=batch_size) print ("\ntest Score:%.3f, accuracy:%.3f "% (score, ACC)) print (' {} {} {}} '. Format (' prediction ', ' real ', ' sentence ')) for I in range (5): idx = NP. Random.randint (Len (Xtest)) Xtest = Xtest[idx].reshape (1,40) Ylabel = Ytest[idx] WHK =model.predict (Xtest) prin T WHK ypred = model.predict (xtest) [0][0] Sent = "". Join ([index2word[x] for x in xtest[0] if x! = 0]) print (' {} {} {} '. Format (int (round (ypred)), int (ylabel), sent)) ##### self-input input_sentences = [' I love reading. ', ' the ' so Bo Ring. '] XX = Np.empty (len (input_sentences), dtype=list) i=0for sentence in input_sentences:words = nltk.word_tokenize (sentence. Lower ()) seq = [] for word in Words:if word in Word2index:seq.append (Word2index[word]) el Se:seq.append (word2index[' UNK ')) xx[i] = seq i+=1xx = sequence.pad_sequences (XX, maxlen=max_sentence_length) labels = [Int (round (x[0])) fo R x in Model.predict (XX)]label2word = {1: ' active ', 0: ' Negative '}for I in range (len (input_sentences)): Print (' {} {} '. Format (Lab El2word[labels[i]], input_sentences[i]) WHK = model.predict (XX) Print WHK
The content in the
txt is roughly the following
1 The Da Vinci Code book are just awesome.1 this were the first Clive Cussler I ' ve ever read, but even books lik e Relic, and Da Vinci code were more plausible than this.1 I liked the DA Vinci code a lot.1 I liked the DA Vi NCI code a Lot.1 I liked the Da Vinci Code but it ultimatly didn ' t seem to hold it's own.1 that's not even an exaggeration) and at midnight we went to Wal-mart to buy the Da Vinci Code, which is amazing of course.1 I loved th E Da Vinci Code, but now I want something better and different!.. 1 I thought da Vinci Code was great, same with Kite Runner.1 the Da Vinci Code is actually a good movie ... 1 I thought the DA Vinci code was a pretty good book.1 the DA Vinci Code are one of the most beautiful movies I ve ever seen.1 the Da Vinci Code is a * amazing * book, does not get me wrong.1 then I turn on the light and th E radio and enjoy my da Vinci code.1 the da Vinci Code was really Good.1I love da Vinci Code .... 1 I loved da Vinci Code. 1 to night:: The DA VINCI CODE and A BEAUTIFUL mind ... 1 The DA VINCI CODE is a AWESOME book .... 1 Thing is, I enjoyed the da Vinci code.1 very da Vinci Code slash Amazing Race.
Example 3, this example is a time series prediction
Import Numpyimport Matplotlib.pyplot as Pltfrom pandas import read_csvimport mathfrom keras.models import sequentialfrom k Eras.layers Import densefrom keras.layers import lstmfrom sklearn.preprocessing import Minmaxscalerfrom sklearn.metrics Import Mean_squared_errordef Create_dataset (DataSet, Look_back=1): datax, Datay = [], [] for I in range (Len (DataSet) -LOOK_BACK-1): a = Dataset[i: (I+look_back), 0] Datax.append (a) datay.append (dataset[i + look_back, 0] ) return Numpy.array (datax), Numpy.array (datay) dataframe = read_csv (' international-airline-passengers.csv ', usecols= [1], engine= ' python ', skipfooter=3) DataSet = Dataframe.valuesdataset = Dataset.astype (' float32 ') train_size = Int (len ( DataSet) * 0.67) Print Datasetscaler = Minmaxscaler (feature_range= (0, 1)) DataSet = Scaler.fit_transform (DataSet) Test_ size = Len (DataSet)-train_sizetrain, test = dataset[0:train_size,:], Dataset[train_size:len (DataSet),:]# reshape into X =t and Y=t+1look_back = 1trainX, Trainy = Create_dataset (Train, Look_back) testx, testy = Create_dataset (test, look_back) Trainx = Numpy.reshape (TrainX, ( Trainx.shape[0], 1, trainx.shape[1]) Testx = Numpy.reshape (Testx, (Testx.shape[0], 1, testx.shape[1])) model = Sequential () Model.add (LSTM (4, input_shape= (1, Look_back))) Model.add (dense (1)) Model.compile (loss= ' mean_squared_ Error ', optimizer= ' Adam ') Model.fit (Trainx, Trainy, epochs=100, batch_size=1, verbose=2) testpredict = Model.predict ( TESTX) testpredict = Scaler.inverse_transform (testpredict) WHK=[[112],[390],[622]]WHK = Scaler.fit_transform (WHK) Testone=numpy.reshape (WHK, (3, 1, 1)) Print testonetestpredict = Model.predict (testone) testpredict = Scaler.inverse_ Transform (testpredict) Print testpredict
The content of International-airline-passengers.csv is less, roughly as follows
"Month","International airline passengers: monthly totals in thousands. Jan 49 ? Dec 60""1949-01",112"1949-02",118"1949-03",132"1949-04",129"1949-05",121"1949-06",135"1949-07",148"1949-08",148"1949-09",136"1949-10",119"1949-11",104"1949-12",118"1950-01",115"1950-02",126"1950-03",141"1950-04",135"1950-05",125"1950-06",149"1950-07",170"1950-08",170"1950-09",158"1950-10",133"1950-11",114"1950-12",140"1951-01",145"1951-02",150"1951-03",178"1951-04",163"1951-05",172"1951-06",178
An example of keras sentiment analysis