{"id":935274,"date":"2024-12-26T18:50:20","date_gmt":"2024-12-26T10:50:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/935274.html"},"modified":"2024-12-26T18:50:22","modified_gmt":"2024-12-26T10:50:22","slug":"python%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0neuralpy","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/935274.html","title":{"rendered":"python\u5982\u4f55\u6dfb\u52a0neuralpy"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072002\/6bf6d0da-8c1c-4640-955c-e340850d5f58.webp\" alt=\"python\u5982\u4f55\u6dfb\u52a0neuralpy\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u6dfb\u52a0NeuralPy\uff0c\u4f60\u9700\u8981\u901a\u8fc7\u5b89\u88c5NeuralPy\u5e93\u3001\u5bfc\u5165\u5e93\u5e76\u5f00\u59cb\u4f7f\u7528\u3001\u5b66\u4e60NeuralPy\u7684\u57fa\u672c\u529f\u80fd\u6765\u5b9e\u73b0\u3002<\/strong> \u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86NeuralPy\u5e93\uff0c\u8fd9\u662f\u4f7f\u7528\u6b64\u5e93\u7684\u57fa\u7840\u3002\u5176\u6b21\uff0c\u5bfc\u5165\u8be5\u5e93\u4ee5\u4fbf\u5728\u4f60\u7684Python\u4ee3\u7801\u4e2d\u4f7f\u7528\u5b83\u3002\u6700\u540e\uff0c\u901a\u8fc7\u5b66\u4e60NeuralPy\u7684\u57fa\u672c\u529f\u80fd\u548c\u4f7f\u7528\u65b9\u6cd5\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u4f60\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u9879\u76ee\u4e2d\u5b9e\u73b0\u5176\u529f\u80fd\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5NeuralPy\u5e93<\/p>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u4f7f\u7528NeuralPy\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\u3002NeuralPy\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u795e\u7ecf\u7f51\u7edc\u7684Python\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5316\u7684\u63a5\u53e3\u6765\u642d\u5efa\u548c\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u3002\u8981\u5b89\u88c5NeuralPy\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177<code>pip<\/code>\u3002\u6253\u5f00\u547d\u4ee4\u884c\u7ec8\u7aef\uff0c\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install neuralpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u548c<code>pip<\/code>\u7248\u672c\u662f\u6700\u65b0\u7684\uff0c\u4ee5\u907f\u514d\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u517c\u5bb9\u6027\u95ee\u9898\u3002\u6210\u529f\u5b89\u88c5\u540e\uff0c\u4f60\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165NeuralPy\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5bfc\u5165\u5e93\u5e76\u5f00\u59cb\u4f7f\u7528<\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165NeuralPy\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5bfc\u5165\u5e76\u5f00\u59cb\u4f7f\u7528NeuralPy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import neuralpy as npy<\/p>\n<h2><strong>\u68c0\u67e5NeuralPy\u662f\u5426\u5bfc\u5165\u6210\u529f<\/strong><\/h2>\n<p>print(&quot;NeuralPy imported successfully!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e00\u65e6\u5bfc\u5165\u6210\u529f\uff0c\u4f60\u5c31\u53ef\u4ee5\u5f00\u59cb\u4f7f\u7528NeuralPy\u6765\u521b\u5efa\u548c\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u3002NeuralPy\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u6a21\u5757\u548c\u7c7b\uff0c\u5e2e\u52a9\u4f60\u5feb\u901f\u6784\u5efa\u548c\u5b9e\u9a8c\u4e0d\u540c\u7684\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5b66\u4e60NeuralPy\u7684\u57fa\u672c\u529f\u80fd<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u795e\u7ecf\u7f51\u7edc\u6a21\u578b<\/strong><\/li>\n<\/ol>\n<p><p>NeuralPy\u63d0\u4f9b\u4e86\u591a\u79cd\u795e\u7ecf\u7f51\u7edc\u5c42\u548c\u529f\u80fd\u6a21\u5757\uff0c\u5e2e\u52a9\u4f60\u8f7b\u677e\u6784\u5efa\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa\u7b80\u5355\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from neuralpy.models import Sequential<\/p>\n<p>from neuralpy.layers import Dense<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aSequential\u6a21\u578b<\/strong><\/h2>\n<p>model = Sequential()<\/p>\n<h2><strong>\u5411\u6a21\u578b\u4e2d\u6dfb\u52a0\u5c42<\/strong><\/h2>\n<p>model.add(Dense(n_nodes=64, input_shape=(784,), activation=&#39;relu&#39;))<\/p>\n<p>model.add(Dense(n_nodes=10, activation=&#39;softmax&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aSequential\u6a21\u578b\uff0c\u5e76\u5411\u5176\u4e2d\u6dfb\u52a0\u4e86\u4e24\u5c42\uff1a\u4e00\u4e2a\u5177\u670964\u4e2a\u8282\u70b9\u7684Dense\u5c42\u548c\u4e00\u4e2a\u5177\u670910\u4e2a\u8282\u70b9\u7684\u8f93\u51fa\u5c42\u3002\u6bcf\u4e2a\u5c42\u90fd\u6307\u5b9a\u4e86\u6fc0\u6d3b\u51fd\u6570\uff0c\u4f8b\u5982<code>relu<\/code>\u548c<code>softmax<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7f16\u8bd1\u548c\u8bad\u7ec3\u6a21\u578b<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u521b\u5efa\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u540e\uff0c\u9700\u8981\u7f16\u8bd1\u548c\u8bad\u7ec3\u8be5\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7f16\u8bd1\u548c\u8bad\u7ec3\u6a21\u578b\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7f16\u8bd1\u6a21\u578b<\/p>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;categorical_crossentropy&#39;, metrics=[&#39;accuracy&#39;])<\/p>\n<h2><strong>\u5047\u8bbeX_tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n\u548cy_train\u662f\u4f60\u7684\u8bad\u7ec3\u6570\u636e\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>model.fit(X_train, y_train, epochs=10, batch_size=32)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528Adam\u4f18\u5316\u5668\u548c\u4ea4\u53c9\u71b5\u635f\u5931\u51fd\u6570\u6765\u7f16\u8bd1\u6a21\u578b\u3002\u7136\u540e\uff0c\u6211\u4eec\u8c03\u7528<code>fit<\/code>\u65b9\u6cd5\u6765\u8bad\u7ec3\u6a21\u578b\uff0c\u8bbe\u7f6e\u8bad\u7ec3\u7684epochs\u548cbatch size\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8bc4\u4f30\u548c\u9884\u6d4b<\/strong><\/li>\n<\/ol>\n<p><p>\u8bad\u7ec3\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u6d4b\u8bd5\u6570\u636e\u6765\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd\uff0c\u5e76\u8fdb\u884c\u9884\u6d4b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8bc4\u4f30\u548c\u9884\u6d4b\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bc4\u4f30\u6a21\u578b<\/p>\n<p>loss, accuracy = model.evaluate(X_test, y_test)<\/p>\n<p>print(f&quot;Test Loss: {loss}, Test Accuracy: {accuracy}&quot;)<\/p>\n<h2><strong>\u8fdb\u884c\u9884\u6d4b<\/strong><\/h2>\n<p>predictions = model.predict(X_new)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u7528<code>evaluate<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u5f97\u6a21\u578b\u5728\u6d4b\u8bd5\u6570\u636e\u4e0a\u7684\u635f\u5931\u548c\u51c6\u786e\u7387\u3002\u4f7f\u7528<code>predict<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5bf9\u65b0\u6570\u636e\u8fdb\u884c\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001NeuralPy\u7684\u9ad8\u7ea7\u529f\u80fd<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u5c42\u548c\u6fc0\u6d3b\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>NeuralPy\u5141\u8bb8\u7528\u6237\u521b\u5efa\u81ea\u5b9a\u4e49\u5c42\u548c\u6fc0\u6d3b\u51fd\u6570\uff0c\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u81ea\u5b9a\u4e49\u5c42\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from neuralpy.layers import Layer<\/p>\n<p>class CustomLayer(Layer):<\/p>\n<p>    def __init__(self, n_nodes):<\/p>\n<p>        super().__init__()<\/p>\n<p>        # \u521d\u59cb\u5316\u81ea\u5b9a\u4e49\u5c42\u7684\u53c2\u6570<\/p>\n<p>        self.n_nodes = n_nodes<\/p>\n<p>    def build(self, input_shape):<\/p>\n<p>        # \u5b9a\u4e49\u5c42\u7684\u6743\u91cd\u548c\u504f\u7f6e<\/p>\n<p>        self.weights = self.add_weight(shape=(input_shape[-1], self.n_nodes))<\/p>\n<p>        self.bias = self.add_bias(shape=(self.n_nodes,))<\/p>\n<p>    def call(self, inputs):<\/p>\n<p>        # \u5b9e\u73b0\u5c42\u7684\u524d\u5411\u4f20\u64ad<\/p>\n<p>        return inputs @ self.weights + self.bias<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6a21\u578b\u4fdd\u5b58\u4e0e\u52a0\u8f7d<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u8bad\u7ec3\u6a21\u578b\u540e\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u4fdd\u5b58\u6a21\u578b\u4ee5\u4fbf\u4ee5\u540e\u4f7f\u7528\u3002NeuralPy\u63d0\u4f9b\u4e86\u4fdd\u5b58\u548c\u52a0\u8f7d\u6a21\u578b\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u6a21\u578b<\/p>\n<p>model.save(&#39;my_model.h5&#39;)<\/p>\n<h2><strong>\u52a0\u8f7d\u6a21\u578b<\/strong><\/h2>\n<p>loaded_model = Sequential.load(&#39;my_model.h5&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u5e94\u7528<\/p>\n<\/p>\n<p><p>NeuralPy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u6784\u5efa\u548c\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u5b89\u88c5\u548c\u4f7f\u7528NeuralPy\uff0c\u5e76\u5c55\u793a\u4e86\u5176\u57fa\u672c\u548c\u9ad8\u7ea7\u529f\u80fd\u3002\u901a\u8fc7NeuralPy\uff0c\u4f60\u53ef\u4ee5\u66f4\u4e13\u6ce8\u4e8e\u6a21\u578b\u7684\u5f00\u53d1\u548c\u4f18\u5316\uff0c\u800c\u4e0d\u5fc5\u8fc7\u591a\u5173\u5fc3\u5e95\u5c42\u5b9e\u73b0\u7ec6\u8282\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528NeuralPy\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5neuralpy\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5b89\u88c5neuralpy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u5de5\u5177\u3002\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<code>pip install neuralpy<\/code>\u3002\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u5df2\u7ecf\u6b63\u786e\u914d\u7f6e\uff0c\u5e76\u4e14pip\u662f\u6700\u65b0\u7248\u672c\u3002\u5982\u679c\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u9047\u5230\u95ee\u9898\uff0c\u53ef\u4ee5\u67e5\u770b\u76f8\u5173\u7684\u9519\u8bef\u4fe1\u606f\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u8c03\u6574\u3002<\/p>\n<p><strong>neuralpy\u5e93\u9002\u5408\u7528\u4e8e\u54ea\u4e9b\u7c7b\u578b\u7684\u9879\u76ee\uff1f<\/strong><br \/>neuralpy\u5e93\u4e3b\u8981\u7528\u4e8e\u6784\u5efa\u548c\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\uff0c\u56e0\u6b64\u975e\u5e38\u9002\u5408\u7528\u4e8e\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u9879\u76ee\u3002\u5b83\u53ef\u4ee5\u5e94\u7528\u4e8e\u56fe\u50cf\u8bc6\u522b\u3001\u81ea\u7136\u8bed\u8a00\u5904\u7406\u3001\u65f6\u95f4\u5e8f\u5217\u9884\u6d4b\u7b49\u591a\u4e2a\u9886\u57df\u3002\u5982\u679c\u4f60\u7684\u9879\u76ee\u6d89\u53ca\u5230\u590d\u6742\u7684\u6570\u636e\u6a21\u5f0f\u8bc6\u522b\uff0cneuralpy\u5c06\u662f\u4e00\u4e2a\u7406\u60f3\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528neuralpy\u5e93\u8fdb\u884c\u57fa\u672c\u7684\u795e\u7ecf\u7f51\u7edc\u8bad\u7ec3\uff1f<\/strong><br \/>\u4f7f\u7528neuralpy\u5e93\u8fdb\u884c\u795e\u7ecf\u7f51\u7edc\u8bad\u7ec3\u901a\u5e38\u5305\u62ec\u51e0\u4e2a\u6b65\u9aa4\uff1a\u5b9a\u4e49\u6a21\u578b\u67b6\u6784\u3001\u7f16\u8bd1\u6a21\u578b\u3001\u51c6\u5907\u6570\u636e\u96c6\u4ee5\u53ca\u8bad\u7ec3\u6a21\u578b\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2aNeural Network\u5bf9\u8c61\uff0c\u6dfb\u52a0\u5c42\u548c\u6fc0\u6d3b\u51fd\u6570\u6765\u5b9a\u4e49\u6a21\u578b\u67b6\u6784\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>.compile()<\/code>\u65b9\u6cd5\u6307\u5b9a\u4f18\u5316\u5668\u548c\u635f\u5931\u51fd\u6570\uff0c\u6700\u540e\u8c03\u7528<code>.fit()<\/code>\u65b9\u6cd5\u8fdb\u884c\u8bad\u7ec3\u3002\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u8be6\u7ec6\u6587\u6863\u53ef\u4ee5\u5728neuralpy\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0a\u627e\u5230\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u6bcf\u4e2a\u6b65\u9aa4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u6dfb\u52a0NeuralPy\uff0c\u4f60\u9700\u8981\u901a\u8fc7\u5b89\u88c5NeuralPy\u5e93\u3001\u5bfc\u5165\u5e93\u5e76\u5f00\u59cb\u4f7f\u7528\u3001\u5b66\u4e60NeuralP [&hellip;]","protected":false},"author":3,"featured_media":935279,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/935274"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=935274"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/935274\/revisions"}],"predecessor-version":[{"id":935280,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/935274\/revisions\/935280"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/935279"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=935274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=935274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=935274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}