Sunday 22 September 2019

neural network 7

code linkhttps://github.com/chuanshuoge6/machineLearning-movieReview

#pycharm
#save model algorithm, so that don't have to create model everytime for a new prediction. Just load the model saved.

model.save('model.h5')

#add a txt file for testing

----------------------------
#test.py

import tensorflow as td
from tensorflow import keras
import numpy as np

data = keras.datasets.imdb

#word dictionary, format [{word, digit}...]
word_index = data.get_word_index()

def review_encode(s):
    #1 means 'start line'
    encoded = [1]

    for word in s:
         #translate human words to machine digits
        if word.lower() in word_index:
            encoded.append(word_index[word.lower()])
        else:
            #2 means 'word not found in dictionary'
            encoded.append(2)

    return encoded

#load model generated from machine training
model = keras.models.load_model('model.h5')

#assume review is either negative or positive
class_names = ['negative', 'positive']


with open('test.txt', encoding='utf-8') as f:

    for line in f.readlines():
        #dictionary does not contain symbols and punctuation, only pure english words
        nline = line.replace(',','').replace('(','').replace(')','').replace(':','').replace("\"",'').strip().split(' ')
        encode = review_encode(nline)
        #comply with the data format the model is trained in, 256 words a paragraphy, fill and trim to keep data shape
        encode = keras.preprocessing.sequence.pad_sequences([encode], value=0, padding='post', maxlen=256)
        predict = model.predict(encode)
        print(line)
        print(encode)
        #predict function input and output are lists, since input is a single paragraph, output[0] is the prediction. A prediction output is a list of numbers, this model only outputs 1 number - predict[0][0], value is between 0 and 1.
        print(predict[0], class_names[int(round(predict[0][0]))])

-------------------------------
#logs

If you're in desperate need of 90 minutes of gore-filled violence, Rambo: Last Blood will fill the order. Beyond that, there's nothing of value to be found here.

[[   1   45  332    8 1677  356    4 1549  231    4    2  564 3953  233
   538   77 2230    1    2  721   12  222  161    4 1104    5   27  255
     2    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0]]
[0.34005865] negative
While Sylvester Stallone made the word Rambo synonymous with tough guy action in the 1980s, it's been a long time since he first made the character famous. In fact, it's been over a decade since the last Rambo movie, which itself came two decades before its predecessor. Culture, attitudes, and movies themselves have all changed a lot in that time. I'm not sure anybody was dying for another Rambo movie in 2019, and after having seen Rambo: Last Blood I'm still not sure exactly who this movie is for.

[[    1   134 13509  5808    90     1   678  3953 17261    16  1208   229
    203     8     1  3483    42    74     3   193    55   234    26    83
     90     1   106     2     8   189    42    74   117     3  2065   234
      1   233  3953    17    60   407   382   104  2737   156    91     2
   1178  4639     2    99   530    25    29  1191     3   173     8    12
      2   143    21   249  1811    13  1718    15   157  3953    17     8
  43837     2   100   257   107  3953   233   538   143   128    21   249
    615    34    11    17     6     2     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0]]
[0.13580143] negative
It's been 10 years since the events of Rambo, at the end of which, Vietnam War veteran John Rambo (Sylvester Stallone) returned to his family's home in Arizona. We learn as Rambo: Last Blood opens that while on the family ranch, he met Maria (Adriana Barraza) and her granddaughter Gabrielle (Yvette Monreal).

[[    1    42    74   155   150   234     1   684     4  3953    30     1
    127     4    60  2568   322  2493   305  3953 13509  5808  3779     5
     24  4715   341     8     2    72   847    14  3953   233   538  2010
     12   134    20     1   220  7030    26  1833  2904 28966     2     2
     38 12498 16931 21532     2     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0]]
[0.10586126] negative
The three of them have lived together on the Rambo ranch where John trains horses and he's become a surrogate father to Gabrielle. Rambo appears mostly at peace, though John's constructed a series of tunnels beneath the property where he keeps a pretty significant arsenal, because he is Rambo, after all.
[[    1     1   286     4    95    25  1449   292    20     1  3953  7030
    118   305  6897  3233     2   237   410     3  9817   333     5     2
   3953   736   666    30  2467   148  9279  4478     3   198     4 14157
   4155     1  4859   118    26   938     3   181  2678 19286    85    26
      6  3953   100     2     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0]]
[0.36746112] negative

-----------------------------------------------------
#copy and paste into test.txt positive reviews, run again
#logs

With its energy, its creativity, its raw passion and its fun, this film and its newcomer cast are the best thing I’ve seen at this year’s Toronto film festival.

[[    1    16    91  1705    91  4855    91  2818  1794     2    91   250
     11    19     2    91  6947   174    23     1   115   152 32871   107
     30    11     2  5879    19     2     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0]]
[0.71682703] positive
It’s a social-realist adventure written by Theresa Ikoko and Claire Wilson and directed by Sarah Gavron about a multi-ethnic community in East London - in the spirit of Ken Loach’s Kes or Céline Sciamma’s Bande Des Filles. It’s tough, but it’s the opposite of miserablist. At the story’s centre is a group of year 11 girls and the star is Bukky Bakray, playing a Nigerian British girl nicknamed “Rocks”, who is maybe no great academic high-flier but really talented at cosmetics. Her dad is dead and she lives with her troubled mum, who has had, as a social worker delicately puts it, issues managing her medication.

