<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 二分類 二分類是指僅有兩個不同類的問題。正如我們在上一章中所做的那樣,我們將使用 SciKit Learn 庫中的便捷函數`make_classification()`生成數據集: ```py X, y = skds.make_classification(n_samples=200, n_features=2, n_informative=2, n_redundant=0, n_repeated=0, n_classes=2, n_clusters_per_class=1) if (y.ndim == 1): y = y.reshape(-1,1) ``` `make_classification()`的論據是不言自明的; `n_samples`是要生成的數據點數,`n_features`是要生成的特征數,`n_classes`是類的數量,即 2: * `n_samples`是要生成的數據點數。我們將其保持在 200 以保持數據集較小。 * `n_features`是要生成的特征數量;我們只使用兩個特征,因此我們可以將它作為一個簡單的問題來理解 TensorFlow 命令。 * `n_classes`是類的數量,它是 2,因為它是二分類問題。 讓我們使用以下代碼繪制數據: ```py plt.scatter(X[:,0],X[:,1],marker='o',c=y) plt.show() ``` 我們得到以下繪圖;您可能會得到一個不同的圖,因為每次運行數據生成函數時都會隨機生成數據: ![](https://img.kancloud.cn/78/1d/781da88435f328778f5171a817d56f8b_820x469.png) 然后我們使用 NumPy `eye`函數將`y`轉換為單熱編碼目標: ```py print(y[0:5]) y=np.eye(num_outputs)[y] print(y[0:5]) ``` 單熱編碼目標如下所示: ```py [1 0 0 1 0] [[ 0\. 1.] [ 1\. 0.] [ 1\. 0.] [ 0\. 1.] [ 1\. 0.]] ``` 將數據劃分為訓練和測試類別: ```py X_train, X_test, y_train, y_test = skms.train_test_split( X, y, test_size=.4, random_state=42) ``` 在分類中,我們使用 sigmoid 函數來量化模型的值,使得輸出值位于范圍[0,1]之間。以下等式表示由`φ(z)`表示的 S 形函數,其中`z`是等式`w × x + b`。損失函數現在變為由`J(θ)`表示的值,其中`θ`表示參數。 ![](https://img.kancloud.cn/5e/f5/5ef575dd61ec4f87c103dccfc5c40651_1300x190.png) ![](https://img.kancloud.cn/3a/0c/3a0cbc2a987aeda65134eaabaa3d9c39_1250x430.png) ![](https://img.kancloud.cn/65/69/656956b9979d34c2ea300bc772241e65_4970x540.png) 我們使用以下代碼實現新模型和損失函數: ```py num_outputs = y_train.shape[1] num_inputs = X_train.shape[1] learning_rate = 0.001 # input images x = tf.placeholder(dtype=tf.float32, shape=[None, num_inputs], name="x") # output labels y = tf.placeholder(dtype=tf.float32, shape=[None, num_outputs], name="y") # model paramteres w = tf.Variable(tf.zeros([num_inputs,num_outputs]), name="w") b = tf.Variable(tf.zeros([num_outputs]), name="b") model = tf.nn.sigmoid(tf.matmul(x, w) + b) loss = tf.reduce_mean(-tf.reduce_sum( (y * tf.log(model)) + ((1 - y) * tf.log(1 - model)), axis=1)) optimizer = tf.train.GradientDescentOptimizer( learning_rate=learning_rate).minimize(loss) ``` 最后,我們運行我們的分類模型: ```py num_epochs = 1 with tf.Session() as tfs: tf.global_variables_initializer().run() for epoch in range(num_epochs): tfs.run(optimizer, feed_dict={x: X_train, y: y_train}) y_pred = tfs.run(tf.argmax(model, 1), feed_dict={x: X_test}) y_orig = tfs.run(tf.argmax(y, 1), feed_dict={y: y_test}) preds_check = tf.equal(y_pred, y_orig) accuracy_op = tf.reduce_mean(tf.cast(preds_check, tf.float32)) accuracy_score = tfs.run(accuracy_op) print("epoch {0:04d} accuracy={1:.8f}".format( epoch, accuracy_score)) plt.figure(figsize=(14, 4)) plt.subplot(1, 2, 1) plt.scatter(X_test[:, 0], X_test[:, 1], marker='o', c=y_orig) plt.title('Original') plt.subplot(1, 2, 2) plt.scatter(X_test[:, 0], X_test[:, 1], marker='o', c=y_pred) plt.title('Predicted') plt.show() ``` 我們獲得了大約 96%的相當好的準確率,原始和預測的數據圖如下所示: ![](https://img.kancloud.cn/81/72/8172c0d9cb98fb911c930f1789428b16_820x264.png) 很簡約!!現在讓我們讓我們的問題變得復雜,并嘗試預測兩個以上的類。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看