Assignment 10 2
Assignment 10 2
2
In [1]:
from keras.preprocessing.text import Tokenizer
import numpy as np
import os
import time
start_time = time.time()
In [8]:
results_dir = Path('results').joinpath('model_1')
results_dir.mkdir(parents=True, exist_ok=True)
imdb_dir = Path('/home/desktop/dsc650/data/external/imdb/aclImdb/')
test_dir = os.path.join(imdb_dir, 'test')
In [9]:
training_samples = 200
max_words = 1000 # Considers only the top 1000 words in the dataset
embedding_dim = 100
In [10]:
labels = []
texts = []
if fname[-4:] == '.txt':
texts.append(f.read())
f.close()
if label_type == 'neg':
labels.append(0)
else:
labels.append(1)
In [11]:
tokenizer = Tokenizer(num_words=max_words)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)
word_index = tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))
labels = np.asarray(labels)
In [12]:
indices = np.arange(data.shape[0])
np.random.shuffle(indices)
data = data[indices]
labels = labels[indices]
x_train = data[:training_samples]
y_train = labels[:training_samples]
In [13]:
# from page 191 Listing 6.12
model = Sequential()
model.add(Flatten())
model.add(Dense(32,activation='relu'))
model.add(Dense(1, activation='sigmoid'))
In [14]:
# Save the summary to file
summary_file = results_dir.joinpath('Assignment_10.2_ModelSummary.txt')
with redirect_stdout(f):
model.summary()
model.save_weights(result_model_file)
Epoch 1/10
Epoch 4/10
Epoch 6/10
Epoch 7/10
Epoch 9/10
In [15]:
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js
# Plots
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.legend()
plt.figure()
plt.legend()
plt.savefig(img_file)
plt.show()
In [16]:
labels=[]
texts=[]
if fname[-4:] == '.txt':
texts.append(f.read())
f.close()
if label_type == 'neg':
labels.append(0)
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js
else:
labels.append(1)
In [17]:
sequence = tokenizer.texts_to_sequences(texts)
y_test = np.asarray(labels)
model.load_weights(result_model_file)
print("")
print(eval)
[0.750030517578125, 0.5329200029373169]
In [ ]:
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js