{"id":107,"date":"2019-11-27T16:52:27","date_gmt":"2019-11-27T08:52:27","guid":{"rendered":"http:\/\/8.154.33.202\/?p=107"},"modified":"2019-11-27T16:52:27","modified_gmt":"2019-11-27T08:52:27","slug":"tensorflow-%e8%ae%ad%e7%bb%83%e7%bd%91%e7%bb%9c%e7%9a%84%e4%b8%80%e8%88%ac%e6%ad%a5%e9%aa%a4","status":"publish","type":"post","link":"http:\/\/8.154.33.202\/?p=107","title":{"rendered":"tensorflow \u8bad\u7ec3\u7f51\u7edc\u7684\u4e00\u822c\u6b65\u9aa4"},"content":{"rendered":"\n<p>\u672c\u6587\u4e0d\u9488\u5bf9tensorflow2.0\u3002\u9996\u5148\u8981\u6784\u5efa\u6570\u636e\u7684\u8f93\u5165\uff0c\u4e00\u822c\u662f\u5c06\u6570\u636e\u8f6c\u5316\u4e3apb\u683c\u5f0f<\/p>\n\n\n\n<p>\u7136\u540e\u6784\u5efa\u81ea\u5df1\u7684\u7f51\u7edc\uff0c\u5e76\u6784\u5efa\u635f\u5931\u51fd\u6570\u7684\u8282\u70b9\u3002\u6784\u5efa\u7f51\u7edc\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u4ee3\u7801\u6784\u5efa\uff08\u5229\u7528slim\u3001keras\u7b49\u9ad8\u7ea7api\uff0c\u6216\u8005\u57fa\u7840\u7684api\uff0c\u6216\u8005\u5df2\u6709\u7684\u4ee3\u7801\uff09\uff0c\u4e5f\u53ef\u4ee5\u4ececkpt.meta\u4e2d\u8f7d\u5165\u7f51\u7edc\u7ed3\u6784\uff08\u65ad\u70b9\u7ee7\u7eed\u8bad\u7ec3\u7b49\u60c5\u51b5\uff09tf.train.import_meta_graph(&#8220;xxx.ckpt.meta&#8221;)\u3002\u8fd9\u91cc\u8981\u6ce8\u610f\uff0c\u4e00\u822c\u8bad\u7ec3\u65f6\u4f1a\u540c\u65f6\u8fdb\u884c\u7f51\u7edc\u5728\u9a8c\u8bc1\u96c6\u4e0a\u7684\u6d4b\u8bd5\uff0c\u6bd4\u5982\u6bcf\u8bad\u7ec3n\u6b65\u540e\u5728\u8bad\u7ec3\u96c6\u4e0a\u8fdb\u884c\u6d4b\u8bd5\u3002\u56e0\u6b64\u6784\u5efa\u7f51\u7edc\u9700\u8981\u540c\u65f6\u6784\u5efa\u4e00\u4e2a\u9a8c\u8bc1\u7f51\u7edc\uff0c\u5171\u4eab\u8bad\u7ec3\u7f51\u7edc\u7684\u53d8\u91cf\u6743\u91cd\u3002\u6784\u5efa\u9a8c\u8bc1\u7f51\u7edc\u65f6\u8981\u5728variable_scope\u4e2d\u8bbe\u7f6ereuse=True\u3002<\/p>\n\n\n\n<p>\u5b9a\u4e49\u4f18\u5316\u5668\uff0c\u5982opt=tf.train.AdamOptimizer()<br>\u5c06\u4f18\u5316\u5668\u5e94\u7528\u5728\u635f\u5931\u8282\u70b9\u4e0a\u8ba1\u7b97\u68af\u5ea6\u3002grads=opt.compute_gradients(L)<br>\u68af\u5ea6\u4e0b\u964d\u4f18\u5316\u8282\u70b9 apply_grad_op = opt.apply_gradients(grads)<\/p>\n\n\n\n<p>\u8bad\u7ec3\u6a21\u578b\u9700\u8981\u4fdd\u5b58\uff0c\u5b9a\u4e49\u4e00\u4e2asaver<br>saver = tf.train.Saver(max_to_keep=10) \u6700\u591a\u4fdd\u755910\u4e2ackpt<br>\u5728\u8bad\u7ec3\u65f6\uff0c\u4f7f\u7528saver.save(sess, &#8220;xxx.ckpt&#8221;, global_step=step)\u4fdd\u5b58ckpt\u6587\u4ef6<\/p>\n\n\n\n<p>\u5e0c\u671b\u5728\u8bad\u7ec3\u65f6\u770b\u5230\u8bad\u7ec3\u8fc7\u7a0b\uff0c \u4f7f\u7528tf.summary.scalar \u6dfb\u52a0\u60f3\u8981\u7684\u53d8\u91cf\u5230\u8bad\u7ec3\u8fc7\u7a0b\u65e5\u5fd7\u4e2d\u3002<br>\u5982 tf.summary.scalar(&#8220;training loss&#8221;, L)\u6dfb\u52a0\u8bad\u7ec3\u635f\u5931\u5230\u8bad\u7ec3\u8fc7\u7a0b\u3002\u7136\u540e\u5b9a\u4e49summary_op = tf.summary.merge_all() <br>\u7136\u540e\u8981\u5b9a\u4e49\u4e00\u4e2asummary_writer<br>summary_writer = tf.summary.FileWriter(logdir, sess.graph)<br>\u8bad\u7ec3\u65f6\uff0c\u6bcf\u9694n\u6b65\uff0c\u4f7f\u7528summary_writer.add_summary(sess.run(summary_op), step)\u4fdd\u5b58\u8bad\u7ec3\u8fc7\u7a0b\u65e5\u5fd7<br>\u8bad\u7ec3\u5f00\u59cb\u540e\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528tensorboard\u67e5\u770b\u8bad\u7ec3\u8fc7\u7a0b\u4e86<\/p>\n\n\n\n<p>\u8bad\u7ec3\u8fc7\u7a0b\u4e00\u822c\u5728\u4e00\u4e2afor\u5faa\u73af\u4e2d\u8fdb\u884c\uff0c<br>sess.run([apply_grad_op])\u8fdb\u884c\u7f51\u7edc\u7684\u8bad\u7ec3<br>\u5728\u8fd9\u4e2a\u5faa\u73af\u4e2d\uff0c\u8fd8\u8981\u8fdb\u884c\u4e0a\u9762\u6240\u8bf4\u7684\u4fdd\u5b58ckpt\u6587\u4ef6\u3001\u8bad\u7ec3\u65e5\u5fd7<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4e0d\u9488\u5bf9tensorflow2.0\u3002\u9996\u5148\u8981\u6784\u5efa\u6570\u636e\u7684\u8f93\u5165\uff0c\u4e00\u822c\u662f\u5c06\u6570\u636e\u8f6c\u5316\u4e3apb\u683c\u5f0f \u7136\u540e\u6784\u5efa\u81ea\u5df1\u7684\u7f51\u7edc\uff0c\u5e76 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/8.154.33.202\/?p=107\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201ctensorflow \u8bad\u7ec3\u7f51\u7edc\u7684\u4e00\u822c\u6b65\u9aa4\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[],"class_list":["post-107","post","type-post","status-publish","format-standard","hentry","category-tensorflow","category-4"],"_links":{"self":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts\/107"}],"collection":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=107"}],"version-history":[{"count":1,"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts\/107\/revisions"}],"predecessor-version":[{"id":108,"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts\/107\/revisions\/108"}],"wp:attachment":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}