{"id":669,"date":"2023-12-04T18:01:01","date_gmt":"2023-12-04T10:01:01","guid":{"rendered":""},"modified":"2023-12-04T18:01:01","modified_gmt":"2023-12-04T10:01:01","slug":"\u5982\u4f55\u7406\u89e3Axis\uff1f","status":"publish","type":"post","link":"https:\/\/mushiming.com\/669.html","title":{"rendered":"\u5982\u4f55\u7406\u89e3Axis\uff1f"},"content":{"rendered":"
\u524d\u8a00<\/strong><\/p>\n \u4e0d\u77e5\u9053\u5927\u5bb6\u6700\u5f00\u59cb\u63a5\u89e6\u5230axis\u7684\u65f6\u5019\u662f\u600e\u4e48\u6837\u7684\uff0c\u53cd\u6b63\u6211\u662f\u633a\u96be\u7406\u89e3\u7684..\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0TensorFlow\u7684\u5f88\u591aAPI\u90fd\u6709axis\u8fd9\u4e2a\u53c2\u6570\uff0c\u5982\u679c\u6211\u4eec\u5bf9axis\u4e0d\u4e86\u89e3\uff0c\u538b\u6839\u4e0d\u77e5\u9053API\u662f\u600e\u4e48\u641e\u7684\u3002<\/p>\n \u4e00\u53e5\u8bdd\u603b\u7ed3axis\uff1aaxis\u53ef\u4ee5\u65b9\u4fbf\u6211\u4eec\u5c06\u6570\u636e\u8fdb\u884c\u4e0d\u540c\u7ef4\u5ea6\u7684\u5904\u7406<\/strong>\u3002<\/p>\n<\/blockquote>\n \u4e00\u3001\u7406\u89e3axis<\/strong><\/p>\n \u5982\u679c\u4f60\u50cf\u6211\u4e00\u6837\uff0c\u53d1\u73b0API\u4e2d\u6709axis\u8fd9\u4e2a\u53c2\u6570\uff0c\u4f46\u4e0d\u77e5\u9053\u662f\u4ec0\u4e48\u610f\u601d\u3002\u53ef\u80fd\u5c31\u4f1a\u641c\u641caxis\u5230\u5e95\u4ee3\u8868\u7684\u4ec0\u4e48\u610f\u601d\u3002\u4e8e\u662f\u53ef\u80fd\u4f1a\u7c7b\u4f3c\u641c\u5230\u4e0b\u9762\u7684\u4fe1\u606f\uff1a<\/p>\n \u4f7f\u75280\u503c\u8868\u793a\u6cbf\u7740\u6bcf\u4e00\u5217\u6216\u884c\u6807\u7b7e\\\u7d22\u5f15\u503c\u5411\u4e0b\u6267\u884c\u65b9\u6cd5(axis=0\u4ee3\u8868\u5f80\u8de8\u884c<\/strong>)<\/p>\n \u4f7f\u75281\u503c\u8868\u793a\u6cbf\u7740\u6bcf\u4e00\u884c\u6216\u8005\u5217\u6807\u7b7e\u6a21\u5411\u6267\u884c\u5bf9\u5e94\u7684\u65b9\u6cd5(axis=1\u4ee3\u8868\u8de8\u5217<\/strong>)<\/p>\n<\/blockquote>\n \u4f46\u6211\u4eec\u53c8\u77e5\u9053\uff0c\u6211\u4eec\u7684\u6570\u7ec4\u4e0d\u5355\u5355\u53ea\u6709\u4e8c\u7ef4\u7684\uff0c\u8fd8\u6709\u4e09\u7ef4\u3001\u56db\u7ef4\u7b49\u7b49\u3002\u4e00\u65e6\u7ef4\u6570\u8d85\u8fc7\u4e8c\u7ef4\uff0c\u5c31\u65e0\u6cd5\u7528\u7b80\u5355\u7684\u884c\u548c\u5217\u6765\u8868\u793a\u4e86<\/strong>\u3002<\/p>\n \u6240\u4ee5\uff0c\u53ef\u4ee5\u7528\u6211\u4e0b\u9762\u7684\u65b9\u5f0f\u8fdb\u884c\u7406\u89e3\uff1a<\/p>\n \u8bdd\u4e0d\u591a\u8bf4\uff0c\u4e0b\u9762\u4ee5\u4f8b\u5b50\u8bf4\u660e~<\/p>\n 1.1\u4e8c\u7ef4\u6570\u7ec4\u4e4bconcat<\/strong><\/p>\n \u9996\u5148\uff0c\u6211\u4eec\u6765\u770b\u4e2aconcat\u7684\u4f8b\u5b50\uff0cconcat\u7b2c\u4e00\u4e2a\u53c2\u6570\u63a5\u6536val\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u63a5\u6536\u7684\u662faxis<\/p>\n ok\uff0c\u4e0b\u9762\u4ee5\u56fe\u793a<\/strong>\u7684\u65b9\u5f0f\u6765\u8bf4\u660e\u3002\u73b0\u5728\u6211\u4eec\u6709\u4e24\u4e2a\u6570\u7ec4\uff0c\u5206\u522b\u662ft1\u548ct2\uff1a<\/p>\n \u9996\u5148\uff0c\u6211\u4eec\u5148\u770baxis=0\u7684\u60c5\u51b5\uff0c\u4e5f\u5c31\u662ftf.concat([t1, t2], 0)\u3002\u4ece\u4e0a\u9762\u7684\u63cf\u8ff0\uff0c\u6211\u4eec\u77e5\u9053\uff0c\u5148\u628a\u7b2c\u4e00\u4e2a\u62ec\u53f7\u53bb\u9664\uff0c\u7136\u540e\u5c06\u5176\u5b50\u5185\u5bb9\u770b\u6210\u4e00\u4e2a\u6574\u4f53\uff0c\u5728\u8fd9\u4e2a\u6574\u4f53\u4e0b\u8fdb\u884c\u60f3\u5bf9\u5e94\u7684\u8fd0\u7b97(\u8fd9\u91cc\u6211\u4eec\u5c31\u662fconcat)\u3002<\/p>\n \u6240\u4ee5\u6700\u7ec8\u7684\u7ed3\u679c\u662f\uff1a<\/p>\n \u63a5\u7740\uff0c\u6211\u4eec\u518d\u770baxis=1\u7684\u60c5\u51b5\uff0c\u4e5f\u5c31\u662ftf.concat([t1, t2], 1)\u3002\u4ece\u4e0a\u9762\u7684\u63cf\u8ff0\uff0c\u6211\u4eec\u77e5\u9053\uff0c\u5148\u628a\u7b2c\u4e8c\u4e2a\u62ec\u53f7\u53bb\u9664\uff0c\u7136\u540e\u5c06\u5176\u5b50\u5185\u5bb9\u770b\u6210\u4e00\u4e2a\u6574\u4f53\uff0c\u5728\u8fd9\u4e2a\u6574\u4f53\u4e0b\u8fdb\u884c\u60f3\u5bf9\u5e94\u7684\u8fd0\u7b97(\u8fd9\u91cc\u6211\u4eec\u5c31\u662fconcat)\u3002<\/p>\n \u6240\u4ee5\u6700\u7ec8\u7684\u7ed3\u679c\u662f\uff1a<\/p>\n 1.2\u4e09\u7ef4\u6570\u7ec4\u4e4bconcat<\/strong><\/p>\n \u63a5\u4e0b\u6765\u6211\u4eec\u770b\u4e00\u4e0b\u4e09\u7ef4\u7684\u60c5\u51b5<\/p>\n ok\uff0c\u4e0b\u9762\u4e5f\u4ee5\u56fe\u793a<\/strong>\u7684\u65b9\u5f0f\u6765\u8bf4\u660e\u3002\u73b0\u5728\u6211\u4eec\u6709\u4e24\u4e2a\u6570\u7ec4\uff0c\u5206\u522b\u662ft3\u548ct4\uff1a<\/p>\n \u9996\u5148\uff0c\u6211\u4eec\u5148\u770baxis=0\u7684\u60c5\u51b5\uff0c\u4e5f\u5c31\u662ftf.concat([t3, t4], 0)\u3002\u4ece\u4e0a\u9762\u7684\u63cf\u8ff0\uff0c\u6211\u4eec\u77e5\u9053\uff0c\u5148\u628a\u7b2c\u4e00\u4e2a\u62ec\u53f7\u53bb\u9664\uff0c\u7136\u540e\u5c06\u5176\u5b50\u5185\u5bb9\u770b\u6210\u4e00\u4e2a\u6574\u4f53\uff0c\u5728\u8fd9\u4e2a\u6574\u4f53\u4e0b\u8fdb\u884c\u60f3\u5bf9\u5e94\u7684\u8fd0\u7b97(\u8fd9\u91cc\u6211\u4eec\u5c31\u662fconcat)\u3002<\/p>\n \u6240\u4ee5\u6700\u7ec8\u7684\u7ed3\u679c\u662f\uff1a<\/p>\n \u63a5\u7740\uff0c\u6211\u4eec\u518d\u770baxis=1\u7684\u60c5\u51b5\uff0c\u4e5f\u5c31\u662ftf.concat([t3, t4], 1)\u3002\u4ece\u4e0a\u9762\u7684\u63cf\u8ff0\uff0c\u6211\u4eec\u77e5\u9053\uff0c\u5148\u628a\u7b2c\u4e8c\u4e2a\u62ec\u53f7\u53bb\u9664\uff0c\u7136\u540e\u5c06\u5176\u5b50\u5185\u5bb9\u770b\u6210\u4e00\u4e2a\u6574\u4f53\uff0c\u5728\u8fd9\u4e2a\u6574\u4f53\u4e0b\u8fdb\u884c\u60f3\u5bf9\u5e94\u7684\u8fd0\u7b97(\u8fd9\u91cc\u6211\u4eec\u5c31\u662fconcat)\u3002<\/p>\n \u6240\u4ee5\u6700\u7ec8\u7684\u7ed3\u679c\u662f\uff1a<\/p>\n \u6700\u540e\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e0baxis=-1\u8fd9\u79cd\u60c5\u51b5\uff0c\u5728\u6587\u6863\u4e5f\u6709\u76f8\u5173\u7684\u4ecb\u7ecd\uff1a<\/p>\n As in Python, the axis could also be negative numbers. Negative axis<\/p>\n are interpreted as counting from the end of the rank, i.e.,<\/p>\n axis + rank(values)-th dimension<\/p>\n<\/blockquote>\n \u6240\u4ee5\uff0c\u5bf9\u4e8e\u6211\u4eec\u4e09\u7ef4\u7684\u6570\u7ec4\u800c\u8a00\uff0c\u90a3axis=-1\u5b9e\u9645\u4e0a\u5c31\u662faxis=2\uff0c\u4e0b\u9762\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e0b\u8fd9\u79cd\u60c5\u51b5\uff1a<\/p>\n \u6700\u7ec8\u7684\u7ed3\u679c\u662f\uff1a<\/p>\n \u9664\u4e86concat\u4ee5\u5916\uff0c\u5176\u5b9e\u5f88\u591a\u51fd\u6570\u90fd\u7528\u5230\u4e86axis\u8fd9\u4e2a\u53c2\u6570\uff0c\u518d\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n
\n
\n
\n
\ndef learn_concat():\n # \u4e8c\u7ef4\u6570\u7ec4\n t1 = tf.constant([[1, 2, 3], [4, 5, 6]])\n t2 = tf.constant([[7, 8, 9], [10, 11, 12]])\n \n with tf.Session() as sess:\n \n # \u4e8c\u7ef4\u6570\u7ec4\u9488\u5bf9 axis \u4e3a0 \u548c 1 \u7684\u60c5\u51b5\n print(sess.run(tf.concat([t1, t2], 0)))\n print(sess.run(tf.concat([t1, t2], 1)))\n<\/pre>\n
\n[\n [1 2 3],\n [4 5 6],\n [7 8 9],\n [10 11 12]\n]\n<\/pre>\n
\n[\n [1, 2, 3, 7, 8, 9]\n [4, 5, 6, 10, 11, 12]\n]\n<\/pre>\n
\ndef learn_concat():\n # \u4e09\u7ef4\u6570\u7ec4 \n t3 = tf.constant([[[1, 2], [2, 3]], [[4, 4], [5, 3]]])\n t4 = tf.constant([[[7, 4], [8, 4]], [[2, 10], [15, 11]]])\n with tf.Session() as sess:\n # \u4e09\u7ef4\u6570\u7ec4\u9488\u5bf9 axis \u4e3a0 \u548c 1 \u548c -1 \u7684\u60c5\u51b5\n print(sess.run(tf.concat([t3, t4], 0)))\n print(sess.run(tf.concat([t3, t4], 1)))\n print(sess.run(tf.concat([t3, t4], -1)))\n<\/pre>\n
\n[\n [\n [1 2]\n [2 3]\n ]\n [\n [4 4]\n [5 3]\n ]\n [\n [7 4]\n [8 4]\n ]\n [\n [2 10]\n [15 11]\n ]\n]\n<\/pre>\n
\n[\n [\n [1 2]\n [2 3]\n [7 4]\n [8 4]\n ]\n [\n [4 4]\n [5 3]\n [2 10]\n [15 11]\n ]\n]\n<\/pre>\n
\n
\n[\n [\n [1 2 7 4]\n [2 3 8 4]\n ]\n [\n [4 4 2 10]\n [5 3 15 11]\n ]\n]\n<\/pre>\n
\n>>> item = np.array([[1,4,8],[2,3,5],[2,5,1],[1,10,7]])\n>>> item\narray([[1, 4, 8],\n [2, 3, 5],\n [2, 5, 1],\n [1, 10, 7]])\n>>> item.sum(axis = 1)\narray([13, 10, 8, 18])\n>>> item.sum(axis = 0)\narray([ 6, 22, 21])\n<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7406\u89e3Axis\uff1f\u4e8e\u662f\u53ef\u80fd\u4f1a\u7c7b\u4f3c\u641c\u5230\u4e0b\u9762\u7684\u4fe1\u606f\uff1a\u4f7f\u75280\u503c\u8868\u793a\u6cbf\u7740\u6bcf\u4e00\u5217\u6216\u884c\u6807\u7b7e\\\u7d22\u5f15\u503c\u5411\u4e0b\u6267\u884c\u65b9\u6cd5\u4f7f\u75281\u503c\u8868\u793a\u6cbf\u7740\u6bcf\u4e00\u884c\u6216\u8005...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/669"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=669"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/669\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}