In [108]:
from keras import models
from keras import layers
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
%matplotlib inline
In [2]:
model = models.Sequential()
model.add(layers.Dense(512,activation='relu', input_shape=(28*28,)))
model.add(layers.Dense(10, activation='softmax'))
In [35]:
model.summary()
In [37]:
28*28*512+512
Out[37]:
In [38]:
512*10+10
Out[38]:
In [13]:
x = np.zeros(28*28)
x = x.reshape(1,-1)
x.shape
Out[13]:
In [14]:
y = model.predict(x)
In [15]:
y
Out[15]:
In [16]:
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
In [17]:
train_images.shape
Out[17]:
In [18]:
train_labels
Out[18]:
In [19]:
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
In [20]:
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255
In [21]:
from keras.utils import to_categorical
# onehot
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
In [22]:
train_labels.shape
Out[22]:
In [23]:
train_labels[0]
Out[23]:
In [24]:
model.fit(train_images, train_labels, epochs=5, batch_size=128)
Out[24]:
In [26]:
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('test_acc:', test_acc)
In [30]:
x = test_images[10]
x = x.reshape(1,-1)
In [31]:
y = model.predict(x)
In [32]:
y
Out[32]:
In [33]:
np.argmax(y)
Out[33]:
In [34]:
test_labels[10]
Out[34]:
In [39]:
model = models.Sequential()
model.add(layers.Dense(512,activation='relu', input_shape=(28*28,)))
model.add(layers.Dropout(0.2))
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
In [41]:
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
In [43]:
model.fit(train_images, train_labels, epochs=5, batch_size=128)
Out[43]:
In [45]:
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('test_acc:', test_acc)
In [46]:
from keras.datasets import imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
In [47]:
train_data.shape
Out[47]:
In [48]:
train_labels.shape
Out[48]:
In [49]:
# word_index is a dictionary mapping words to an integer index
word_index = imdb.get_word_index()
# We reverse it, mapping integer indices to words
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
# We decode the review; note that our indices were offset by 3
# because 0, 1 and 2 are reserved indices for "padding", "start of sequence", and "unknown".
decoded_review = ' '.join([reverse_word_index.get(i - 3, '?') for i in train_data[0]])
In [50]:
decoded_review
Out[50]:
In [51]:
len(train_data[0])
Out[51]:
In [52]:
len(train_data[1])
Out[52]:
In [53]:
import numpy as np
def vectorize_sequences(sequences, dimension=10000):
# Create an all-zero matrix of shape (len(sequences), dimension)
results = np.zeros((len(sequences), dimension))
for i, sequence in enumerate(sequences):
results[i, sequence] = 1. # set specific indices of results[i] to 1s
return results
# Our vectorized training data
x_train = vectorize_sequences(train_data)
# Our vectorized test data
x_test = vectorize_sequences(test_data)
In [55]:
len(x_train[0])
Out[55]:
In [56]:
len(x_train[1])
Out[56]:
In [57]:
# Our vectorized labels
y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('float32')
In [58]:
y_train.shape
Out[58]:
In [61]:
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'))
from keras import losses
from keras import metrics
from keras import optimizers
model.compile(optimizer=optimizers.RMSprop(lr=0.001),
loss=losses.binary_crossentropy,
metrics=[metrics.binary_accuracy])
In [62]:
x_val = x_train[:10000]
partial_x_train = x_train[10000:]
y_val = y_train[:10000]
partial_y_train = y_train[10000:]
In [63]:
history = model.fit(partial_x_train,
partial_y_train,
epochs=20,
batch_size=512,
validation_data=(x_val, y_val))
In [64]:
history_dict = history.history
history_dict.keys()
Out[64]:
In [66]:
import matplotlib.pyplot as plt
acc = history.history['binary_accuracy']
val_acc = history.history['val_binary_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc) + 1)
# "bo" is for "blue dot"
plt.plot(epochs, loss, 'bo', label='Training loss')
# b is for "solid blue line"
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
In [67]:
plt.clf() # clear figure
acc_values = history_dict['binary_accuracy']
val_acc_values = history_dict['val_binary_accuracy']
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
In [68]:
# 因为val loss在4个epoch处,就开始上涨了,所以我们在4处停止
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'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=4, batch_size=512)
results = model.evaluate(x_test, y_test)
In [69]:
results
Out[69]:
In [70]:
x_test.shape
Out[70]:
In [72]:
model.predict(x_test[0].reshape(1,-1))
Out[72]:
In [102]:
from keras.layers import Input, Dense
from keras.models import Model
# This returns a tensor
inputs = Input(shape=(784,))
# a layer instance is callable on a tensor, and returns a tensor
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# model.fit(data, labels) # starts training
In [103]:
from keras.utils import plot_model
plot_model(model, to_file='model.png')
In [109]:
Image.open('model.png')
Out[109]:
In [74]:
model.summary()
In [75]:
x = Input(shape=(784,))
# This works, and returns the 10-way softmax we defined above.
y = model(x)
In [81]:
x = np.random.rand(784)
x = x.reshape(1,-1)
y = model.predict(x)
In [82]:
y.shape
Out[82]:
In [88]:
from keras.layers import TimeDistributed
# Input tensor for sequences of 20 timesteps,
# each containing a 784-dimensional vector
input_sequences = Input(shape=(20, 784))
# This applies our previous model to every timestep in the input sequences.
# the output of the previous model was a 10-way softmax,
# so the output of the layer below will be a sequence of 20 vectors of size 10.
processed_sequences = TimeDistributed(model)(input_sequences)
model = Model(input_sequences, processed_sequences)
In [91]:
x = np.random.rand(20,784)
x.shape
x = x.reshape(1,20,-1)
In [92]:
y = model.predict(x)
In [93]:
y.shape
Out[93]:
In [101]:
import keras
keras.losses.hinge??
In [ ]: