Keras是基于TensorFlow和Theano(由加拿大蒙特利尔大学开发的机器学习框架)的深度学习库,是由纯python编写而成的高层神经网络API,也仅支持python开发。它是为了支持快速实践而对tensorflow或者Theano的再次封装,让我们可以不用关注过多的底层细节,能够把想法快速转换为结果。它也很灵活,且比较容易学。Keras默认的后端为tensorflow,如果想要使用theano可以自行更改。tensorflow和theano都可以使用GPU进行硬件加速,往往可以比CPU运算快很多倍。因此如果你的显卡支持cuda的话,建议尽可能利用cuda加速模型训练。(当机器上有可用的GPU时,代码会自动调用GPU 进行并行计算。) 目前Keras已经被TensorFlow收录,添加到TensorFlow 中,成为其默认的框架,成为TensorFlow官方的高级API。
我们用keras 实际的搭一个网络来看一下,在确定是否比tensorflow更加友好:
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
# Generate dummy data
x_train = np.random.random((1000, 20))
y_train = np.random.randint(2, size=(1000, 1))
x_test = np.random.random((100, 20))
y_test = np.random.randint(2, size=(100, 1))
model = Sequential()
model.add(Dense(64, input_dim=20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(x_train, y_train,
epochs=200,
batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)
print(keras.__version__)
model.summary()
Epoch 1/200
1000/1000 [==============================] - 0s - loss: 0.7169 - acc: 0.4930
Epoch 2/200
1000/1000 [==============================] - 0s - loss: 0.7091 - acc: 0.5000
Epoch 3/200
1000/1000 [==============================] - 0s - loss: 0.7099 - acc: 0.5120
Epoch 4/200
1000/1000 [==============================] - 0s - loss: 0.7016 - acc: 0.4940
Epoch 5/200
1000/1000 [==============================] - 0s - loss: 0.7037 - acc: 0.5010
Epoch 6/200
1000/1000 [==============================] - 0s - loss: 0.6971 - acc: 0.5060
Epoch 7/200
1000/1000 [==============================] - 0s - loss: 0.7024 - acc: 0.4880
Epoch 8/200
1000/1000 [==============================] - 0s - loss: 0.6945 - acc: 0.5130
Epoch 9/200
1000/1000 [==============================] - 0s - loss: 0.6963 - acc: 0.4990
Epoch 10/200
1000/1000 [==============================] - 0s - loss: 0.6980 - acc: 0.5000
Epoch 11/200
1000/1000 [==============================] - 0s - loss: 0.6869 - acc: 0.5500
Epoch 12/200
1000/1000 [==============================] - 0s - loss: 0.6936 - acc: 0.5140
Epoch 13/200
1000/1000 [==============================] - 0s - loss: 0.6959 - acc: 0.5080
Epoch 14/200
1000/1000 [==============================] - 0s - loss: 0.7027 - acc: 0.4810
Epoch 15/200
1000/1000 [==============================] - 0s - loss: 0.6911 - acc: 0.5200
Epoch 16/200
1000/1000 [==============================] - 0s - loss: 0.6879 - acc: 0.5210
Epoch 17/200
1000/1000 [==============================] - 0s - loss: 0.6988 - acc: 0.5000
Epoch 18/200
1000/1000 [==============================] - 0s - loss: 0.6972 - acc: 0.4990
Epoch 19/200
1000/1000 [==============================] - 0s - loss: 0.6929 - acc: 0.5380
Epoch 20/200
1000/1000 [==============================] - 0s - loss: 0.6955 - acc: 0.5090
Epoch 21/200
1000/1000 [==============================] - 0s - loss: 0.6897 - acc: 0.5200
Epoch 22/200
1000/1000 [==============================] - 0s - loss: 0.6856 - acc: 0.5360
Epoch 23/200
1000/1000 [==============================] - 0s - loss: 0.6860 - acc: 0.5510
Epoch 24/200
1000/1000 [==============================] - 0s - loss: 0.6947 - acc: 0.5030
Epoch 25/200
1000/1000 [==============================] - 0s - loss: 0.6913 - acc: 0.5280
Epoch 26/200
1000/1000 [==============================] - 0s - loss: 0.6828 - acc: 0.5470
Epoch 27/200
1000/1000 [==============================] - 0s - loss: 0.6876 - acc: 0.5370
Epoch 28/200
1000/1000 [==============================] - 0s - loss: 0.6872 - acc: 0.5360
Epoch 29/200
1000/1000 [==============================] - 0s - loss: 0.6926 - acc: 0.5250
Epoch 30/200
1000/1000 [==============================] - 0s - loss: 0.6878 - acc: 0.5380
Epoch 31/200
1000/1000 [==============================] - 0s - loss: 0.6889 - acc: 0.5420
Epoch 32/200
1000/1000 [==============================] - 0s - loss: 0.6885 - acc: 0.5350
Epoch 33/200
1000/1000 [==============================] - 0s - loss: 0.6858 - acc: 0.5470
Epoch 34/200
1000/1000 [==============================] - 0s - loss: 0.6902 - acc: 0.5410
Epoch 35/200
1000/1000 [==============================] - 0s - loss: 0.6837 - acc: 0.5620
Epoch 36/200
1000/1000 [==============================] - 0s - loss: 0.6854 - acc: 0.5430
Epoch 37/200
1000/1000 [==============================] - 0s - loss: 0.6867 - acc: 0.5220
Epoch 38/200
1000/1000 [==============================] - 0s - loss: 0.6830 - acc: 0.5490
Epoch 39/200
1000/1000 [==============================] - 0s - loss: 0.6829 - acc: 0.5510
Epoch 40/200
1000/1000 [==============================] - 0s - loss: 0.6826 - acc: 0.5630
Epoch 41/200
1000/1000 [==============================] - 0s - loss: 0.6869 - acc: 0.5320
Epoch 42/200
1000/1000 [==============================] - 0s - loss: 0.6849 - acc: 0.5440
Epoch 43/200
1000/1000 [==============================] - 0s - loss: 0.6855 - acc: 0.5430
Epoch 44/200
1000/1000 [==============================] - 0s - loss: 0.6797 - acc: 0.5550
Epoch 45/200
1000/1000 [==============================] - 0s - loss: 0.6795 - acc: 0.5870
Epoch 46/200
1000/1000 [==============================] - 0s - loss: 0.6842 - acc: 0.5460
Epoch 47/200
1000/1000 [==============================] - 0s - loss: 0.6824 - acc: 0.5510
Epoch 48/200
1000/1000 [==============================] - 0s - loss: 0.6835 - acc: 0.5530
Epoch 49/200
1000/1000 [==============================] - 0s - loss: 0.6852 - acc: 0.5500
Epoch 50/200
1000/1000 [==============================] - 0s - loss: 0.6774 - acc: 0.5740
Epoch 51/200
1000/1000 [==============================] - 0s - loss: 0.6764 - acc: 0.5670
Epoch 52/200
1000/1000 [==============================] - 0s - loss: 0.6781 - acc: 0.5490
Epoch 53/200
1000/1000 [==============================] - 0s - loss: 0.6770 - acc: 0.5520
Epoch 54/200
1000/1000 [==============================] - 0s - loss: 0.6785 - acc: 0.5700
Epoch 55/200
1000/1000 [==============================] - 0s - loss: 0.6746 - acc: 0.5810
Epoch 56/200
1000/1000 [==============================] - 0s - loss: 0.6820 - acc: 0.5540
Epoch 57/200
1000/1000 [==============================] - 0s - loss: 0.6819 - acc: 0.5660
Epoch 58/200
1000/1000 [==============================] - 0s - loss: 0.6647 - acc: 0.5950
Epoch 59/200
1000/1000 [==============================] - 0s - loss: 0.6843 - acc: 0.5610
Epoch 60/200
1000/1000 [==============================] - 0s - loss: 0.6812 - acc: 0.5590
Epoch 61/200
1000/1000 [==============================] - 0s - loss: 0.6776 - acc: 0.5610
Epoch 62/200
1000/1000 [==============================] - 0s - loss: 0.6777 - acc: 0.5550
Epoch 63/200
1000/1000 [==============================] - 0s - loss: 0.6824 - acc: 0.5510
Epoch 64/200
1000/1000 [==============================] - 0s - loss: 0.6740 - acc: 0.5860
Epoch 65/200
1000/1000 [==============================] - 0s - loss: 0.6710 - acc: 0.5870
Epoch 66/200
1000/1000 [==============================] - 0s - loss: 0.6706 - acc: 0.5860
Epoch 67/200
1000/1000 [==============================] - 0s - loss: 0.6714 - acc: 0.5730
Epoch 68/200
1000/1000 [==============================] - 0s - loss: 0.6695 - acc: 0.5870
Epoch 69/200
1000/1000 [==============================] - 0s - loss: 0.6703 - acc: 0.5890
Epoch 70/200
1000/1000 [==============================] - 0s - loss: 0.6732 - acc: 0.5620
Epoch 71/200
1000/1000 [==============================] - 0s - loss: 0.6749 - acc: 0.5630
Epoch 72/200
1000/1000 [==============================] - 0s - loss: 0.6661 - acc: 0.5890
Epoch 73/200
1000/1000 [==============================] - 0s - loss: 0.6687 - acc: 0.5990
Epoch 74/200
1000/1000 [==============================] - 0s - loss: 0.6721 - acc: 0.5910
Epoch 75/200
1000/1000 [==============================] - 0s - loss: 0.6632 - acc: 0.5830
Epoch 76/200
1000/1000 [==============================] - 0s - loss: 0.6662 - acc: 0.6020
Epoch 77/200
1000/1000 [==============================] - 0s - loss: 0.6718 - acc: 0.5690
Epoch 78/200
1000/1000 [==============================] - 0s - loss: 0.6717 - acc: 0.5670
Epoch 79/200
1000/1000 [==============================] - 0s - loss: 0.6658 - acc: 0.5890
Epoch 80/200
1000/1000 [==============================] - 0s - loss: 0.6672 - acc: 0.5750
Epoch 81/200
1000/1000 [==============================] - 0s - loss: 0.6589 - acc: 0.6020
Epoch 82/200
1000/1000 [==============================] - 0s - loss: 0.6699 - acc: 0.5740
Epoch 83/200
1000/1000 [==============================] - 0s - loss: 0.6626 - acc: 0.5900
Epoch 84/200
1000/1000 [==============================] - 0s - loss: 0.6648 - acc: 0.5960
Epoch 85/200
1000/1000 [==============================] - 0s - loss: 0.6716 - acc: 0.5760
Epoch 86/200
1000/1000 [==============================] - 0s - loss: 0.6594 - acc: 0.5950
Epoch 87/200
1000/1000 [==============================] - 0s - loss: 0.6603 - acc: 0.5830
Epoch 88/200
1000/1000 [==============================] - 0s - loss: 0.6627 - acc: 0.5940
Epoch 89/200
1000/1000 [==============================] - 0s - loss: 0.6631 - acc: 0.6060
Epoch 90/200
1000/1000 [==============================] - 0s - loss: 0.6606 - acc: 0.5980
Epoch 91/200
1000/1000 [==============================] - 0s - loss: 0.6534 - acc: 0.6060
Epoch 92/200
1000/1000 [==============================] - 0s - loss: 0.6706 - acc: 0.5720
Epoch 93/200
1000/1000 [==============================] - 0s - loss: 0.6455 - acc: 0.6340
Epoch 94/200
1000/1000 [==============================] - 0s - loss: 0.6592 - acc: 0.5990
Epoch 95/200
1000/1000 [==============================] - 0s - loss: 0.6554 - acc: 0.5920
Epoch 96/200
1000/1000 [==============================] - 0s - loss: 0.6606 - acc: 0.5930
Epoch 97/200
1000/1000 [==============================] - 0s - loss: 0.6570 - acc: 0.6070
Epoch 98/200
1000/1000 [==============================] - 0s - loss: 0.6503 - acc: 0.6250
Epoch 99/200
1000/1000 [==============================] - 0s - loss: 0.6505 - acc: 0.6080
Epoch 100/200
1000/1000 [==============================] - 0s - loss: 0.6534 - acc: 0.6160
Epoch 101/200
1000/1000 [==============================] - 0s - loss: 0.6535 - acc: 0.6190
Epoch 102/200
1000/1000 [==============================] - 0s - loss: 0.6595 - acc: 0.6000
Epoch 103/200
1000/1000 [==============================] - 0s - loss: 0.6616 - acc: 0.5970
Epoch 104/200
1000/1000 [==============================] - 0s - loss: 0.6469 - acc: 0.6120
Epoch 105/200
1000/1000 [==============================] - 0s - loss: 0.6526 - acc: 0.6060
Epoch 106/200
1000/1000 [==============================] - 0s - loss: 0.6544 - acc: 0.5920
Epoch 107/200
1000/1000 [==============================] - 0s - loss: 0.6523 - acc: 0.6140
Epoch 108/200
1000/1000 [==============================] - 0s - loss: 0.6465 - acc: 0.6230
Epoch 109/200
1000/1000 [==============================] - 0s - loss: 0.6417 - acc: 0.6250
Epoch 110/200
1000/1000 [==============================] - 0s - loss: 0.6447 - acc: 0.6160
Epoch 111/200
1000/1000 [==============================] - 0s - loss: 0.6484 - acc: 0.6130
Epoch 112/200
1000/1000 [==============================] - 0s - loss: 0.6470 - acc: 0.6030
Epoch 113/200
1000/1000 [==============================] - 0s - loss: 0.6537 - acc: 0.5960
Epoch 114/200
1000/1000 [==============================] - 0s - loss: 0.6458 - acc: 0.6190
Epoch 115/200
1000/1000 [==============================] - 0s - loss: 0.6395 - acc: 0.6110
Epoch 116/200
1000/1000 [==============================] - 0s - loss: 0.6499 - acc: 0.6070
Epoch 117/200
1000/1000 [==============================] - 0s - loss: 0.6320 - acc: 0.6340
Epoch 118/200
1000/1000 [==============================] - 0s - loss: 0.6390 - acc: 0.6120
Epoch 119/200
1000/1000 [==============================] - 0s - loss: 0.6394 - acc: 0.6240
Epoch 120/200
1000/1000 [==============================] - 0s - loss: 0.6503 - acc: 0.6280
Epoch 121/200
1000/1000 [==============================] - 0s - loss: 0.6409 - acc: 0.6390
Epoch 122/200
1000/1000 [==============================] - 0s - loss: 0.6463 - acc: 0.6060
Epoch 123/200
1000/1000 [==============================] - 0s - loss: 0.6351 - acc: 0.6170
Epoch 124/200
1000/1000 [==============================] - 0s - loss: 0.6334 - acc: 0.6410
Epoch 125/200
1000/1000 [==============================] - 0s - loss: 0.6446 - acc: 0.6190
Epoch 126/200
1000/1000 [==============================] - 0s - loss: 0.6335 - acc: 0.6200
Epoch 127/200
1000/1000 [==============================] - 0s - loss: 0.6342 - acc: 0.6420
Epoch 128/200
1000/1000 [==============================] - 0s - loss: 0.6349 - acc: 0.6260
Epoch 129/200
1000/1000 [==============================] - 0s - loss: 0.6362 - acc: 0.6090
Epoch 130/200
1000/1000 [==============================] - 0s - loss: 0.6385 - acc: 0.6340
Epoch 131/200
1000/1000 [==============================] - 0s - loss: 0.6286 - acc: 0.6490
Epoch 132/200
1000/1000 [==============================] - 0s - loss: 0.6292 - acc: 0.6400
Epoch 133/200
1000/1000 [==============================] - 0s - loss: 0.6394 - acc: 0.6350
Epoch 134/200
1000/1000 [==============================] - 0s - loss: 0.6335 - acc: 0.6480
Epoch 135/200
1000/1000 [==============================] - 0s - loss: 0.6302 - acc: 0.6380
Epoch 136/200
1000/1000 [==============================] - 0s - loss: 0.6206 - acc: 0.6520
Epoch 137/200
1000/1000 [==============================] - 0s - loss: 0.6252 - acc: 0.6600
Epoch 138/200
1000/1000 [==============================] - 0s - loss: 0.6241 - acc: 0.6410
Epoch 139/200
1000/1000 [==============================] - 0s - loss: 0.6315 - acc: 0.6250
Epoch 140/200
1000/1000 [==============================] - 0s - loss: 0.6207 - acc: 0.6450
Epoch 141/200
1000/1000 [==============================] - 0s - loss: 0.6258 - acc: 0.6420
Epoch 142/200
1000/1000 [==============================] - 0s - loss: 0.6350 - acc: 0.6240
Epoch 143/200
1000/1000 [==============================] - 0s - loss: 0.6265 - acc: 0.6380
Epoch 144/200
1000/1000 [==============================] - 0s - loss: 0.6278 - acc: 0.6400
Epoch 145/200
1000/1000 [==============================] - 0s - loss: 0.6339 - acc: 0.6320
Epoch 146/200
1000/1000 [==============================] - 0s - loss: 0.6207 - acc: 0.6640
Epoch 147/200
1000/1000 [==============================] - 0s - loss: 0.6319 - acc: 0.6400
Epoch 148/200
1000/1000 [==============================] - 0s - loss: 0.6102 - acc: 0.6320
Epoch 149/200
1000/1000 [==============================] - 0s - loss: 0.6303 - acc: 0.6260
Epoch 150/200
1000/1000 [==============================] - 0s - loss: 0.6209 - acc: 0.6360
Epoch 151/200
1000/1000 [==============================] - 0s - loss: 0.6112 - acc: 0.6480
Epoch 152/200
1000/1000 [==============================] - 0s - loss: 0.6315 - acc: 0.6320
Epoch 153/200
1000/1000 [==============================] - 0s - loss: 0.6174 - acc: 0.6480
Epoch 154/200
1000/1000 [==============================] - 0s - loss: 0.6222 - acc: 0.6340
Epoch 155/200
1000/1000 [==============================] - 0s - loss: 0.6284 - acc: 0.6270
Epoch 156/200
1000/1000 [==============================] - 0s - loss: 0.6251 - acc: 0.6470
Epoch 157/200
1000/1000 [==============================] - 0s - loss: 0.6163 - acc: 0.6570
Epoch 158/200
1000/1000 [==============================] - 0s - loss: 0.6119 - acc: 0.6600
Epoch 159/200
1000/1000 [==============================] - 0s - loss: 0.6227 - acc: 0.6400
Epoch 160/200
1000/1000 [==============================] - 0s - loss: 0.6178 - acc: 0.6540
Epoch 161/200
1000/1000 [==============================] - 0s - loss: 0.5987 - acc: 0.6700
Epoch 162/200
1000/1000 [==============================] - 0s - loss: 0.6195 - acc: 0.6520
Epoch 163/200
1000/1000 [==============================] - 0s - loss: 0.5964 - acc: 0.6770
Epoch 164/200
1000/1000 [==============================] - 0s - loss: 0.6135 - acc: 0.6360
Epoch 165/200
1000/1000 [==============================] - 0s - loss: 0.6135 - acc: 0.6600
Epoch 166/200
1000/1000 [==============================] - 0s - loss: 0.6076 - acc: 0.6680
Epoch 167/200
1000/1000 [==============================] - 0s - loss: 0.6119 - acc: 0.6470
Epoch 168/200
1000/1000 [==============================] - 0s - loss: 0.6224 - acc: 0.6330
Epoch 169/200
1000/1000 [==============================] - 0s - loss: 0.6083 - acc: 0.6570
Epoch 170/200
1000/1000 [==============================] - 0s - loss: 0.6158 - acc: 0.6310
Epoch 171/200
1000/1000 [==============================] - 0s - loss: 0.5967 - acc: 0.6670
Epoch 172/200
1000/1000 [==============================] - 0s - loss: 0.6069 - acc: 0.6620
Epoch 173/200
1000/1000 [==============================] - 0s - loss: 0.6173 - acc: 0.6520
Epoch 174/200
1000/1000 [==============================] - 0s - loss: 0.6081 - acc: 0.6560
Epoch 175/200
1000/1000 [==============================] - 0s - loss: 0.6028 - acc: 0.6750
Epoch 176/200
1000/1000 [==============================] - 0s - loss: 0.5989 - acc: 0.6760
Epoch 177/200
1000/1000 [==============================] - 0s - loss: 0.5975 - acc: 0.6690
Epoch 178/200
1000/1000 [==============================] - 0s - loss: 0.6016 - acc: 0.6820
Epoch 179/200
1000/1000 [==============================] - 0s - loss: 0.5929 - acc: 0.6660
Epoch 180/200
1000/1000 [==============================] - 0s - loss: 0.6043 - acc: 0.6740
Epoch 181/200
1000/1000 [==============================] - 0s - loss: 0.5839 - acc: 0.6730
Epoch 182/200
1000/1000 [==============================] - 0s - loss: 0.6007 - acc: 0.6650
Epoch 183/200
1000/1000 [==============================] - 0s - loss: 0.6087 - acc: 0.6680
Epoch 184/200
1000/1000 [==============================] - 0s - loss: 0.5902 - acc: 0.6910
Epoch 185/200
1000/1000 [==============================] - 0s - loss: 0.6017 - acc: 0.6660
Epoch 186/200
1000/1000 [==============================] - 0s - loss: 0.5949 - acc: 0.6640
Epoch 187/200
1000/1000 [==============================] - 0s - loss: 0.6036 - acc: 0.6590
Epoch 188/200
1000/1000 [==============================] - 0s - loss: 0.6023 - acc: 0.6570
Epoch 189/200
1000/1000 [==============================] - 0s - loss: 0.5892 - acc: 0.6710
Epoch 190/200
1000/1000 [==============================] - 0s - loss: 0.6060 - acc: 0.6570
Epoch 191/200
1000/1000 [==============================] - 0s - loss: 0.5939 - acc: 0.6690
Epoch 192/200
1000/1000 [==============================] - 0s - loss: 0.5912 - acc: 0.6970
Epoch 193/200
1000/1000 [==============================] - 0s - loss: 0.5863 - acc: 0.6870
Epoch 194/200
1000/1000 [==============================] - 0s - loss: 0.5917 - acc: 0.6630
Epoch 195/200
1000/1000 [==============================] - 0s - loss: 0.5739 - acc: 0.6960
Epoch 196/200
1000/1000 [==============================] - 0s - loss: 0.5986 - acc: 0.6780
Epoch 197/200
1000/1000 [==============================] - 0s - loss: 0.6069 - acc: 0.6570
Epoch 198/200
1000/1000 [==============================] - 0s - loss: 0.5793 - acc: 0.6860
Epoch 199/200
1000/1000 [==============================] - 0s - loss: 0.5962 - acc: 0.6720
Epoch 200/200
1000/1000 [==============================] - 0s - loss: 0.5961 - acc: 0.6870
100/100 [==============================] - 0s
2.0.6
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_33 (Dense) (None, 64) 1344
_________________________________________________________________
dropout_17 (Dropout) (None, 64) 0
_________________________________________________________________
dense_34 (Dense) (None, 64) 4160
_________________________________________________________________
dropout_18 (Dropout) (None, 64) 0
_________________________________________________________________
dense_35 (Dense) (None, 1) 65
=================================================================
Total params: 5,569
Trainable params: 5,569
Non-trainable params: 0
_________________________________________________________________
结束!
|