In [2]:
from keras.datasets import imdb
In [3]:
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
In [4]:
train_data.shape
Out[4]:
In [5]:
train_labels.shape
Out[5]:
In [6]:
test_data.shape
Out[6]:
In [12]:
train_data[0][:10]
Out[12]:
In [13]:
train_labels[0]
Out[13]:
In [16]:
max([max(sequence) for sequence in train_data])
Out[16]:
In [17]:
word_index = imdb.get_word_index()
In [19]:
reverse_word_index = dict(
[(value, key) for (key, value) in word_index.items()])
In [30]:
decoded_review = ' '.join([reverse_word_index.get(i - 3, '?') for i in train_data[0]])
decoded_review
Out[30]:
In [40]:
import numpy as np
def vectorize_sequences(sequences, dimension=10000):
results = np.zeros((len(sequences), dimension))
for i, sequence in enumerate(sequences):
results[i, sequence] = 1.
return results
In [42]:
x_train = vectorize_sequences(train_data)
x_test = vectorize_sequences(test_data)
In [43]:
x_train.shape
Out[43]:
In [44]:
x_test.shape
Out[44]:
In [45]:
x_train[0]
Out[45]:
In [46]:
y_train = np.array(train_labels).astype('float32')
y_test = np.array(test_labels).astype('float32')
In [47]:
y_train
Out[47]:
In [52]:
from keras import models
from keras import layers
model = models.Sequential()
model.add(layers.Dense(16, activation='relu', input_shape=(10000, )))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
In [53]:
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
In [54]:
x_val = x_train[:10000]
partial_x_train = x_train[10000:]
In [55]:
x_val.shape
Out[55]:
In [56]:
partial_x_train.shape
Out[56]:
In [57]:
y_val = y_train[:10000]
partial_y_train = y_train[10000:]
In [59]:
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['acc'])
In [60]:
history = model.fit(partial_x_train,
partial_y_train,
epochs=20,
batch_size=512,
validation_data=(x_val, y_val))
In [61]:
history_dict = history.history
In [62]:
history_dict.keys()
Out[62]:
In [70]:
import matplotlib.pyplot as plt
history_dict = history.history
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
epochs = range(1, len(loss_values) + 1)
plt.plot(epochs, loss_values, 'bo', label='Training loss')
plt.plot(epochs, val_loss_values, 'b', label='Validation loss')
plt.title('Training and validatioin loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
In [71]:
plt.clf()
acc_values = history_dict['acc']
val_acc_values = history_dict['val_acc']
plt.plot(epochs, acc_values, 'bo', label='Training acc')
plt.plot(epochs, val_acc_values, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
In [90]:
model = models.Sequential()
model.add(layers.Dense(2, activation='relu', input_shape=(10000, )))
#model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['acc'])
history = model.fit(partial_x_train,
partial_y_train,
epochs=14,
batch_size=512,
validation_data=(x_val, y_val))
In [91]:
results = model.evaluate(x_test, y_test)
In [92]:
results
Out[92]:
In [93]:
model.predict(x_test)
Out[93]:
In [94]:
from keras.datasets import reuters
(train_data, train_labels), (test_data, test_labels) = reuters.load_data(num_words=10000)
In [96]:
train_data.shape
Out[96]:
In [97]:
train_labels.shape
Out[97]:
In [98]:
test_data.shape
Out[98]:
In [100]:
word_index = reuters.get_word_index()
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
In [102]:
decoded_newswire = ' '.join([reverse_word_index.get(i - 3, '?') for i in train_data[0]])
decoded_newswire
Out[102]:
In [103]:
import numpy as np
def vectorize_sequences(sequences, dimension=10000):
results = np.zeros((len(sequences), dimension))
for i, sequence in enumerate(sequences):
results[i, sequence] = 1.
return results
x_train = vectorize_sequences(train_data)
x_test = vectorize_sequences(test_data)
In [104]:
def to_one_hot(labels, dimension=46):
results = np.zeros((len(labels), dimension))
for i, label in enumerate(labels):
results[i, label] = 1.
return results
one_hot_train_labels = to_one_hot(train_labels)
one_hot_test_labels = to_one_hot(test_labels)
In [105]:
one_hot_train_labels.shape
Out[105]:
In [106]:
one_hot_train_labels[0]
Out[106]:
In [107]:
test_labels[0]
Out[107]:
In [108]:
from keras.utils.np_utils import to_categorical
one_hot_train_labels = to_categorical(train_labels)
one_hot_test_labels = to_categorical(test_labels)
In [109]:
from keras.models import Sequential
from keras.layers import Dense
In [110]:
model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(10000, )))
model.add(Dense(64, activation='relu'))
model.add(Dense(46, activation='softmax'))
In [112]:
model.summary()
In [113]:
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
In [114]:
x_val = x_train[:1000]
partial_x_train = x_train[1000:]
y_val = one_hot_train_labels[:1000]
partial_y_train = one_hot_train_labels[1000:]
In [115]:
history = model.fit(partial_x_train,
partial_y_train,
epochs=20,
batch_size=512,
validation_data=(x_val, y_val))
In [116]:
import matplotlib.pyplot as plt
history_dict = history.history
loss = history_dict['loss']
val_loss = history_dict['val_loss']
epochs = range(1, len(loss) + 1)
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validatioin loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
In [117]:
acc = history_dict['acc']
val_acc = history_dict['val_acc']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validatioin accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
In [130]:
y_train = np.array(train_labels)
y_test = np.array(test_labels)
y_val = y_train[:1000]
partial_y_train = y_train[1000:]
model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(10000, )))
model.add(Dense(64, activation='relu'))
model.add(Dense(46, activation='softmax'))
model.compile(optimizer='rmsprop',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(partial_x_train,
partial_y_train,
epochs=9,
batch_size=512,
validation_data=(x_val, y_val))
In [120]:
results = model.evaluate(x_test, one_hot_test_labels)
In [122]:
results
Out[122]:
In [124]:
predictions = model.predict(x_test)
In [125]:
predictions.shape
Out[125]:
In [126]:
np.sum(predictions[0])
Out[126]:
In [127]:
np.argmax(predictions[0])
Out[127]:
In [131]:
from keras.datasets import boston_housing
In [132]:
(train_data, train_targets), (test_data, test_targets) = boston_housing.load_data()
In [133]:
train_data.shape
Out[133]:
In [134]:
test_data.shape
Out[134]:
In [136]:
mean = train_data.mean(axis=0)
In [137]:
mean.shape
Out[137]:
In [138]:
std = train_data.std(axis=0)
In [139]:
std.shape
Out[139]:
In [140]:
train_data -= mean
train_data /= std
test_data -= mean
test_data /= std
In [141]:
train_data.shape[1]
Out[141]:
In [142]:
from keras.models import Sequential
from keras.layers import Dense
In [148]:
def build_model():
model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(train_data.shape[1], )))
model.add(Dense(64, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
return model
In [149]:
model = build_model()
In [150]:
import numpy as np
k = 4
num_val_samples = len(train_data) // k
num_epochs = 100
all_scores = []
In [151]:
num_val_samples
Out[151]:
In [157]:
for i in range(k):
print('processing fold #', i)
val_data = train_data[i * num_val_samples: (i + 1) * num_val_samples]
val_targets = train_targets[i * num_val_samples: (i + 1) * num_val_samples]
partial_train_data = np.concatenate(
[train_data[:i * num_val_samples],
train_data[(i + 1) * num_val_samples:]], axis=0)
partial_train_targets = np.concatenate(
[train_targets[:i * num_val_samples],
train_targets[(i + 1) * num_val_samples:]], axis=0)
model = build_model()
model.fit(partial_train_data, partial_train_targets,
epochs=num_epochs, batch_size=1, verbose=0)
val_mse, val_mae = model.evaluate(val_data, val_targets, verbose=0)
all_scores.append(val_mae)
In [158]:
all_scores
Out[158]:
In [159]:
np.mean(all_scores)
Out[159]:
In [162]:
num_epochs = 500
all_mae_histories = []
for i in range(k):
print('processing fold #', i)
val_data = train_data[i * num_val_samples: (i + 1) * num_val_samples]
val_targets = train_targets[i * num_val_samples: (i + 1) * num_val_samples]
partial_train_data = np.concatenate(
[train_data[:i * num_val_samples],
train_data[(i + 1) * num_val_samples:]], axis=0)
partial_train_targets = np.concatenate(
[train_targets[:i * num_val_samples],
train_targets[(i + 1) * num_val_samples:]], axis=0)
model = build_model()
history = model.fit(partial_train_data, partial_train_targets,
validation_data=(val_data, val_targets),
epochs=num_epochs, batch_size=1, verbose=0)
mae_history = history.history['val_mean_absolute_error']
all_mae_histories.append(mae_history)
In [164]:
np.array(all_mae_histories).shape
Out[164]:
In [170]:
average_mae_history = np.mean(all_mae_histories, axis=0)
In [171]:
average_mae_history.shape
Out[171]:
In [172]:
import matplotlib.pyplot as plt
plt.plot(range(1, len(average_mae_history) + 1), average_mae_history)
plt.xlabel('Epoch')
plt.ylabel('Validation MAE')
plt.show()
In [175]:
def smooth_curve(points, factor=0.9):
smoothed_points = []
for point in points:
if smoothed_points:
previous = smoothed_points[-1]
smoothed_points.append(previous * factor + point * (1 - factor))
else:
smoothed_points.append(point)
return smoothed_points
smooth_mae_history = smooth_curve(average_mae_history[10:])
In [176]:
import matplotlib.pyplot as plt
plt.plot(range(1, len(smooth_mae_history) + 1), smooth_mae_history)
plt.xlabel('Epoch')
plt.ylabel('Validation MAE')
plt.show()
In [177]:
model = build_model()
model.fit(train_data, train_targets,
epochs=80, batch_size=16, verbose=0)
test_mse_score, test_mae_score = model.evaluate(test_data, test_targets)
In [178]:
test_mae_score
Out[178]:
In [ ]: