{"id":1247,"date":"2023-04-16T20:17:36","date_gmt":"2023-04-16T12:17:36","guid":{"rendered":"http:\/\/www.wayln.com\/?p=1247"},"modified":"2023-04-19T13:37:47","modified_gmt":"2023-04-19T05:37:47","slug":"tf-data%e6%95%b0%e6%8d%ae%e9%9b%86%e5%88%9b%e5%bb%ba%e4%b8%8e%e9%a2%84%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=1247","title":{"rendered":"tf.data\u6570\u636e\u96c6"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_transparent no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#tfdata\"><span class=\"toc_number toc_depth_1\">1<\/span> tf.data\u6570\u636e\u96c6\u521b\u5efa<\/a><ul><li><a href=\"#i\"><span class=\"toc_number toc_depth_2\">1.1<\/span> \u6570\u636e\u96c6\u7684\u521b\u5efa\u4e3b\u8981\u6709\u4ee5\u4e0b\u65b9\u5f0f<\/a><\/li><\/ul><\/li><li><a href=\"#tfdata-2\"><span class=\"toc_number toc_depth_1\">2<\/span> tf.data\u6570\u636e\u9884\u5904\u7406<\/a><\/li><li><a href=\"#tfdata-3\"><span class=\"toc_number toc_depth_1\">3<\/span> tf.data\u6570\u636e\u4f7f\u7528<\/a><\/li><li><a href=\"#TensorFlowNet_Keras3\"><span class=\"toc_number toc_depth_1\">4<\/span> TensorFlow.Net Keras\u6a21\u578b\u642d\u5efa\u76843\u79cd\u65b9\u5f0f<\/a><ul><li><a href=\"#Sequential_API\"><span class=\"toc_number toc_depth_2\">4.1<\/span> Sequential API<\/a><\/li><li><a href=\"#Functional_API\"><span class=\"toc_number toc_depth_2\">4.2<\/span> Functional API<\/a><\/li><li><a href=\"#Model_Subclassing\"><span class=\"toc_number toc_depth_2\">4.3<\/span> Model Subclassing<\/a><\/li><\/ul><\/li><li><a href=\"#DNN_with_Keras\"><span class=\"toc_number toc_depth_1\">5<\/span> DNN with Keras<\/a><\/li><\/ul><\/div>\n\n<h1><span id=\"tfdata\">tf.data\u6570\u636e\u96c6\u521b\u5efa<\/span><\/h1>\n<p>tf.data\u7684\u6838\u5fc3\u662ftf.data.Dataset\u7c7b\uff0c\u63d0\u4f9b\u4e86\u5bf9\u6570\u636e\u96c6\u7684\u9ad8\u5c42\u5c01\u88c5\u80fd\u529b\u3002tf.data.Dataset\u7531\u4e00\u7cfb\u5217\u7684\u53ef\u8fed\u4ee3\u8bbf\u95ee\u7684\u5143\u7d20\u7ec4\u6210\uff0c\u6bcf\u4e2a\u5143\u7d20\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u5f20\u91cf<\/p>\n<h2><span id=\"i\">\u6570\u636e\u96c6\u7684\u521b\u5efa\u4e3b\u8981\u6709\u4ee5\u4e0b\u65b9\u5f0f<\/span><\/h2>\n<ol>\n<li>tf.data.Dataset.from_tensor_slices<br \/>\n\u6700\u57fa\u7840\u7684\u6570\u636e\u96c6\u521b\u5efa\u65b9\u5f0f\uff0c\u4e5f\u662f\u6700\u5e8a\u7528\u548c\u63a8\u8350\u65b9\u5f0f<\/li>\n<li>tf.data.Dataset.from_generator<br \/>\n\u4f7f\u7528\u751f\u6210\u5668\u6765\u521d\u59cb\u5316\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u7075\u6d3b\u7684\u5904\u7406\u957f\u5ea6\u4e0d\u540c\u7684\u5143\u7d20<\/li>\n<li>tf.data.TFRecordDataset<br \/>\n\u8fd9\u662f\u4e00\u79cd\u4eceTFRecord\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u7684\u63a5\u53e3<\/li>\n<li>tf.data.TextLineDataset<br \/>\n\u9002\u7528\u4e8e\u6587\u672c\u6570\u636e\u8f93\u5165\u7684\u573a\u666f<\/li>\n<li>tf.data.experimental.makecsvdataset<br \/>\n\u9002\u7528\u4e8eCSV\u683c\u5f0f\u6570\u636e\u8f93\u5165\u7684\u573a\u666f<\/li>\n<\/ol>\n<h1><span id=\"tfdata-2\">tf.data\u6570\u636e\u9884\u5904\u7406<\/span><\/h1>\n<ol>\n<li>Dataset.batch()\u6570\u636e\u5206\u6279<br \/>\n\u4f7f\u7528\u8be5\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u636e\u5212\u5206\u4e3a\u56fa\u5b9a\u5927\u5c0f\u7684\u6279\u6b21\uff0c\u65b9\u4fbf\u6a21\u578b\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u6570\u636e\u91c7\u96c6Batch\u7684\u65b9\u5f0f\u8ba1\u7b97\u68af\u5ea6\u548c\u66f4\u65b0\u53c2\u6570<\/li>\n<li>Dataset.shuffle()\u6570\u636e\u4e71\u5e8f<br \/>\n\u53ef\u4ee5\u5c06\u6570\u636e\u7684\u987a\u5e8f\u968f\u673a\u6253\u4e71\uff0c\u540c\u65f6\u7ec4\u5408Dataset.batch()\u65b9\u6cd5\uff0c\u5c06\u6570\u636e\u4e71\u5e8f\u540e\u8bbe\u7f6e\u6279\u6b21\uff0c\u53ef\u4ee5\u6d88\u9664\u6570\u636e\u95f4\u7684\u987a\u5e8f\u5173\u8054\uff0c\u5728\u8bad\u7ec3\u5e08\u975e\u5e38\u5e38\u7528<br \/>\nDataset.shuffle()\u65b9\u6cd5\u7684\u4e3b\u8981\u53c2\u6570\u662fbuffer_size<\/li>\n<li>Dataset.repeat()\u6570\u636e\u590d\u5236<br \/>\n\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u590d\u5236\uff0c\u4ed6\u7684\u53c2\u6570count\u8868\u793a\u590d\u5236\u7684\u500d\u6570\uff0c\u9ed8\u8ba4count=-1\u4e3a\u65e0\u9650\u500d\u6570\u7684\u8d4b\u503c<\/li>\n<li>Dataset.prefetch()\u6570\u636e\u9884\u53d6\u51fa\uff08\u5e76\u884c\u5316\u7b56\u7565\uff09<br \/>\n\u53ef\u4ee5\u8fdb\u884c\u5e76\u884c\u5316\u8bfb\u53d6\u6570\u636e\uff0c\u5145\u5206\u5229\u7528\u8ba1\u7b97\u8d44\u6e90\uff0c\u51cf\u5c11CPU\u6570\u636e\u52a0\u8f7d\u548cGPU\u6570\u636e\u8bad\u7ec3\u65f6\u95f4\u4e4b\u95f4\u7684\u5207\u6362\u7a7a\u8f7d\u65f6\u95f4<\/li>\n<li>Dataset.map()<br \/>\nDataset.map(f)\u5bf9\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u51fd\u6570f(\u81ea\u5b9a\u4e49\u51fd\u6570Function\u6216Lambda\u8868\u8fbe\u5f0f)\uff0c\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u96c6<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_5159b32fcfdd109479b7a0179084c1b2.jpg\"><img decoding=\"async\" src=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_5159b32fcfdd109479b7a0179084c1b2.jpg\" alt=\"\" \/><\/a><\/li>\n<\/ol>\n<h1><span id=\"tfdata-3\">tf.data\u6570\u636e\u4f7f\u7528<\/span><\/h1>\n<ol>\n<li>\u76f4\u63a5for\u5faa\u73af<\/li>\n<li>Dataset.take()\u53d6\u51fa\u90e8\u5206\u6216\u5168\u90e8\u6570\u636e<\/li>\n<li>\u76f4\u63a5\u8f93\u5165\u81f3Keras\u4f7f\u7528<\/li>\n<\/ol>\n<h1><span id=\"TensorFlowNet_Keras3\">TensorFlow.Net Keras\u6a21\u578b\u642d\u5efa\u76843\u79cd\u65b9\u5f0f<\/span><\/h1>\n<ol>\n<li>Sequential API(\u5e8f\u5217\u5f0fAPI)\uff1a\u6309\u5c42\u987a\u5e8f\u642d\u5efa\u6a21\u578b<\/li>\n<li>Functional API(\u51fd\u6570\u5f0fAPI)\uff1a\u51fd\u6570\u5f0fAPI\u642d\u5efa\u4efb\u610f\u7ed3\u6784<\/li>\n<li>Model Subclassing(\u6a21\u578b\u5b50\u7c7b\u5316)\uff1a \u6a21\u578b\u5b50\u7c7b\u5316\u642d\u5efa\u81ea\u5b9a\u4e49\u6a21\u578b<\/li>\n<\/ol>\n<p>\u4f18\u5148\u4f7f\u7528Sequential API\u8fdb\u884c\u6a21\u578b\u7684\u5feb\u901f\u642d\u5efa\uff1b\u5982\u679c\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\uff08\u5171\u4eab\u7f51\u7edc\u5c42\u6216\u591a\u8f93\u5165\u7b49\uff09\uff0c\u5219\u8003\u8651\u4f7f\u7528Functional API\u642d\u5efa\u81ea\u5b9a\u4e49\u7ed3\u6784\u7684\u6a21\u578b\uff1b\u5982\u679c\u4ecd\u7136\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\uff08\u9700\u8981\u81ea\u5b9a\u4e49\u63a7\u5236\u8bad\u7ec3\u8fc7\u7a0b\u6216\u7814\u53d1\u521b\u65b0\u60f3\u6cd5\uff09\uff0c\u5219\u53ef\u4ee5\u8003\u8651Model Subclassing.<\/p>\n<h2><span id=\"Sequential_API\">Sequential API<\/span><\/h2>\n<p>\u662fKeras\u6700\u7b80\u5355\u7684\u6a21\u578b\u642d\u5efa\u65b9\u5f0f\uff0c\u5b83\u5148\u987a\u5e8f\u5730\u628a\u6240\u6709\u6a21\u578b\u5c42\u6b21\u4f9d\u6b21\u5b9a\u4e49\uff0c\u518d\u4f7f\u7528\u5185\u7f6e\u7684\u8bad\u7ec3\u5faa\u73afmodel.fit\u5bf9\u6a21\u578b\u8fdb\u884c\u8bad\u7ec3\u3002<br \/>\n\u4f46\u662fSequential API\u662fLayer-by-Layer\u7684\uff0c\u5bf9\u67d0\u4e9b\u573a\u666f\u7684\u4f7f\u7528\u7565\u6709\u9650\u5236<br \/>\n&#8211; \u65e0\u6cd5\u5171\u4eab\u7f51\u7edc\u5c42<br \/>\n&#8211; \u4e0d\u80fd\u521b\u5efa\u591a\u5206\u652f\u7ed3\u6784<br \/>\n&#8211; \u4e0d\u80fd\u6709\u591a\u4e2a\u8f93\u5165<br \/>\n\u8fd9\u79cd\u65b9\u5f0f\u7279\u522b\u9002\u7528\u4e8e\u7ecf\u5178\u7f51\u7edc\u6a21\u578bLeNet,AlexNet,VGGNet<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_6b2ac81d75c9fbc5593bed270f0e8e7d.jpg\"><img decoding=\"async\" src=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_6b2ac81d75c9fbc5593bed270f0e8e7d.jpg\" alt=\"\" \/><\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_df4c1345ecf8474365506ade354bf9e9.jpg\"><img decoding=\"async\" src=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_df4c1345ecf8474365506ade354bf9e9.jpg\" alt=\"\" \/><\/a><\/p>\n<pre><code class=\"language-CSharp line-numbers\">            var model =keras.Sequential();\n            model.add(keras.Input(shape: 16));\n            model.add(keras.layers.Dense(32, activation: keras.activations.Relu));\n            \/\/\u73b0\u5728\uff0c\u6a21\u578b\u7684\u8f93\u5165\u6570\u503c\u7684\u5f62\u72b6\u8bbe\u7f6e\u4e3a\uff08none,16)\n            \/\/\u6a21\u578b\u7684\u8f93\u51fa\u6570\u7ec4\u7684\u5f62\u72b6\u8bbe\u7f6e\u4e3a\uff08None,32)\n            \/\/\u5728\u7b2c\u4e00\u5c42\u4e4b\u540e\uff0c\u4e0d\u9700\u8981\u6307\u5b9a\u8f93\u5165\u7684\u5927\u5c0f\uff0cKeras\u5185\u90e8\u4f1a\u52a8\u8fdb\u884c\u4e0a\u4e0b\u6587\u9002\u914d\n            model.add(keras.layers.Dense(32));\n            Assert.AreEqual((-1, 32), model.output_shape);\n<\/code><\/pre>\n<h2><span id=\"Functional_API\">Functional API<\/span><\/h2>\n<p>\u7b80\u5355\u7684Sequential API\u65b9\u5f0f\u6709\u65f6\u5019\u4e0d\u80fd\u642d\u5efa\u4efb\u610f\u7ed3\u6784\u7684\u795e\u7ecf\u7f51\u7edc\u3002\u4e3a\u6b64\uff0cKeras\u63d0\u4f9b\u4e86Functional API,\u5e2e\u52a9\u6211\u4eec\u5efa\u7acb\u66f4\u590d\u6742\u548c\u7075\u6d3b\u7684\u6a21\u578b\u3002Functional API\u53ef\u4ee5\u5904\u7406\u975e\u7ebf\u6027\u62d3\u6251\u3001\u5177\u6709\u5171\u4eab\u5c42\u7684\u6a21\u578b\u548c\u5177\u6709\u591a\u4e2a\u8f93\u5165\u6216\u8f93\u51fa\u7684\u6a21\u578b\u3002Functional API\u7684\u4f7f\u7528\u65b9\u6cd5\u662f\u5c06\u5c42\u4f5c\u4e3a\u53ef\u8c03\u7528\u7684\u5bf9\u8c61\u5e76\u8fd4\u56de\u5f20\u91cf\uff0c\u5e76\u5c06\u8f93\u5165\u5411\u91cf\u548c\u8f93\u51fa\u5411\u91cf\u63d0\u4f9b\u7ed9Model\u7684inputs\u548coutputs\u53c2\u6570\u3002<br \/>\nFunctional API\u6709\u5982\u4e0b\u529f\u80fd\u3002<br \/>\n&#8211; \u5b9a\u4e49\u66f4\u590d\u6742\u7684\u6a21\u578b<br \/>\n&#8211; \u652f\u6301\u591a\u8f93\u5165\u548c\u591a\u8f93\u51fa<br \/>\n&#8211; \u53ef\u4ee5\u5b9a\u4e49\u6a21\u578b\u5206\u652f\uff0c\u5982Inception Block \u3001ResNet Block<br \/>\n&#8211; \u65b9\u4fbf\u5c42\u5171\u4eab<\/p>\n<p>Functional API\u65b9\u5f0f\u7279\u522b\u9002\u7528\u4e8e\u4e00\u4e9b\u590d\u6742\u7684\u7f51\u7edc\u6a21\u578b\uff0c\u5982ResNet\u3001GoogleNet\/Inception\u3001Xception\u3001SqueezeNet\u7b49<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_a5cea99f4aee94345df496bc07d3ff5e.jpg\"><img decoding=\"async\" src=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_a5cea99f4aee94345df496bc07d3ff5e.jpg\" alt=\"\" \/><\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_5c3c90936c511517486275de7b8cf3d5.jpg\"><img decoding=\"async\" src=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_5c3c90936c511517486275de7b8cf3d5.jpg\" alt=\"\" \/><\/a><\/p>\n<pre><code class=\"language-CSharp line-numbers\"> Model model;\n            NDArray x_train,y_train,x_test,y_test;\n            LayersApi layers = new LayersApi();\n\n            (x_train, y_train, x_test, y_test) = keras.datasets.mnist.load_data();\n            x_train = x_train.reshape((60000,784)) \/ 255f;\n            x_test = x_test.reshape((10000, 784)) \/ 255f;\n\n            \/\/\u642d\u5efa\u6a21\u578b\n            var inputs = keras.Input(shape: 784);\/\/\u8f93\u5165\u5c42\n            var outputs = layers.Dense(64, activation: keras.activations.Relu).Apply(inputs);\/\/\u7b2c\u4e00\u4e2a\u5168\u8fde\u63a5\u5c42\n            outputs = layers.Dense(64, activation: keras.activations.Relu).Apply(outputs);\/\/\u7b2c\u4e8c\u4e2a\u5168\u8fde\u63a5\u5c42\n            outputs=layers.Dense(10).Apply(outputs);\/\/\u8f93\u51fa\u5c42\n            model = keras.Model(inputs, outputs, name: \"mnist_model\");\/\/\u642d\u5efakeras\u6a21\u578b\n            model.summary();\/\/\u663e\u793a\u6a21\u578b\u6982\u51b5\n            model.compile(loss: keras.losses.SparseCategoricalCrossentropy(from_logits: true), optimizer: keras.optimizers.RMSprop(), metrics: new[] { \"accuracy\" });\/\/\u5c06keras DNN\u6a21\u578b\u7f16\u8bd1\u6210TensorFlow\u7684\u9759\u6001\u56fe\n\n            \/\/\u4f7f\u7528\u8f93\u5165\u6570\u636e\u548c\u6807\u7b7e\u6765\u8bad\u7ec3\u6a21\u578b\n            model.fit(x_train,y_train,batch_size:64,epochs: 2,validation_split:0.2f);\n\n            \/\/\u8bc4\u4f30\u6a21\u578b\n            model.evaluate(x_test, y_test, verbose: 2);\n\n            \/\/\u5e8f\u5217\u5316\u4fdd\u5b58\u6a21\u578b\n            model.save(\"mnist_model\");\n<\/code><\/pre>\n<h2><span id=\"Model_Subclassing\">Model Subclassing<\/span><\/h2>\n<p>\u901a\u8fc7\u8be5\u6a21\u578b\uff0c\u53ef\u4ee5\u642d\u5efa\u81ea\u5b9a\u4e49\u6a21\u578b\uff0c\u4e3b\u8981\u6c1b\u56f4\u81ea\u5b9a\u4e49\u5c42\uff08\u53ef\u4ee5\u7ee7\u627fLayer\u7c7b\uff09\u3001\u81ea\u5b9a\u4e49\u635f\u5931\u51fd\u6570\uff08\u53ef\u4ee5\u7ee7\u627fLoss\u7c7b\uff09\u548c\u81ea\u5b9a\u4e49\u8bc4\u4f30\u51fd\u6570\uff08\u53ef\u4ee5\u7ee7\u627fMetric\u7c7b\uff09<br \/>\n\u4ece\u5f00\u53d1\u4eba\u5458\u89d2\u5ea6\uff0c\u8fd9\u79cd\u5de5\u4f5c\u65b9\u5f0f\u5148\u6269\u5c55\u6846\u67b6\u5b9a\u4e49\u7684\u6a21\u578b\u7c7b\uff0c\u5b9e\u4f8b\u5316\u5c42\uff0c\u518d\u7f16\u5199\u6a21\u578b\u7684\u6b63\u5411\u4f20\u64ad\u8fc7\u7a0b\u3002\u901a\u8fc7Keras Subclassing API\u652f\u6301\u8fd9\u79cd\u5f00\u7bb1\u5373\u7528\u7684\u65b9\u5f0f\u3002\u5728Keras\u4e2d\uff0c\u6a21\u578b\u7c7b\u662f\u57fa\u672c\u7c7b\uff0c\u53ef\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u8fdb\u884c\u4efb\u610f\u7684\u81ea\u5b9a\u4e49\u64cd\u4f5c\uff0c\u5bf9\u6a21\u578b\u7684\u6240\u6709\u90e8\u5206\u8fdb\u884c\u63a7\u5236<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_d7b16d62ae8cbbf95456a262b4e7c1e5.jpg\"><img decoding=\"async\" src=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_d7b16d62ae8cbbf95456a262b4e7c1e5.jpg\" alt=\"\" \/><\/a><\/p>\n<p>\u81ea\u5b9a\u4e49\u6a21\u578b\u9700\u8981\u5148\u96c6\u6210Tesnorflow.Keras.Engine.Model\uff0c\u518d\u5728\u6784\u9020\u51fd\u6570\u4e2d\u521d\u59cb\u5316\u6a21\u578b\u6240\u9700\u8981\u7684\u5c42\uff08\u53ef\u4ee5\u4f7f\u7528Keras\u7684\u5c42\u6216\u7ee7\u627fLayer\u8fdb\u884c\u81ea\u5b9a\u4e49\u5c42\uff09\uff0c\u5e76\u91cd\u8f7dCall()\u65b9\u6cd5\u8fdb\u884c\u6a21\u578b\u7684\u8c03\u7528\uff0c\u5efa\u7acb\u8f93\u5165\u548c\u8f93\u51fa\u4e4b\u95f4\u7684\u51fd\u6570\u5173\u7cfb\u3002<\/p>\n<pre><code class=\"language-CSharp line-numbers\">\/\/\u6f14\u793a\u7528\uff0c\u4e0d\u80fd\u8fd0\u884c\n    internal class MyModel:Model\n    {\n        Layer myLayer1, myLayer2, output;\n        public MyModel(ModelArgs args): base(args) {\n\n            \/\/\u7b2c\u4e00\u5c42\n            myLayer1 = Layer.xxx;\n            \/\/\u7b2c\u4e8c\u5c42\n            myLayer2 = Layer.XXX;\n            output = Layer.xxx;\n        }\n\n        \/\/\u914d\u7f6e\u524d\u5411\u4f20\u64ad\u903b\u8f91\n        protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null)\n        {\n            inputs = myLayer1.Apply(inputs);\n            inputs = myLayer2.Apply(inputs);\n            inputs = output.Apply(inputs);\n            return inputs;\n        }\n    }\n<\/code><\/pre>\n<p>\u901a\u8fc7Model Subclassing\u65b9\u5f0f\u81ea\u5b9a\u4e49\u7684\u6a21\u578b\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528Sequential API\u6216Functional API\u65b9\u5f0f\uff0c\u5176\u4e2d\u5b9a\u4e49\u7684\u5c42\u9700\u8981\u6dfb\u52a0get_config\u65b9\u6cd5\u4ee5\u5e8f\u5217\u5316\u7ec4\u5408\u6a21\u578b<\/p>\n<h1><span id=\"DNN_with_Keras\">DNN with Keras<\/span><\/h1>\n<p>Keras\u65b9\u5f0f\u7684DNN\u6a21\u578b\u642d\u5efa\u6d41\u7a0b\u548cEager\u65b9\u5f0f\u7c7b\u4f3c\uff0c\u5dee\u5f02\u90e8\u5206\u4e3b\u8981\u5728\u4e8e\u4f7f\u7528Keras\u7684\u5168\u8fde\u63a5\u5c42(Dense\u5c42)\u66ff\u4ee3\u4e86Eager\u65b9\u5f0f\u4e2d\u7684\u201c\u7ebf\u6027\u53d8\u6362+\u6fc0\u6d3b\u51fd\u6570\u201d\u3002<\/p>\n<p>TensorFlow.Net2\u63a8\u8350\u4f7f\u7528Keras\u8fdb\u884c\u6a21\u578b\u642d\u5efa\u548c\u8bad\u7ec3\uff0cKeras\u662f\u4e00\u79cd\u9ad8\u7ea7\u795e\u7ecf\u7f51\u7edcAPI\uff0c\u53ef\u4ee5\u7b80\u5355\u3001\u5feb\u901f\u548c\u7075\u6d3b\u5730\u642d\u5efa\u7f51\u7edc\u6a21\u578b\u3002<br \/>\nKeras\u6709\u4e24\u4e2a\u6bd4\u8f83\u91cd\u8981\u7684\u6982\u5ff5\uff1a\u6a21\u578b\uff08Model\uff09\u548c\u5c42\uff08Layer\uff09\u3002\u5c42\u5c06\u5e38\u7528\u7684\u795e\u7ecf\u7f51\u7edc\u5c42\u8fdb\u884c\u5c01\u88c5\uff08\u5168\u8fde\u63a5\u5c42\u3001\u5377\u79ef\u5c42\u3001\u6c60\u5316\u5c42\u7b49\uff09\uff1b\u6a21\u578b\u5c06\u5404\u4e2a\u5c42\u8fdb\u884c\u8fde\u63a5\uff0c\u5e76\u5c01\u88c5\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u7f51\u7edc\u6a21\u578b\u3002\u5728\u8fdb\u884c\u6a21\u578b\u8c03\u7528\u7684\u65f6\u5019\uff0c\u4f7f\u7528y_pred=model(x)\u7684\u5f62\u5f0f\u5373\u53ef\u3002<br \/>\nKeras\u5728Tensorflow.Keras.Engine.Layer\u4e0b\u5185\u7f6e\u4e86\u6df1\u5ea6\u5b66\u4e60\u4e2d\u5e38\u7528\u7684\u7f51\u7edc\u5c42\uff0c\u540c\u65f6\u652f\u6301\u7ee7\u627f\u5e76\u81ea\u5b9a\u4e49\u5c42\u3002<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_1a648360d2c0409e4a94d9a7b823fb4f.jpg\"><img decoding=\"async\" src=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_1a648360d2c0409e4a94d9a7b823fb4f.jpg\" alt=\"\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 tf.data\u6570\u636e\u96c6\u521b\u5efa1.1 \u6570\u636e\u96c6\u7684\u521b\u5efa\u4e3b\u8981\u6709\u4ee5\u4e0b\u65b9\u5f0f2 tf.data\u6570\u636e\u9884\u5904\u74063  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50,49,2],"tags":[],"class_list":["post-1247","post","type-post","status-publish","format-standard","hentry","category-tensorflow","category-49","category-2"],"_links":{"self":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/1247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1247"}],"version-history":[{"count":15,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/1247\/revisions"}],"predecessor-version":[{"id":1306,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/1247\/revisions\/1306"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}