<jupyter_start><jupyter_text>初始化<jupyter_code>#@markdown - **挂载** 
from google.colab import drive
drive.mount('GoogleDrive')
# #@markdown - **卸载**
# !fusermount -u GoogleDrive<jupyter_output><empty_output><jupyter_text>代码区<jupyter_code>#@title 支持向量机 { display-mode: "both" }
# 该程序通过梯度下降法来实现支持向量机
#@markdown [参考资料](https://en.wikipedia.org/wiki/Support-vector_machine)
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data

tf.logging.set_verbosity(tf.logging.ERROR)
#@markdown - **计时装饰器**
import time
def timer(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        t = end_time - start_time
        print('This operation takes {} s.'.format(t))
        return result
    return wrapper
#@markdown - **参数设置**
batch_size = 128 #@param {type: "integer"}
num_epochs = 100 #@param {type: "integer"}
C_param = 0.1 #@param {type: "number"}
Reg_param = 1.0 #@param {type: "number"}
delta = 1.0 #@param {type: "number"}
learning_rate = 1e-3 #@param {type: "number"}

#@markdown - **误差函数与精度函数**
def loss_fn(W,b,x_data,y_target):
    logits = tf.subtract(tf.matmul(x_data, W),b)
    norm_term = tf.divide(tf.reduce_sum(tf.multiply(tf.transpose(W),W)),2)
    classification_loss = tf.reduce_mean(tf.maximum(0., tf.subtract(delta, tf.multiply(logits, y_target))))
    total_loss = tf.add(tf.multiply(C_param,classification_loss), tf.multiply(Reg_param,norm_term))
    return total_loss

def inference_fn(W,b,x_data,y_target):
    prediction = tf.sign(tf.subtract(tf.matmul(x_data, W), b))
    accuracy = tf.reduce_mean(tf.cast(tf.equal(prediction, y_target), tf.float32))
    return accuracy
# 只获取0或1的图像的索引
@timer
def extraction_fn(data):
    index_list = []
    for idx in range(data.shape[0]):
        if data[idx] == 0 or data[idx] == 1:
            index_list.append(idx)
    return index_list
#@markdown - **所需训练图像和标签的获取**
mnist = input_data.read_data_sets("sample_data/MNIST_data", reshape=True, one_hot=False)
data = {}
data['train_image'] = mnist.train.images
data['train_label'] = mnist.train.labels
data['test_image'] = mnist.test.images
data['test_label'] = mnist.test.labels

index_list_train = extraction_fn(data['train_label'])
index_list_test = extraction_fn(data['test_label'])

data['train_image'] = mnist.train.images[index_list_train]
data['train_label'] = mnist.train.labels[index_list_train]
data['test_image'] = mnist.test.images[index_list_test]
data['test_label'] = np.array(mnist.test.labels[index_list_test], dtype=np.float32)
# data['test_label'] = mnist.test.labels[index_list_test].astype('float32')

data['train_image_label'] = np.c_[data['train_image'], data['train_label']]
num_samples, num_features = data['train_image'].shape
num_hidden = 256

graph = tf.Graph()
with graph.as_default():
    with tf.name_scope('Input'):
        x_data = tf.placeholder(shape=[None, num_features], dtype=tf.float32)
        y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)
    with tf.name_scope('Net'):
        # 降低数据维数
        w_1 = tf.Variable(tf.random_normal(shape=[num_features, num_hidden]), name='w_1')
        b_1 = tf.Variable(tf.random_normal(shape=[num_hidden]), name='b_1')
        layer_1 = tf.sigmoid(tf.matmul(x_data, w_1) + b_1)

        W = tf.Variable(tf.random_normal(shape=[num_hidden, 1]), name='weights')
        b = tf.Variable(tf.random_normal(shape=[1]), name='bias')
    with tf.name_scope('Loss'):
        total_loss = loss_fn(W, b, layer_1, y_target)
    with tf.name_scope('Accuracy'):
        prediction = tf.sign(tf.subtract(tf.matmul(layer_1, W), b))
        accuracy = inference_fn(W, b, layer_1, y_target)
    with tf.name_scope('Train'):
        train_op = tf.train.AdamOptimizer(learning_rate).minimize(total_loss)
#@markdown - **转换测试集标签**
test_label = data['test_label'].reshape(-1, 1)
test_label[test_label==0] = -1
#@markdown - **训练网络**
with tf.Session(graph=graph) as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(num_epochs):
        np.random.shuffle(data['train_image_label'])
        image_batch = data['train_image_label'][:batch_size,:-1]
        label_batch = data['train_image_label'][:batch_size,-1]
        label_batch[label_batch==0] = -1

        _, loss, acc = sess.run([train_op, total_loss, accuracy], feed_dict={x_data: image_batch, 
                                                                            y_target: label_batch.reshape(-1, 1)})
        acc *= 100
        if (epoch + 1) % 5 == 0:
            test_loss, test_acc = sess.run([total_loss, accuracy], feed_dict={x_data: data['test_image'], 
                                                                            y_target: test_label})
            test_acc *= 100
            print_list = [epoch + 1, loss, acc, test_acc]
            print('Epoch {0[0]}, loss: {0[1]:.4f}, training accuracy: {0[2]:.2f}%.'.format(print_list))
            print(' '*10, 'Testing accuracy is {0[3]:.2f}%.'.format(print_list))
sess.close()<jupyter_output>Epoch 5, loss: 486.1747, training accuracy: 55.47%.
           Testing accuracy is 49.46%.
Epoch 10, loss: 446.9977, training accuracy: 56.25%.
           Testing accuracy is 48.42%.
Epoch 15, loss: 409.8583, training accuracy: 50.00%.
           Testing accuracy is 54.37%.
Epoch 20, loss: 374.6324, training accuracy: 75.00%.
           Testing accuracy is 71.68%.
Epoch 25, loss: 341.4847, training accuracy: 87.50%.
           Testing accuracy is 85.15%.
Epoch 30, loss: 310.4678, training accuracy: 90.62%.
           Testing accuracy is 91.63%.
Epoch 35, loss: 281.5071, training accuracy: 94.53%.
           Testing accuracy is 94.94%.
Epoch 40, loss: 254.5600, training accuracy: 99.22%.
           Testing accuracy is 96.55%.
Epoch 45, loss: 229.6027, training accuracy: 98.44%.
           Testing accuracy is 97.59%.
Epoch 50, loss: 206.5325, training accuracy: 98.44%.
           Testing accuracy is 98.01%.
Epoch 55, loss: 185.2778, training accuracy: 97.66%.
           Testing accuracy [...]
{'fraction_non_alphanumeric': 0.15041782729805014, 'fraction_numerical': 0.042929706701622154, 'mean_word_length': 3.524833209785026, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '186116', 'license': 'MIT', 'n_tokens_mistral': 2375, 'n_tokens_neox': 2196, 'n_words': 428, 'path': 'notebooks(colab)/Linear_models/linear_SVM.ipynb', 'repo_name': 'jswanglp/MyML', 'size': '6103'}