{"id":204003,"date":"2025-05-29T13:39:07","date_gmt":"2025-05-29T05:39:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204003\/"},"modified":"2025-05-29T13:39:07","modified_gmt":"2025-05-29T05:39:07","slug":"%e4%bd%bf%e7%94%a8-cnn-vgg-%e7%bd%91%e7%bb%9c%e6%a3%80%e6%b5%8b%e5%a4%96%e6%b1%87%e4%bb%b7%e6%a0%bc%e4%bf%ae%e6%ad%a3%ef%bc%88%e4%bd%bf%e7%94%a8-python%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204003\/","title":{"rendered":"\u4f7f\u7528 CNN VGG \u7f51\u7edc\u68c0\u6d4b\u5916\u6c47\u4ef7\u683c\u4fee\u6b63\uff08\u4f7f\u7528 Python\uff09"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528 CNN VGG \u7f51\u7edc\u68c0\u6d4b\u5916\u6c47\u4ef7\u683c\u4fee\u6b63\uff08\u4f7f\u7528 Python\uff09<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528 CNN VGG \u7f51\u7edc\u68c0\u6d4b\u5916\u6c47\u4ef7\u683c\u4fee\u6b63\uff08\u4f7f\u7528 Python\uff09\u300b<\/span>\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241017\/1729136387671087039d27b.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5916\u6c47\u4ea4\u6613<\/strong>\u662f\u6700\u5177\u6d3b\u529b\u7684\u91d1\u878d\u5e02\u573a\u4e4b\u4e00\uff0c\u4ef7\u683c\u4e0d\u65ad\u53d8\u5316\u3002\u5bf9\u4e8e\u4ea4\u6613\u8005\u6765\u8bf4\uff0c\u5c3d\u65e9\u53d1\u73b0\u4ef7\u683c\u8c03\u6574\u81f3\u5173\u91cd\u8981\u3002 <strong>\u4ef7\u683c\u8c03\u6574<\/strong>\u662f\u6307\u5728\u5e02\u573a\u7ee7\u7eed\u5176\u539f\u6765\u7684\u65b9\u5411\u4e4b\u524d\u6574\u4f53\u8d8b\u52bf\u7684\u6682\u65f6\u9006\u8f6c\u3002 <strong>\u5377\u79ef\u795e\u7ecf\u7f51\u7edc (cnn)<\/strong>\uff0c\u5c24\u5176\u662f <strong>vgg \u67b6\u6784<\/strong>\uff0c\u63d0\u4f9b\u4e86\u901a\u8fc7\u8bc6\u522b\u5916\u6c47\u6570\u636e\u4e2d\u7684\u5fae\u5999\u6a21\u5f0f\u6765\u68c0\u6d4b\u8fd9\u4e9b\u4fee\u6b63\u7684\u521b\u65b0\u65b9\u6cd5\u3002<\/p>\n<h4> \u4ec0\u4e48\u662f\u4ef7\u683c\u4fee\u6b63\uff1f <\/h4>\n<p>\u5f53\u4ef7\u683c\u77ed\u6682\u5730\u4e0e\u8d8b\u52bf\u76f8\u53cd\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f<strong>\u4ef7\u683c\u8c03\u6574<\/strong>\uff0c\u4e3a\u4ea4\u6613\u8005\u521b\u9020\u5efa\u7acb\u65b0\u5934\u5bf8\u6216\u8c03\u6574\u73b0\u6709\u5934\u5bf8\u7684\u673a\u4f1a\u3002\u4f8b\u5982\uff0c\u5728\u770b\u6da8\u8d8b\u52bf\u4e2d\uff0c\u5f53\u4ef7\u683c\u6682\u65f6\u4e0b\u8dcc\u7136\u540e\u6062\u590d\u4e0a\u884c\u8f68\u8ff9\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u4fee\u6b63\u3002\u53ca\u65e9\u53d1\u73b0\u8fd9\u4e9b\u4ef7\u683c\u8c03\u6574\u53ef\u4ee5\u663e\u7740\u5f71\u54cd\u4ea4\u6613\u8005\u7684\u7b56\u7565\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u597d\u7684\u98ce\u9669\u7ba1\u7406\u548c\u53ca\u65f6\u7684\u51b3\u7b56\u3002<\/p>\n<p><strong>cnn<\/strong> \u5df2\u88ab\u8bc1\u660e\u5728\u6a21\u5f0f\u8bc6\u522b\u65b9\u9762\u975e\u5e38\u6709\u6548\uff0c\u5c24\u5176\u662f\u5728\u56fe\u50cf\u5206\u7c7b\u4efb\u52a1\u4e2d\u3002\u50cf\u5916\u6c47\u8fd9\u6837\u7684\u91d1\u878d\u5e02\u573a\u867d\u7136\u57fa\u4e8e\u6570\u5b57\u6570\u636e\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u5c06\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff08\u4f8b\u5982\u70db\u53f0\u56fe\uff09\u8f6c\u6362\u4e3a\u56fe\u50cf\u6765\u53d7\u76ca\u4e8e cnn \u7684\u4f18\u52bf\u3002 <strong>vgg \u7f51\u7edc<\/strong> \u7531\u725b\u6d25\u5927\u5b66\u89c6\u89c9\u51e0\u4f55\u5c0f\u7ec4\u63a8\u51fa\uff0c\u7531\u4e8e\u5176\u6df1\u5ea6\u548c\u7b80\u5355\u6027\u800c\u7279\u522b\u9002\u5408\u3002\u5b83\u4eec\u7531\u591a\u4e2a\u5377\u79ef\u5c42\u7ec4\u6210\uff0c\u8fd9\u4e9b\u5c42\u9010\u6e10\u4ece\u8f93\u5165\u6570\u636e\u4e2d\u5b66\u4e60\u590d\u6742\u7684\u7279\u5f81\u3002<\/p>\n<h4> \u5728\u5916\u6c47\u4ea4\u6613\u4e2d\u4f7f\u7528 cnn vgg \u7684\u4f18\u70b9\uff1a <\/h4>\n<ul>\n<li> <strong>\u6a21\u5f0f\u8bc6\u522b\uff1a<\/strong> cnn \u64c5\u957f\u8bc6\u522b\u56fe\u50cf\u4e2d\u7684\u5fae\u5999\u6a21\u5f0f\u548c\u8d8b\u52bf\uff0c\u5e2e\u52a9\u4ea4\u6613\u8005\u68c0\u6d4b\u901a\u8fc7\u4f20\u7edf\u6280\u672f\u5206\u6790\u53ef\u80fd\u4e0d\u6613\u770b\u5230\u7684\u4fee\u6b63\u3002<\/li>\n<li> <strong>\u81ea\u52a8\u5316\uff1a<\/strong> cnn \u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u5927\u91cf\u5916\u6c47\u6570\u636e\uff0c\u4ece\u800c\u5b9e\u73b0\u5b9e\u65f6\u5206\u6790\u3002<\/li>\n<li> <strong>\u901f\u5ea6\uff1a<\/strong>\u9274\u4e8e\u5916\u6c47\u4ea4\u6613\u7684\u5feb\u8282\u594f\u672c\u8d28\uff0cvgg \u7f51\u7edc\u53ef\u4ee5\u5feb\u901f\u8bc6\u522b\u6f5c\u5728\u7684\u8c03\u6574\uff0c\u4e3a\u4ea4\u6613\u8005\u63d0\u4f9b\u7ade\u4e89\u4f18\u52bf\u3002<\/li>\n<\/ul>\n<p>\u8981\u5c06 cnn \u5e94\u7528\u4e8e\u5916\u6c47\u4ea4\u6613\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u5c06\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u6a21\u578b\u53ef\u4ee5\u5904\u7406\u7684\u683c\u5f0f\u2014\u2014\u56fe\u50cf\u3002\u8fd9\u4e9b\u56fe\u50cf\u53ef\u4ee5\u662f\u4ef7\u683c\u53d8\u52a8\u7684\u89c6\u89c9\u8868\u793a\uff0c\u4f8b\u5982\u70db\u53f0\u56fe\u3001\u70ed\u56fe\u6216\u6298\u7ebf\u56fe\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u6211\u4eec\u5982\u4f55\u5c06\u5916\u6c47\u4ef7\u683c\u6570\u636e\u8f6c\u6362\u4e3a\u70db\u53f0\u56fe\u4ee5\u4f9b cnn \u5904\u7406\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre>import matplotlib.pyplot as plt\nimport numpy as np\n\ndef create_candlestick_image(open_prices, high_prices, low_prices, close_prices, output_file):\n    fig, ax = plt.subplots(figsize=(6, 6))  # increased image size for more clarity\n\n    for i in range(len(open_prices)):\n        color = 'green' if close_prices[i] &gt; open_prices[i] else 'red'\n        ax.plot([i, i], [low_prices[i], high_prices[i]], color='black', linewidth=1.5)\n        ax.plot([i, i], [open_prices[i], close_prices[i]], color=color, linewidth=6)\n\n    ax.axis('off')  # hide the axes for better image clarity\n    plt.savefig(output_file, bbox_inches='tight', pad_inches=0)\n    plt.close()\n\n# example data\nopen_prices = np.random.rand(20) * 100\nhigh_prices = open_prices + np.random.rand(20) * 10\nlow_prices = open_prices - np.random.rand(20) * 10\nclose_prices = open_prices + np.random.rand(20) * 5 - 2.5\n\n# generate candlestick image\ncreate_candlestick_image(open_prices, high_prices, low_prices, close_prices, \"candlestick_chart.png\")\n<\/pre>\n<p>\u6b64 python \u4ee3\u7801\u751f\u6210\u4e00\u4e2a\u70db\u53f0\u56fe\uff0c\u53ef\u4ee5\u5c06\u5176\u53e6\u5b58\u4e3a\u56fe\u50cf\u4ee5\u8f93\u5165 vgg \u6a21\u578b\u3002<\/p>\n<p>\u4e00\u65e6\u5916\u6c47\u6570\u636e\u8f6c\u6362\u4e3a\u56fe\u50cf\uff0cvgg \u7f51\u7edc\u5c31\u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u4ef7\u683c\u4fee\u6b63\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5b9e\u65bd <strong>vgg16 \u7f51\u7edc<\/strong> \u5bf9\u5916\u6c47\u4ef7\u683c\u4fee\u6b63\u8fdb\u884c\u5206\u7c7b\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u9884\u5904\u7406\uff1a<\/strong>\u52a0\u8f7d\u5e76\u9884\u5904\u7406\u5916\u6c47\u70db\u53f0\u56fe\u50cf\uff0c\u786e\u4fdd vgg16 \u4f7f\u7528\u6b63\u786e\u7684\u56fe\u50cf\u5c3a\u5bf8 (224&#215;224)\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u7279\u5f81\u63d0\u53d6\uff1a<\/strong>\u4f7f\u7528\u9884\u5148\u8bad\u7ec3\u7684 vgg16 \u6a21\u578b\u4ece\u5916\u6c47\u6570\u636e\u56fe\u50cf\u4e2d\u63d0\u53d6\u9ad8\u7ea7\u7279\u5f81\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8bad\u7ec3\u6a21\u578b\uff1a<\/strong>\u5fae\u8c03\u6a21\u578b\u4ee5\u9884\u6d4b\u662f\u5426\u4f1a\u53d1\u751f\u4ef7\u683c\u4fee\u6b63\uff08\u4e70\u5165\u3001\u5356\u51fa\u3001\u65e0\uff09\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>from tensorflow.keras.applications import VGG16\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import Adam\n\n# Load VGG16 without the top fully connected layers\nvgg_base = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n\n# Build a new model using VGG as the base\nmodel = Sequential()\nmodel.add(vgg_base)\nmodel.add(Flatten())  # Flatten the 3D outputs to 1D\nmodel.add(Dense(512, activation='relu'))  # Fully connected layer\nmodel.add(Dropout(0.5))  # Regularization to prevent overfitting\nmodel.add(Dense(3, activation='softmax'))  # Output layer for 3 classes: Buy, Sell, None\n\n# Freeze the convolutional base of VGG16\nfor layer in vgg_base.layers:\n    layer.trainable = False\n\n# Compile the model\nmodel.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Data augmentation to increase the diversity of the dataset\ntrain_datagen = ImageDataGenerator(rescale=1.\/255, rotation_range=30, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)\n\n# Assuming 'train_dir' contains the candlestick images\ntrain_generator = train_datagen.flow_from_directory(\n    'train_dir',\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode='categorical')\n\n# Train the model\nmodel.fit(train_generator, epochs=20)\n<\/pre>\n<p>\u6b64\u793a\u4f8b\u901a\u8fc7\u5229\u7528\u9884\u8bad\u7ec3\u7684 vgg16 \u6a21\u578b\u6765\u4f7f\u7528<strong>\u8fc1\u79fb\u5b66\u4e60<\/strong>\uff0c\u8be5\u6a21\u578b\u5df2\u7ecf\u7cbe\u901a\u7279\u5f81\u63d0\u53d6\u3002\u901a\u8fc7\u51bb\u7ed3\u5377\u79ef\u5c42\u5e76\u6dfb\u52a0\u65b0\u7684\u5168\u8fde\u63a5\u5c42\uff0c\u53ef\u4ee5\u5bf9\u6a21\u578b\u8fdb\u884c\u5fae\u8c03\u4ee5\u68c0\u6d4b\u7279\u5b9a\u4e8e\u5916\u6c47\u6570\u636e\u7684\u4ef7\u683c\u4fee\u6b63\u3002<\/p>\n<p>\u867d\u7136 cnn\uff0c\u5c24\u5176\u662f vgg\uff0c\u63d0\u4f9b\u4e86\u51c6\u786e\u6027\u548c\u901f\u5ea6\uff0c\u4f46\u4ecd\u9700\u8981\u8003\u8651\u4e00\u4e9b\u6311\u6218\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u8868\u793a\uff1a<\/strong>\u5916\u6c47\u6570\u636e\u5fc5\u987b\u8f6c\u6362\u4e3a\u56fe\u50cf\uff0c\u8fd9\u9700\u8981\u4ed4\u7ec6\u89c4\u5212\u4ee5\u786e\u4fdd\u56fe\u50cf\u4ee3\u8868\u6709\u610f\u4e49\u7684\u91d1\u878d\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8fc7\u5ea6\u62df\u5408\uff1a<\/strong>\u5982\u679c\u4f7f\u7528\u4e0d\u8db3\u6216\u975e\u591a\u6837\u5316\u7684\u6570\u636e\u8fdb\u884c\u8bad\u7ec3\uff0c\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u53ef\u80fd\u4f1a\u8fc7\u5ea6\u62df\u5408\u3002 <strong>\u4e22\u5f03<\/strong>\u3001<strong>\u6570\u636e\u589e\u5f3a<\/strong>\u7b49\u6280\u672f\u4ee5\u53ca\u786e\u4fdd\u5927\u578b\u3001\u5e73\u8861\u7684\u6570\u636e\u96c6\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5e02\u573a\u566a\u97f3\uff1a<\/strong>\u91d1\u878d\u6570\u636e\u5145\u6ee1\u566a\u97f3\uff0c\u533a\u5206\u771f\u6b63\u7684\u4fee\u6b63\u548c\u968f\u673a\u6ce2\u52a8\u53ef\u80fd\u5f88\u68d8\u624b\u3002\u8fd9\u4f7f\u5f97\u4f7f\u7528\u9ad8\u8d28\u91cf\u7684\u6807\u8bb0\u6570\u636e\u8bad\u7ec3 cnn \u53d8\u5f97\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/li>\n<\/ol>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 CNN VGG \u7f51\u7edc\u68c0\u6d4b\u5916&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204003","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204003"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204003\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}