{"id":265328,"date":"2023-04-01T08:55:33","date_gmt":"2023-04-01T00:55:33","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=265328"},"modified":"2023-02-25T20:56:23","modified_gmt":"2023-02-25T12:56:23","slug":"tensorflow-linux-flags","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/tensorflow-linux-flags.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdTensorFlow\u4e2d\u5173\u4e8etf.app.flags\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u6a21\u5757"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86TensorFlow\u4e2d\u5173\u4e8etf.app.flags\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u6a21\u5757\uff0c\u5177\u6709\u5f88\u597d\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002\u5982\u6709\u9519\u8bef\u6216\u672a\u8003\u8651\u5b8c\u5168\u7684\u5730\u65b9\uff0c\u671b\u4e0d\u541d\u8d50\u6559<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
tf.app.flags\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u6a21\u5757<\/strong><\/div>\n

\u8bf4\u9053\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\uff0c\u5c31\u4e0d\u5f97\u4e0d\u63d0\u5230 python \u7684 argparse \u6a21\u5757\uff0c\u8be6\u60c5\u53ef\u53c2\u8003\u6211\u4e4b\u524d\u7684\u4e00\u7bc7\u6587\u7ae0\uff1apython argparse \u6a21\u5757\u547d\u4ee4\u884c\u53c2\u6570\u7528\u6cd5\u53ca\u8bf4\u660e\u3002<\/p>\n

\u5728\u9605\u8bfb\u76f8\u5173\u5de5\u7a0b\u7684\u6e90\u7801\u65f6\uff0c\u5f88\u5bb9\u6613\u53d1\u73b0 tf.app.flags \u6a21\u5757\u7684\u8eab\u5f71\u3002\u5176\u4f5c\u7528\u4e0e python \u7684 argparse \u7c7b\u4f3c\u3002<\/p>\n

\u76f4\u63a5\u4e0a\u4ee3\u7801\u5b9e\u4f8b\uff0c\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3a test_flags.py \u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n

#coding:utf-8\r\nimport tensorflow as tf\r\n \r\nFLAGS = tf.app.flags.FLAGS\r\n# tf.app.flags.DEFINE_string(\"param_name\", \"default_val\", \"description\")\r\ntf.app.flags.DEFINE_string(\"train_data_path\", \"\/home\/feige\", \"training data dir\")\r\ntf.app.flags.DEFINE_string(\"log_dir\", \".\/logs\", \" the log dir\")\r\ntf.app.flags.DEFINE_integer(\"train_batch_size\", 128, \"batch size of train data\")\r\ntf.app.flags.DEFINE_integer(\"test_batch_size\", 64, \"batch size of test data\")\r\ntf.app.flags.DEFINE_float(\"learning_rate\", 0.001, \"learning rate\")\r\n \r\ndef main(unused_argv):\r\n    train_data_path = FLAGS.train_data_path\r\n    print(\"train_data_path\", train_data_path)\r\n    train_batch_size = FLAGS.train_batch_size\r\n    print(\"train_batch_size\", train_batch_size)\r\n    test_batch_size = FLAGS.test_batch_size\r\n    print(\"test_batch_size\", test_batch_size)\r\n    size_sum = tf.add(train_batch_size, test_batch_size)\r\n    with tf.Session() as sess:\r\n        sum_result = sess.run(size_sum)\r\n        print(\"sum_result\", sum_result)\r\n \r\n# \u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u4fdd\u8bc1\u4e86\uff0c\u5982\u679c\u6b64\u6587\u4ef6\u88ab\u5176\u4ed6\u6587\u4ef6 import\u7684\u65f6\u5019\uff0c\u4e0d\u4f1a\u6267\u884cmain \u51fd\u6570\r\nif __name__ == '__main__':\r\n    tf.app.run()   # \u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u8c03\u7528main \u51fd\u6570 main(sys.argv)<\/pre>\n

