手写数字识别
from keras.datasets import mnist
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import SimpleRNN, Dense
"""数据读取和预处理"""
(x, y), _ = mnist.load_data()
x = x / 255
y = to_categorical(y, 10)
"""建模"""
model = Sequential()
model.add(SimpleRNN(units=64, input_shape=(28, 28)))
model.add(Dense(units=10, activation='softmax'))
"""编译"""
model.compile('adam', 'categorical_crossentropy', ['acc'])
"""拟合、取10%样本来验证"""
model.fit(x, y, batch_size=256, epochs=20, verbose=2, validation_split=.1)
文本序列预测
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, LSTM
from keras.utils.np_utils import to_categorical
"""创建样本"""
sequence = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
chr2int = {c: i for i, c in enumerate(sequence)}
int2chr = {i: c for i, c in enumerate(sequence)}
seq_len = len(sequence)
window = 4
x_ls, y_ls = [], []
for i in range(seq_len - window):
seq_in = sequence[i: i + window]
seq_out = sequence[i + window]
x_ls.append([chr2int[c] for c in seq_in])
y_ls.append(chr2int[seq_out])
print(seq_in, '->', seq_out)
x = np.reshape(x_ls, (len(x_ls), window, 1))
y = to_categorical(y_ls)
"""建模"""
model = Sequential()
model.add(LSTM(40, input_shape=x.shape[1:]))
model.add(Dense(y.shape[1], activation='softmax'))
model.compile('RMSprop', 'categorical_crossentropy', ['acc'])
model.fit(x, y, batch_size=1, epochs=400, verbose=2)
"""预测和评分"""
prediction = np.argmax(model.predict(x), axis=1)
for seq_in, seq_out in zip(x, prediction):
seq_in = [int2chr[i] for i in seq_in.reshape(-1)]
seq_out = int2chr[seq_out]
print(seq_in, '->', seq_out)
acc = model.evaluate(x, y, verbose=2)[1]
print('准确率:%.2f%%' % acc)
余弦曲线拟合
import numpy as np, matplotlib.pyplot as mp
from keras.models import Sequential
from keras.layers import Dense, LSTM
"""创建样本"""
x_len = 1075
x = np.linspace(0, np.pi * 10.75, x_len, endpoint=False)
y = np.cos(x)
window = 75
X = [y[i: i + window] for i in range(x_len - window)]
X = np.reshape(X, (-1, window, 1))
Y = y[window:].reshape(-1, 1)
"""建模"""
model = Sequential()
model.add(LSTM(units=50, input_shape=X.shape[1:],
return_sequences=True))
model.add(LSTM(units=100,
return_sequences=False))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
model.fit(X, Y, batch_size=100, epochs=10, verbose=2)
"""预测"""
pred_len = 150
for start in range(0, 1000, 200):
x_pred = x[window + start: window + start + pred_len]
y_pred = []
X_pred = X[start]
for i in range(pred_len):
Y_pred = model.predict(X_pred.reshape(-1, window, 1))
y_pred.append(Y_pred[0])
X_pred = np.concatenate((X_pred, Y_pred))[1:]
mp.scatter(x_pred[0], y_pred[0], c='r', s=9)
mp.plot(x_pred, y_pred, 'r')
mp.plot(x, y, 'y', linewidth=5, alpha=0.3)
mp.show()

from keras.utils import plot_model
plot_model(model, show_shapes=True, show_layer_names=False)

打印state
from keras.models import Model
from keras.layers import Input, SimpleRNN, LSTM, GRU
from keras.utils import plot_model
from numpy import reshape
x = reshape([1, 1], (1, 2, 1))
units = 4
l_rnn_ls = [
SimpleRNN(units, return_sequences=True, return_state=True),
LSTM(units, return_sequences=True, return_state=True),
GRU(units, return_sequences=True, return_state=True)]
path_ls = ['SimpleRNN.png', 'LSTM.png', 'GRU.png']
for l_rnn, path in zip(l_rnn_ls, path_ls):
t_input = Input(shape=(2, 1))
t_output_ls = l_rnn(t_input)
model = Model(t_input, t_output_ls)
plot_model(model, path, show_shapes=True, show_layer_names=False)
t_output_ls = model.predict(x)
print(path.replace('.png', ''))
for t_output in t_output_ls:
print(t_output, t_output.shape)
print()
