Aaaaa988

lab4MMO

Dec 8th, 2020
1,131
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import tensorflow as tf
  3. from keras import models
  4. from keras import layers
  5. from keras.datasets import imdb
  6.  
  7. (training_data, training_targets), (testing_data, testing_targets) = imdb.load_data(num_words=10000)
  8. data = np.concatenate((training_data, testing_data), axis=0)
  9. targets = np.concatenate((training_targets, testing_targets), axis=0)
  10.  
  11.  
  12. def vectorize(sequences, dimension=10000):
  13.     results = np.zeros((len(sequences), dimension))
  14.     for i, sequence in enumerate(sequences):
  15.         results[i, sequence] = 1
  16.     return results
  17.  
  18. def toFixed(numObj, digits=0):
  19.     return f"{numObj:.{digits}f}"
  20.  
  21.  
  22. print(data[0])
  23. data = vectorize(data)
  24. targets = np.array(targets).astype("float32")
  25. test_x = data[:10000]
  26. test_y = targets[:10000]
  27. train_x = data[10000:]
  28. train_y = targets[10000:]
  29.  
  30. model = models.Sequential()
  31.  
  32. # Входной слой
  33. model.add(layers.Dropout(0.2, input_shape=(10000,)))
  34. model.add(layers.Dense(50, activation="relu", input_shape=(10000,)))
  35. # Скрытый слой и слои выбивания
  36. model.add(layers.Dropout(0.3, input_shape=(50,)))
  37. model.add(layers.Dense(50, activation="relu"))
  38. # Выходной слой
  39. model.add(layers.Dropout(0.2, input_shape=(50,)))
  40. model.add(layers.Dense(1, activation="sigmoid"))
  41. model.summary()
  42.  
  43. model.compile(
  44.     optimizer="adam",
  45.     loss="binary_crossentropy",
  46.     metrics=["accuracy"]
  47. )
  48. results = model.fit(
  49.     train_x, train_y,
  50.     epochs=10,
  51.     batch_size=500,
  52.     validation_split=0.2
  53. )
  54. score = model.evaluate(test_x, test_y)
  55.  
  56. print("Test Потери: %.2f%%" % (score[0]*100))
  57. print("Test Точность: %.2f%%" % (score[1]*100))
  58.  
  59. predictions = model.predict(test_x)
  60. print("Предсказание -- на самом деле")
  61. for i in range(10):
  62.     print('%.6f -- %f' %(predictions[i], test_y[i]))
  63.  
  64. words = tf.keras.datasets.imdb.get_word_index(path="imdb_word_index.json")
  65. print("Индексы слов согласно IMDB (первые 20шт)")
  66. i = 0
  67. for key, value in words.items():
  68.   print(key,":", value),
  69.   i+=1
  70.   if(i>20): break
  71.  
  72. text = "The Shawshank Redemption is written and directed by Frank Darabont. \n" \
  73.        "It is an adaptation of the Stephen King novella Rita Hayworth and Shawshank Redemption. \n" \
  74.        "Starring Tim Robbins and Morgan Freeman, the film portrays the story of Andy Dufresne (Robbins), \n" \
  75.        "a banker who is sentenced to two life sentences at Shawshank State Prison for apparently murdering his wife \n" \
  76.        "and her lover. Andy finds it tough going but finds solace in the friendship he forms with fellow inmate \n" \
  77.        "Ellis Red Redding (Freeman). While things start to pick up when the warden finds Andy a \n" \
  78.        "prison job more befitting his talents as a banker. However, the arrival of another inmate is going \n" \
  79.        "to vastly change things for all of them."
  80.  
  81. print("\nВходной текст рецензии\n\n", text,"\n\n")
  82.  
  83. print("Секвенция слов из входной рецензии")
  84. from keras.preprocessing.text import text_to_word_sequence
  85. result = text_to_word_sequence(text)
  86. print(result)
  87.  
  88. for i in range(len(result)):
  89.     if(words.get(result[i]) == None or words.get(result[i]) > 10000):
  90.         result[i] = 0
  91.     else:
  92.         result[i] = words.get(result[i])
  93.  
  94. print("Длина секвенции")
  95. print(len(result))
  96. print("Преобразованная секвенция в индексы")
  97. print(result)
  98.  
  99. vector = vectorize(result)
  100.  
  101. newpredict = model.predict(vector)
  102. print('Уверенность нейросети в эмоциональности рецензии - %.6f ' %(newpredict[0]))
  103.  
RAW Paste Data