[[    1 21380     3     2  1151   395    31 12453     2     2  2891  2610
      2   523    31  2692     2    41     3     2  1827     8  2921  1313
      2     8     1  1100     4  3659     2 35070    39 38785     2     2
   6169     2 21380  1208    18 21380     1  1958     4     2    30     1
  57724  5923     6     3   601     4   288  1499   536     2     1   320
      6     2     2   392     3     2   695   247 21359     2    34     6
    276    54    84  9094     2    18    63  1017    30     2    38  1243
      6   348     2    56   453    16    38  3203  6997    34    44    66
     14     3  1028  3501 17247  1454     9  1338  8245    38     2     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0]]
[0.69423324] positive
It is Rocks who largely has the responsibility of minding her kid brother, Emmanuel, gloriously played by D’Angelou Osei Kissiedu – and Emmanuel is a black-belt scene-stealer. He starts the film the way he means to go on, with a hilarious setpiece. Asking if he may say grace before dinner, Emmanuel launches into his own version of the Lord’s Prayer: “Our father – he’s up in heaven.” Rocks cheerfully calls that his “remix”. But there’s real trouble when Rocks’s mum absents herself from the family home.

[[    1     9     6  3336    34  2253    44     1  4753     4 19316    38
    551   594 33274 12546   253    31     2     2     2  5047     2 33274
      6     3     2     2    26   514     1    19     1    93    26   814
      5   137    20    16     3   639     2  2251    45    26   200   132
   1695   156  3065 33274 20969    80    24   202   307     4     1     2
  11467     2   333  5047 36347    53     8     2  3336 17321  2015    12
     24     2    18 35733   144  1110    51     2  6997     2   762    36
      1   220     2     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0]]
[0.7644717] positive
The odyssey of Rocks and Emmanuel – who in effect go on the run, fugitives from the world of grownup authority – provides a motor that drives the film but, in a way, its best moments come when Rocks and her friends are doing nothing more dramatically significant than just hanging out, talking and laughing. Occasionally these scenes erupt into something defiant, as when a food fight monumentally kicks off in the middle of a home economics class or when a girl tells her grumpy teacher: “You’re like this because you’ve got your period, sir.” (He furiously replies: “That’s actually really offensive.”)

[[    1     1  6758     4  3336     2 33274  5047    34     8   959   137
     20     1   518 32018    36     1   179     4 79917  4262  5047  1565
      3 11035    12  3041     1    19    18     8     3    93    91   115
    385   213    51  3336     2    38   366    23   396   161    50  6728
   2678    71    40  2345    43   659     2     2  2033   131   136 21687
     80   139 16633    14    51     3  1641   545 22736  3412   122     8
      1   652     4     3   341 21005   704    39    51     3   247   713
     38  9739  1747     2    37    11    85     2   185   126   807     2
     26 33322  7228     2   162    63     2     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0]]
[0.64768946] positive
But really, the improv-type dialogue doesn’t need to go anywhere or do anything to be hugely entertaining and watchable. The group around Rocks are capable, in the best possible way, of laughing about nothing, laughing from sheer directionless joy. The sadness, when it comes, is piercing – yet so is Rocks’s resilience and her philosophical acceptance. Bukky Bakray gives a very moving portrayal of someone who has boldly accepted maternal responsibility for Emmanuel at the very moment that she is to be deprived of it.

[[    1    18    63     1     2   411 40881   356     5   137  1761    39
     78   230     5    27  6121   438     2     2     1   601   184  3336
     23  2247     8     1   115   611    93     4  1101    41   161  1101
     36  2096 26625     2     1  3904    51     9   263     6 12315  5047
    243    35     6     2 26408     2    38  4342     2     2     2   405
      3    52   725  1143     4   291    34    44 15145  3208 32086  4753
     15 33274    30     1    52   558    12    56     6     5    27 14224
      4     2     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0]]
[0.63985294] positive
Of course, there is a larger sadness here: the realisation that the sheer energy and dynamism of this group is likely to be dissipated and wasted when they leave school – society will probably not find a way to tap this resource. When the class is taught about Picasso and cubism and they make spoof Picasso cut-out images of people’s faces cut from magazines, it is a funny moment, but serious too, because there is a real sense of potential. This film is such a rush of vitality. It rocks.
[[    1     4   262    47     6     3  3220  3904   130     1 17640    12
      1  2096  1705     2 35071     4    11   601     6  1326     5    27
  31510     2  1050    51    33   560   393  5047   923    77   239    21
    166     3    93     5  3929    11     2    51     1   704     6  4456
     41 21141     2     2     2    33    94  2835 21141     2  1215     4
      2  1587   602    36  8257     9     6     3   160   558    18   619
     96    85    47     6     3   144   278     4     2    11    19     6
    138     3  3406     4     2     9     2     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0     0     0     0
      0     0     0     0]]
[0.6369015] positive

----------------------------------
reference:
http://chuanshuoge2.blogspot.com/2019/09/neural-network-6.html
https://www.youtube.com/watch?v=Xmga_snTFBs&list=PLzMcBGfZo4-lak7tiFDec5_ZMItiIIfmj&index=8

No comments:

Post a Comment