{"id":582,"date":"2018-06-09T18:54:58","date_gmt":"2018-06-09T09:54:58","guid":{"rendered":"http:\/\/www.ecobioinfo.com\/?p=582"},"modified":"2022-06-05T22:47:54","modified_gmt":"2022-06-05T13:47:54","slug":"%e6%b7%b1%e5%b1%a4%e5%ad%a6%e7%bf%92%e3%80%80keras%ef%bc%88tensorflow%e7%94%a8%ef%bc%89%e3%81%ae%e3%82%a4%e3%83%b3%e3%82%b9%e3%83%88%e3%83%bc%e3%83%ab","status":"publish","type":"post","link":"http:\/\/www.ecobioinfo.com\/?p=582","title":{"rendered":"\u6df1\u5c64\u5b66\u7fd2\u3000Keras\uff08TensorFlow\u7528\uff09\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb"},"content":{"rendered":"<p>\u4eca\u307e\u3067\u306f\u521d\u5fc3\u8005\u5411\u3051\u7df4\u7fd2\u74b0\u5883\u3068\u3057\u3066\u3001TensorFlow \u306eTFLearn \u3092\u5229\u7528\u3057\u3066\u3044\u307e\u3057\u305f\u3002<br \/>\n\u4eca\u5f8c\u306f\u3001\u3088\u308a\u8907\u96d1\u3067\u5b9f\u7528\u5411\u3051\u306e\u7d50\u679c\u3092\u5f97\u308b\u305f\u3081\u306b\u3001\u6df1\u5c64\u5b66\u7fd2\u30e9\u30a4\u30d6\u30e9\u30ea\u306e Keras \u3082\u4f7f\u3063\u3066\u307f\u307e\u3059\u3002<br \/>\nKeras \u306f TensorFlow \u5c02\u7528\u3067\u306f\u306a\u304f\u3001 CNTK, Theano \u7528\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3082\u3042\u308b\u305d\u3046\u3067\u3059\u3002<br \/>\nKeras \u3067\u306f\u3001\u65e2\u5b58\u306e\u5b66\u7fd2\u6e08\u307f\u30e2\u30c7\u30eb\u3092\u6d3b\u7528\u3057\u3084\u3059\u304f\u3001\u3088\u308a\u5b9f\u7528\u7684\u306a\u30b7\u30b9\u30c6\u30e0\u3092\u69cb\u7bc9\u3059\u308b\u306e\u306b\u5411\u3044\u3066\u3044\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u53c2\u8003URL<br \/>\n<a href=\"https:\/\/keras.io\/\">https:\/\/keras.io\/<\/a><\/p>\n<p>\uff08\uff11\uff09Keras \uff08TensorFlow\u7528\uff09 \u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/p>\n<p>Ubuntsu \u306e TensorFlow \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u6e08\u306e\u3000Anaconda\u4eee\u60f3\u74b0\u5883\u306b Keras \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>&gt; pip install keras<br \/>\n&#8230;&#8230;<br \/>\nInstalling collected packages: keras<br \/>\nSuccessfully installed keras-2.1.6<br \/>\n&#8230;&#8230;<\/p>\n<p>\u3053\u308c\u3067\u3001keras-2.1.6 \u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<p>\u203b\u3057\u304b\u3057\u3001\u8a73\u7d30\u306f\u5f8c\u8ff0\u3057\u307e\u3059\u304c\u3001\u5b66\u7fd2\u30e2\u30c7\u30eb\u4f5c\u6210\u306e\u5b9f\u884c\u6642\u306b\u30a8\u30e9\u30fc\u304c\u51fa\u305f\u306e\u3067\u3001\u5c11\u3057\u524d\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\uff082.1.3\uff09\u306b\u3057\u307e\u3057\u305f\u3002<br \/>\n\u203b\u3084\u308a\u76f4\u3057<br \/>\n&gt; pip install keras==2.1.3<\/p>\n<p>\uff08\uff12\uff09\u5b66\u7fd2\u3068\u30c6\u30b9\u30c8 (MNIST\u3092\u8a66\u3059)<\/p>\n<p>TensorFlow + TFLearn \u3067\u4f5c\u6210\u3057\u305f MNIST(\u624b\u66f8\u304d\u6587\u5b57\u306e\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8) \u3092 Keras \u3067\u3082\u8a66\u3057\u3066\u307f\u307e\u3059\u3002<br \/>\n\u300c<a href=\"https:\/\/www.ecobioinfo.com\/?p=464\">\u6df1\u5c64\u5b66\u7fd2 TensorFlow \u3092 MNIST \u3067\u8a66\u3059<\/a>\u300d\u306e\u5168\u7d50\u5408\u578b\u306e Python \u30d7\u30ed\u30b0\u30e9\u30e0\u3092 keras \u7528\u306b\u66f8\u304d\u63db\u3048\u3066\u5b9f\u884c\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- \u5168\u7d50\u5408\uff11\u5c64 Python \u30d6\u30ed\u30b0\u30e9\u30e0<br \/>\n## keras \u3092\u30a4\u30f3\u30dd\u30fc\u30c8<br \/>\nimport tensorflow as tf<br \/>\nimport keras as kr<br \/>\n## \u7d50\u679c\u306e\u8a55\u4fa1\u3000\u6570\u5024\u8a08\u7b97\u7528<br \/>\nimport numpy as np<\/p>\n<p>## trainX \u5b66\u7fd2\u7528\u30c7\u30fc\u30bf\u3001trainY \u6b63\u89e3\u30c7\u30fc\u30bf\u3001testX \u30c6\u30b9\u30c8\u7528\u30c7\u30fc\u30bf\u3001testY \u6b63\u89e3\u30c7\u30fc\u30bf<br \/>\n(trainX, trainY), (testX, testY) = kr.datasets.mnist.load_data()<\/p>\n<p>## \u5f62\u5f0f\u5909\u63db<br \/>\n# \u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u5909\u63db<br \/>\ntrainX = trainX.reshape(60000, 784)<br \/>\ntrainX = trainX\/255.<br \/>\ntestX = testX.reshape(10000, 784)<br \/>\ntestX = testX\/255.<br \/>\n# \u6b63\u89e3\u30c7\u30fc\u30bf\u3092\u5909\u63db<br \/>\ntrainY = kr.utils.to_categorical(trainY, 10)<br \/>\ntestY = kr.utils.to_categorical(testY, 10)<\/p>\n<p>## \u30e2\u30c7\u30eb\u69cb\u7bc9<br \/>\n# \u4e2d\u9593\u5c64\uff11\u5c64\u3000\u5168\u7d50\u5408\u578b<br \/>\nmodel = kr.models.Sequential()<br \/>\nmodel.add(kr.layers.Dense(units=256, input_shape=(784,), activation=&#8217;relu&#8217;))<br \/>\nmodel.add(kr.layers.Dropout(0.5))<\/p>\n<p># \u51fa\u529b\u5c64<br \/>\nmodel.add(kr.layers.Dense(units=10, activation=&#8217;softmax&#8217;))<\/p>\n<p>## \u5b66\u7fd2<br \/>\nmodel.compile(optimizer=&#8217;sgd&#8217;, loss=&#8217;categorical_crossentropy&#8217;, metrics=[&#8216;accuracy&#8217;])<br \/>\nmodel.fit(trainX, trainY, batch_size=100, epochs=20, validation_split=0.1)<\/p>\n<p>## \u30c6\u30b9\u30c8<br \/>\nresult = model.predict(testX)<\/p>\n<p>## \u7d50\u679c\u3092\u8868\u793a<br \/>\npred = np.array(result).argmax(axis=1)<br \/>\nprint(pred)<br \/>\nlabel=testY.argmax(axis=1)<br \/>\nprint(label)<br \/>\naccuracy = np.mean(pred == label, axis=0)<br \/>\nprint(accuracy)<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>keras\u306e\u30d0\u30fc\u30b8\u30e7\u30f3 2.1.6 \u3067\u5b9f\u884c\u3059\u308b\u3068\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30a8\u30e9\u30fc\u304c\u51fa\u307e\u3057\u305f\u3002<\/p>\n<p>TypeError: softmax() got an unexpected keyword argument &#8216;axis&#8217;<\/p>\n<p>\u5c11\u3057\u524d\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3067\u8a66\u3059\u3068\u30a8\u30e9\u30fc\u304c\u3067\u306a\u3044\u306e\u3067 keras==2.1.3 \u306b\u5909\u66f4\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8211; \u5b9f\u884c\u7d50\u679c<br \/>\n[7 2 1 &#8230; 4 5 6]<br \/>\n[7 2 1 &#8230; 4 5 6]<br \/>\n0.9461<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>\u3068\u308a\u3042\u3048\u305a\u3001Keras \u306e\u74b0\u5883\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>(S.Onda 2018\/6\/9)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"\u4eca\u307e\u3067\u306f\u521d\u5fc3\u8005\u5411\u3051\u7df4\u7fd2\u74b0\u5883\u3068\u3057\u3066\u3001TensorFlow \u306eTFLearn \u3092\u5229\u7528\u3057\u3066\u3044\u307e\u3057\u305f\u3002 \u4eca\u5f8c\u306f\u3001\u3088\u308a\u8907\u96d1\u3067\u5b9f\u7528\u5411\u3051\u306e\u7d50\u679c\u3092\u5f97\u308b\u305f\u3081\u306b\u3001\u6df1\u5c64\u5b66\u7fd2\u30e9\u30a4\u30d6\u30e9\u30ea\u306e Keras \u3082\u4f7f\u3063\u3066\u307f\u307e\u3059\u3002 Keras \u306f Tenso &#8230;","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-582","post","type-post","status-publish","format-standard","hentry","category-ai_ecobio"],"_links":{"self":[{"href":"http:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/posts\/582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=582"}],"version-history":[{"count":5,"href":"http:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/posts\/582\/revisions"}],"predecessor-version":[{"id":1486,"href":"http:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/posts\/582\/revisions\/1486"}],"wp:attachment":[{"href":"http:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=582"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}