\u4e0a\u8ff0\u4ee3\u7801\u5df2\u7ed9\u51fa\u8f83\u4e3a\u8be6\u7ec6\u7684\u6ce8\u91ca\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002<\/p>\n

\u8be5\u6587\u4ef6\u7684\u8c03\u7528\u793a\u4f8b\u4ee5\u53ca\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\u6240\u793a<\/strong><\/div>\n

\"\"<\/p>\n

\u5982\u679c\u9700\u8981\u4fee\u6539\u9ed8\u8ba4\u53c2\u6570\u7684\u503c\uff0c\u5219\u5728\u547d\u4ee4\u884c\u4f20\u5165\u81ea\u5b9a\u4e49\u53c2\u6570\u503c\u5373\u53ef\uff0c\u82e5\u5168\u90e8\u4f7f\u7528\u9ed8\u8ba4\u53c2\u6570\u503c\uff0c\u5219\u53ef\u76f4\u63a5\u5728\u547d\u4ee4\u884c\u8fd0\u884c\u8be5 python \u6587\u4ef6\u3002<\/p>\n

\u8bfb\u8005\u53ef\u80fd\u4f1a\u5bf9 tf.app.run() \u6709\u4e9b\u7591\u95ee\uff0c\u5728\u4e0a\u8ff0\u6ce8\u91ca\u4e2d\u4e5f\u6709\u6240\u89e3\u91ca\uff0c\u4f46\u8981\u771f\u6b63\u5f04\u6e05\u695a\u5176\u8fd0\u884c\u539f\u7406<\/p>\n

\u8fd8\u9700\u67e5\u9605\u5176\u6e90\u4ee3\u7801<\/strong><\/div>\n
def run(main=None, argv=None):\r\n  \"\"\"Runs the program with an optional 'main' function and 'argv' list.\"\"\"\r\n  f = flags.FLAGS\r\n \r\n  # Extract the args from the optional `argv` list.\r\n  args = argv[1:] if argv else None\r\n \r\n  # Parse the known flags from that list, or from the command\r\n  # line otherwise.\r\n  # pylint: disable=protected-access\r\n  flags_passthrough = f._parse_flags(args=args)\r\n  # pylint: enable=protected-access\r\n \r\n  main = main or sys.modules['__main__'].main\r\n \r\n  # Call the main function, passing through any arguments\r\n  # to the final program.\r\n  sys.exit(main(sys.argv[:1] + flags_passthrough))<\/pre>\n

flags_passthrough=f._parse_flags(args=args)\u8fd9\u91cc\u7684_parse_flags\u5c31\u662f\u6211\u4eectf.app.flags\u6e90\u7801\u4e2d\u7528\u6765\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u7684\u51fd\u6570\u3002<\/p>\n

\u6240\u4ee5\u8fd9\u4e00\u884c\u5c31\u662f\u89e3\u6790\u53c2\u6570\u7684\u529f\u80fd\uff1b<\/p>\n

\u4e0b\u9762\u4e24\u884c\u4ee3\u7801\u4e5f\u5c31\u662f tf.app.run \u7684\u6838\u5fc3\u610f\u601d\uff1a\u6267\u884c\u7a0b\u5e8f\u4e2d main \u51fd\u6570\uff0c\u5e76\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

\u8bf4\u9053\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\uff0c\u5c31\u4e0d\u5f97\u4e0d\u63d0\u5230 python \u7684 argparse \u6a21\u5757\uff0c\u8be6\u60c5\u53ef\u53c2\u8003\u6211\u4e4b\u524d\u7684\u4e00\u7bc7\u6587\u7ae0\uff1apyt […]<\/p>\n","protected":false},"author":668,"featured_media":121075,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-265328","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/265328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=265328"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/265328\/revisions"}],"predecessor-version":[{"id":265333,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/265328\/revisions\/265333"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/121075"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=265328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=265328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=265328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}