莫烦大神Keras -->> 学习视频地址
这两天用keras训练模型发现原来keras会根据当前环境自动使用GPU,不需要自己设置的,一直在网上找各种方法,原来根本不要需要加其他东西,有两个GPU,它就使用两个GPU,只有CPU就只是用CPU,方便。
2019.3.8
只有两个
查看当前使用的backend
:
在terminal输入import keras
就会有类似输出
Using TensorFlow backend.
修改当前backend
:
找到以下文件并打开:
~/.keras/keras.json
文件内容:
{
"image_dim_ordering": "tf",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "theano"
}
如果需要把 Backend 改成 Tensorflow 的话,只需要改动最后一行”backend”对应的值即可。
不过一般这样直接修改参数会报错,但是如果把文本内容全部复制过去替换之前的内容就不会报错,神奇
最好的解决方法,还是在python代码中import keras前加入一个环境变量修改的语句:
import os
os.environ['KERAS_BACKEND']='theano'
这时import keras
就会显示Using Theano backend
。
不过这种方法只是临时的,并不会修改.json
文件,再次打开还是根据.json
文件里的backend
设置来启动。
这里只涉及一个简单的线性回归方程
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.models import Sequential #按顺序建层
from keras.layers import Dense #全连接层
import matplotlib.pyplot as plt #可视化模块
# create some data
X = np.linspace(-1, 1, 200)
np.random.shuffle(X) # randomize the data
Y = 0.5 * X + 2 + np.random.normal(0, 0.05, (200, ))
# plot data
plt.scatter(X, Y)
plt.show()
X_train, Y_train = X[:160], Y[:160] # first 160 data points
X_test, Y_test = X[160:], Y[160:] # last 40 data points
# build a neural network from the 1st layer to the last layer
model = Sequential()
model.add(Dense(output_dim=1,input_dim=1)) #在定义下一层的时候跟tf有不同,不需要指定上一层是哪一层,会默认上一层为输入层
# choose loss functon and optimizing method
model.compile(loss='mse',optimizer='sgd') # mse表示均方误差 sgd表示随机梯度下降法
# training
print('Training -----------')
for step in range(301):
# train_on_batch 一批一批的训练 X_train, Y_train。默认的返回值是 cost,每100步输出一下结果
cost = model.train_on_batch(X_train, Y_train) #默认返回值cost
if step % 100 == 0:
print('train cost: ', cost)
# test
print('\nTesting ------------')
cost = model.evaluate(X_test, Y_test, batch_size=40)
print('test cost:', cost)
W, b = model.layers[0].get_weights() #选第一个layer,就是Dense的全连接层
print('Weights=', W, '\nbiases=', b)
# plotting the prediction
Y_pred = model.predict(X_test)
plt.scatter(X_test, Y_test)
plt.plot(X_test, Y_pred)
plt.show()
/home/will/anaconda3/envs/py35/lib/python3.5/site-packages/ipykernel_launcher.py:20: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(units=1, input_dim=1)`
Training -----------
train cost: 4.0225005
train cost: 0.07323862
train cost: 0.00386274
train cost: 0.0026434488
Testing ------------
40/40 [==============================] - 0s 666us/step
test cost: 0.0031367032788693905
Weights= [[0.4922711]]
biases= [1.9995022]
这一节中需要去下载mnist的数据集
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import RMSprop
# download the mnist to the path '~/.keras/datasets/' if it is the first time to be called
# X shape (60,000 28x28), y shape (10,000, )
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# data pre-processing
# 输入的 x 变成 60,000*784 的数据,然后除以 255 进行标准化
# 因为每个像素都是在 0 到 255 之间的,标准化之后就变成了 0 到 1 之间。
X_train = X_train.reshape(X_train.shape[0], -1) / 255. # normalize
X_test = X_test.reshape(X_test.shape[0], -1) / 255. # normalize
y_train = np_utils.to_categorical(y_train, num_classes=10) # 将y转化为one-hot vector
y_test = np_utils.to_categorical(y_test, num_classes=10)
# Another way to build your neural net
#32是输出的维数
model = Sequential([
Dense(32,input_dim=784),
Activation('relu'),
Dense(10),
Activation('softmax')
])
# Another way to define your optimizer
rmsprop = RMSprop(lr=0.001,rho=0.9,epsilon=1e-08,decay=0.0)
# We add metrics to get more results you want to see
# 不自己定义,直接用内置的优化器也行,optimizer='rmsprop'
#激活模型:接下来用 model.compile 激励神经网络。
model.compile(
optimizer=rmsprop,
loss='categorical_crossentropy',
metrics=['accuracy'],
)
print('Training ------------')
# Another way to train the model
# 上一个教程是用train_on_batch 一批一批的训练 X_train, Y_train。默认的返回值是 cost,每100步输出一下结果
model.fit(X_train,y_train,epochs=2,batch_size=32)
print('\nTesting ------------')
# Evaluate the model with the metrics we defined earlier
loss, accuracy = model.evaluate(X_test, y_test)
print('test loss: ', loss)
print('test accuracy: ', accuracy)
输出结果
Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz
11493376/11490434 [==============================] - 24s 2us/step
Training ------------
Epoch 1/2
60000/60000 [==============================] - 13s 216us/step - loss: 0.3453 - acc: 0.9043
Epoch 2/2
60000/60000 [==============================] - 13s 219us/step - loss: 0.1991 - acc: 0.9427
Testing ------------
10000/10000 [==============================] - 1s 112us/step
test loss: 0.17848415599018336
test accuracy: 0.9489
输出的样式与上一节的有所不同,感觉用model.fit()
更清晰明了
这里用到了2个卷积层、2个池化层和2个全连接层
教学里面说的函数和参数都更新了,注意分辨,运行的时候也会有warning提示。
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flatten,Conv2D
from keras.optimizers import Adam
# download the mnist to the path '~/.keras/datasets/' if it is the first time to be called
# training X shape (60000, 28x28), Y shape (60000, ). test X shape (10000, 28x28), Y shape (10000, )
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# data pre-processing
X_train = X_train.reshape(-1, 1,28, 28)/255. # -1代表例子的个数 ,1是channel ,
X_test = X_test.reshape(-1, 1,28, 28)/255.
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
# Another way to build your CNN
model = Sequential()
#Conv2D(input_shape=(1, 28, 28..., kernel_size=(5, 5), filters=32, padding="same")`
# Conv layer 1 output shape (32, 28, 28)
# 教程里的参数名称很多都更新了,如:nb_filter是滤波器的个数,现在用filter=32就可以了
model.add(Conv2D(
batch_input_shape=(None, 1, 28, 28), #(batch,channels,steps)第一个参数是batch,1是channel,后面的是height & width
filters=32,
kernel_size=5,
padding='same', #padding method
#有两种格式,根据输入格式来定这个,channels_first对应(batch,channels,steps)
#channels_last对应(batch,steps,channels)
data_format='channels_first',
))
model.add(Activation('relu'))
#MaxPooling2D(padding="same", strides=(2, 2), pool_size=(2, 2))
# Pooling layer 1 (max pooling) output shape (32, 14, 14)
model.add(MaxPooling2D(
pool_size=2,
strides=2,
padding='same', #padding method #border_mode='same'已经更新了,可以用padding=“same”
data_format='channels_first',
))
# Conv layer 2 output shape (64, 14, 14)
model.add(Conv2D(64,5,strides=1,padding='same',data_format='channels_first'))
model.add(Activation('relu'))
# Pooling layer 2 (max pooling) output shape (64, 7, 7)
model.add(MaxPooling2D(2,2,padding="same",data_format='channels_first',))
# Fully connected layer 1 input shape (64 * 7 * 7) = (3136), output shape (1024)
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
# Fully connected layer 2 to shape (10) for 10 classes
model.add(Dense(10))
model.add(Activation('softmax'))
# Another way to define your optimizer
adam = Adam(lr=1e-4)
# We add metrics to get more results you want to see
model.compile(optimizer=adam,
loss='categorical_crossentropy',
metrics=['accuracy'])
print('Training ------------')
# Another way to train the model
model.fit(X_train, y_train, epochs=1, batch_size=64,)
print('\nTesting ------------')
# Evaluate the model with the metrics we defined earlier
loss, accuracy = model.evaluate(X_test, y_test)
print('\ntest loss: ', loss)
print('\ntest accuracy: ', accuracy)
输出结果:
Training ------------
Epoch 1/1
60000/60000 [==============================] - 554s 9ms/step - loss: 0.2698 - acc: 0.9265
Testing ------------
10000/10000 [==============================] - 42s 4ms/step
test loss: 0.09977031375914812
test accuracy: 0.9689
以64为单位的小数据集进行训练,做了一个对比,不用小数据集训练的结果没有以小数据集训练的效果高。
使用keras
可以保存model
的结构
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.models import Sequential
from keras.layers import Dense
from keras.models import load_model
# create some data
X = np.linspace(-1, 1, 200) #-1是下限,1是上限,200是分成200份均匀间隔的数字
np.random.shuffle(X) # randomize the data #打乱数据的排序
Y = 0.5 * X + 2 + np.random.normal(0, 0.05, (200, ))
X_train, Y_train = X[:160], Y[:160] # first 160 data points
X_test, Y_test = X[160:], Y[160:] # last 40 data points
model = Sequential()
model.add(Dense(units=1, input_dim=1))
model.compile(loss='mse', optimizer='sgd')
for step in range(301):
cost = model.train_on_batch(X_train, Y_train)
# save
print('test before save:',model.predict(X_test[0:2]))
model.save('my_model.h5') # h5是保存格式,需要安装有HDF5,pip install h5py
del model #deletes the existing model 保存完模型之后,删掉它,后面可以来比较是否成功的保存。
#load
model = load_model('my_model.h5') #导入保存好的模型,再执行一遍预测
print('test after load:',model.predict(X_test[0:2]))
思路就是先创建一个model
,做一次预测,然后保存模型和参数,跟着把模型删除掉,提取保存的模型文件,再做一次预测,对比输出结果。
输出结果:
test before save: [[1.8832371]
[2.1850314]]
test after load: [[1.8832371]
[2.1850314]]
另外还有其他保存模型并调用的方式,第一种是只保存权重而不保存模型的结构。
# save and load weights
model.save_weights('my_model_weights.h5')
model.load_weights('my_model_weights.h5')
第二种是用 model.to_json 保存完结构之后,然后再去加载这个json_string。
# save and load fresh network without trained weights
from keras.models import model_from_json
json_string = model.to_json()
model = model_from_json(json_string)