форкнуто от main/is_dnn
Родитель
d6c7ed670f
Сommit
2eacec555a
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
@ -0,0 +1,150 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""LR4
|
||||
|
||||
Automatically generated by Colab.
|
||||
|
||||
Original file is located at
|
||||
https://colab.research.google.com/drive/1yCiW4PaAoMLkJgUu05TvwmSYoqVG1iyL
|
||||
"""
|
||||
|
||||
import tensorflow as tf
|
||||
device_name = tf.test.gpu_device_name()
|
||||
if device_name != '/device:GPU:0':
|
||||
raise SystemError('GPU device not found')
|
||||
print('Found GPU at: {}'.format(device_name))
|
||||
|
||||
from google.colab import drive
|
||||
drive.mount('/content/drive')
|
||||
import os
|
||||
os.chdir('/content/drive/MyDrive/Colab Notebooks/is_lab4')
|
||||
|
||||
from tensorflow import keras
|
||||
from tensorflow.keras import layers
|
||||
from tensorflow.keras.models import Sequential
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from sklearn.metrics import classification_report, confusion_matrix
|
||||
from sklearn.metrics import ConfusionMatrixDisplay
|
||||
|
||||
# загрузка датасета
|
||||
from keras.datasets import imdb
|
||||
vocabulary_size = 5000
|
||||
index_from = 3
|
||||
(X_train, y_train), (X_test, y_test) = imdb.load_data(path="imdb.npz",
|
||||
num_words=vocabulary_size,
|
||||
skip_top=0,
|
||||
maxlen=None,
|
||||
seed=7,
|
||||
start_char=1,
|
||||
oov_char=2,
|
||||
index_from=index_from
|
||||
)
|
||||
print('Shape of X train:', X_train.shape)
|
||||
print('Shape of y train:', y_train.shape)
|
||||
print('Shape of X test:', X_test.shape)
|
||||
print('Shape of y test:', y_test.shape)
|
||||
|
||||
# создание словаря для перевода индексов в слова
|
||||
# заргузка словаря "слово:индекс"
|
||||
word_to_id = imdb.get_word_index()
|
||||
# уточнение словаря
|
||||
word_to_id = {key:(value + index_from) for key,value in word_to_id.items()}
|
||||
word_to_id["<PAD>"] = 0
|
||||
word_to_id["<START>"] = 1
|
||||
word_to_id["<UNK>"] = 2
|
||||
word_to_id["<UNUSED>"] = 3
|
||||
# создание обратного словаря "индекс:слово"
|
||||
id_to_word = {value:key for key,value in word_to_id.items()}
|
||||
|
||||
#Вывод отзыва из обучающего множества в виде списка индексов слов
|
||||
some_number = 192
|
||||
review_indices = X_train[some_number]
|
||||
print("Список индексов слов:")
|
||||
print(review_indices)
|
||||
|
||||
#Преобразование списка индексов в текст
|
||||
review_as_text = ' '.join(id_to_word[id] for id in X_train[some_number])
|
||||
print("\nОтзыв в виде текста:")
|
||||
print(review_as_text)
|
||||
|
||||
#Вывод длины отзыва
|
||||
max_review_length = len(max(X_train, key=len))
|
||||
print(f"\nМаксимальная длина отзыва: {max_review_length}")
|
||||
min_review_length = len(min(X_train, key=len))
|
||||
print(f"\nМинимальная длина отзыва: {min_review_length}")
|
||||
review_length = len(review_indices)
|
||||
print(f"\nДлина отзыва: {review_length}")
|
||||
|
||||
#Вывод метки и названия класса
|
||||
class_label = y_train[some_number]
|
||||
class_name = "Positive" if class_label == 1 else "Negative"
|
||||
print(f"\nМетка класса: {class_label} - {class_name}")
|
||||
|
||||
# предобработка данных
|
||||
from tensorflow.keras.utils import pad_sequences
|
||||
max_words = 500
|
||||
X_train = pad_sequences(X_train, maxlen=max_words, value=0, padding='pre', truncating='post')
|
||||
X_test = pad_sequences(X_test, maxlen=max_words, value=0, padding='pre', truncating='post')
|
||||
|
||||
#Вывод отзыва из обучающего множества в виде списка индексов слов
|
||||
some_number = 192
|
||||
review_indices = X_train[some_number]
|
||||
print("Список индексов слов:")
|
||||
print(review_indices)
|
||||
|
||||
#Преобразование списка индексов в текст
|
||||
review_as_text = ' '.join(id_to_word[id] for id in X_train[some_number])
|
||||
print("\nОтзыв в виде текста:")
|
||||
print(review_as_text)
|
||||
|
||||
#Вывод длины отзыва
|
||||
max_review_length = len(max(X_train, key=len))
|
||||
print(f"\nМаксимальная длина отзыва: {max_review_length}")
|
||||
min_review_length = len(min(X_train, key=len))
|
||||
print(f"\nМинимальная длина отзыва: {min_review_length}")
|
||||
review_length = len(review_indices)
|
||||
print(f"\nДлина отзыва: {review_length}")
|
||||
|
||||
# вывод данных
|
||||
print('X train: \n',X_train)
|
||||
print('X train: \n',X_test)
|
||||
|
||||
# вывод размерностей
|
||||
print('Shape of X train:', X_train.shape)
|
||||
print('Shape of X test:', X_test.shape)
|
||||
|
||||
model = Sequential()
|
||||
model.add(layers.Embedding(input_dim=vocabulary_size, output_dim=32, input_length=max_words, input_shape=(max_words,)))
|
||||
model.add(layers.LSTM(76))
|
||||
model.add(layers.Dropout(0.3))
|
||||
model.add(layers.Dense(1, activation='sigmoid'))
|
||||
|
||||
model.summary()
|
||||
|
||||
batch_size = 64
|
||||
epochs = 5
|
||||
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
|
||||
model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.2)
|
||||
|
||||
test_loss, test_acc = model.evaluate(X_test, y_test)
|
||||
print(f"\nTest accuracy: {test_acc}")
|
||||
print(f"\nTest loss: {test_loss}")
|
||||
|
||||
y_score = model.predict(X_test)
|
||||
y_pred = [1 if y_score[i,0]>=0.5 else 0 for i in range(len(y_score))]
|
||||
from sklearn.metrics import classification_report
|
||||
print(classification_report(y_test, y_pred, labels = [0, 1], target_names=['Negative', 'Positive']))
|
||||
|
||||
from sklearn.metrics import roc_curve, auc
|
||||
import matplotlib.pyplot as plt
|
||||
fpr, tpr, thresholds = roc_curve(y_test, y_score)
|
||||
plt.plot(fpr, tpr)
|
||||
plt.grid()
|
||||
plt.xlabel('False Positive Rate')
|
||||
plt.ylabel('True Positive Rate')
|
||||
plt.title('ROC')
|
||||
plt.show()
|
||||
print('Area under ROC is', auc(fpr, tpr))
|
||||
|
||||
from sklearn.metrics import roc_auc_score
|
||||
print('AUC ROC:', roc_auc_score(y_test, y_score))
|
||||
Загрузка…
Ссылка в новой задаче