keras学习基础
参考《Python深度学习》一书
python 扩展学习
1 在 Python 2.2 :
要引用: from __future__ import division
" / "就表示 浮点数除法,返回浮点结果;" // "表示整数除法。
2 Python 3以后 :
" / "就表示 浮点数除法,返回浮点结果;" // "表示整数除法。
————————————————
版权声明:本文为CSDN博主「lw_waston」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lw_waston/article/details/83150304
数据处理相关
'''
如果你的 targets 是 one-hot 编码,用 categorical_crossentropy
one-hot 编码:[0, 0, 1], [1, 0, 0], [0, 1, 0]
如果你的 tagets 是 数字编码 ,用 sparse_categorical_crossentropy
数字编码:2, 0, 1
'''
def to_categorical(y, num_classes=None, dtype='float32')
简单来说,to_categorical就是将类别向量转换为二进制(只有0和1)的矩阵类型表示。其表现为将原有的类别向量转换为独热编码的形式。
等同于以下方法:
def to_one_hot(labels, dimension=46):#负责转变y的,结果和上面to_categorical方法得到的结果是一样的,
results = np.zeros((len(labels), dimension))#新建这么大维度的矩阵
for i, label in enumerate(labels):
results[i, label] = 1.#对应label位置标1
return results
'''array和asarray都可将结构数据转换为ndarray类型。
但是主要区别就是当数据源是ndarray时,
array仍会copy出一个副本,占用新的内存,但asarray不会。'''
y_train = train_labels.astype('float32')#强制类型转换
轴方向
import numpy as np
a =np.array([[1,2,3],[2,3,4]])
mean=a.mean(axis=0)
a=a-mean
print(a)
print(mean)
print(a.shape)
#axis=0纵轴方向求平均值
[[-0.5 -0.5 -0.5]
[ 0.5 0.5 0.5]]
[1.5 2.5 3.5]
(2, 3)
注意之后,k折交叉验证合并数据的代码,axis=0
partial_train_data = np.concatenate([train_data[:i*num_val_samples],train_data[(i+1)*num_val_samples:]],axis=0)#?axis=0 纵向拼接
partial_train_targets = np.concatenate([train_data[:i*num_val_samples],train_data[(i+1)*num_val_samples:]],axis=0)
常用画图总结
print(history.history.keys())
#dict_keys(['loss', 'accuracy', 'val_loss', 'val_accuracy'])
import matplotlib.pyplot as plt
acc = history.history['accuracy']
val_acc = history.history['val_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, 'b', label='Training loss')
# b is for "solid blue line"
plt.plot(epochs, val_loss, 'r', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.figure()
plt.plot(epochs,acc,'b',label='Training acc')
plt.plot(epochs,val_acc,'r',label='Validation acc')
plt.title('Accuracy')
plt.legend()
plt.show()
神经网络相关
如果您需要将数据分类为大量类别,那么您应该避免使用太小的中间层造成的信息瓶颈。
metrics
acc mae
model.compile(loss='mean_squared_error',
optimizer='sgd',
metrics=['mae', 'acc'])#这里同时写了两种metrics
from keras import metrics
model.compile(loss='mean_squared_error',
optimizer='sgd',
metrics=[metrics.mae, metrics.categorical_accuracy])
binary_accuracy
binary_accuracy(y_true, y_pred)
categorical_accuracy
categorical_accuracy(y_true, y_pred)
sparse_categorical_accuracy
sparse_categorical_accuracy(y_true, y_pred)
top_k_categorical_accuracy
top_k_categorical_accuracy(y_true, y_pred, k=5)
sparse_top_k_categorical_accuracy
sparse_top_k_categorical_accuracy(y_true, y_pred, k=5)
自定义评价函数
自定义评价函数应该在编译的时候(compile)传递进去。该函数需要以 (y_true, y_pred)
作为输入参数,并返回一个张量作为输出结果。
import keras.backend as K
def mean_pred(y_true, y_pred):
return K.mean(y_pred)
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy', mean_pred])
loss
可用损失函数
mean_squared_error
mean_squared_error(y_true, y_pred)
mean_absolute_error
mean_absolute_error(y_true, y_pred)
mean_absolute_percentage_error
mean_absolute_percentage_error(y_true, y_pred)
mean_squared_logarithmic_error
mean_squared_logarithmic_error(y_true, y_pred)
squared_hinge
squared_hinge(y_true, y_pred)
hinge
hinge(y_true, y_pred)
categorical_hinge
categorical_hinge(y_true, y_pred)
logcosh
logcosh(y_true, y_pred)
预测误差的双曲余弦的对数。
相关名词
https://www.jianshu.com/p/9ee85fdad150
均方误差(MSE)
_preditc=reg.predict(x_test) #reg是训练好的模型
mse_test=np.sum((y_preditc-y_test)**2)/len(y_test) #跟数学公式一样的
均方根误差(RMSE)
rmse_test=mse_test ** 0.5
MAE(平均绝对误差)
mae_test=np.sum(np.absolute(y_preditc-y_test))/len(y_test)
R Squared
1- mean_squared_error(y_test,y_preditc)/ np.var(y_test)
from sklearn.metrics import mean_squared_error #均方误差
from sklearn.metrics import mean_absolute_error #平方绝对误差
from sklearn.metrics import r2_score#R square
#调用
mean_squared_error(y_test,y_predict)
mean_absolute_error(y_test,y_predict)
r2_score(y_test,y_predict)
k折交叉验证
#K折交叉验证
import numpy as np
k=4
num_val_samples = len(train_data) // k
num_epochs = 100
all_scores = []
for i in range(k):
print('processing fol #',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)#?axis=0 纵向拼接
partial_train_targets = np.concatenate([train_data[:i*num_val_samples],train_data[(i+1)*num_val_samples:]],axis=0)
# Build the Keras model (already compiled)
model = build_model()
# Train the model (in silent mode, verbose=0)
model.fit(partial_train_data, partial_train_targets,
epochs=num_epochs, batch_size=1, verbose=0)
# Evaluate the model on the validation data
val_mse, val_mae = model.evaluate(val_data, val_targets, verbose=0)
all_scores.append(val_mae)
防止过拟合的方法
1)更小的模型
2)添加权重正则化
- L1 正则化(L1 regularization):添加的成本与权重系数的绝对值[权重的 L1 范数(norm)]成正比。
- L2 正则化(L2 regularization):添加的成本与权重系数的平方(权重的 L2 范数)成正比。
神经网络的 L2 正则化也叫权重衰减(weight decay)。不要被不同的名称搞混,权重衰减
与 L2 正则化在数学上是完全相同的。
在 Keras 中,添加权重正则化的方法是向层传递权重正则化项实例(weight regularizer
instance)作为关键字参数。
from keras import regularizers
model = models.Sequential()
model.add(layers.Dense(16, kernel_regularizer=regularizers.l2(0.001),
activation='relu', input_shape=(10000,)))
model.add(layers.Dense(16, kernel_regularizer=regularizers.l2(0.001),
activation='relu'))
model.add(layers.Dense(1, activation='sigmoid')
from keras import regularizers
regularizers.l1(0.001)#l1正则化
regularizers.l1_l2(l1=0.001, l2=0.001)#同时l2和l1正则化
3)dropout层
model = models.Sequential()
model.add(layers.Dense(16, activation='relu', input_shape=(10000,)))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(1, activation='sigmoid'))
总结一下,防止神经网络过拟合的常用方法包括:
- 获取更多的训练数据
- 减小网络容量
- 添加权重正则化
- 添加 dropout
网友评论