夜雨聆风学习资料网

ARTICLE · 1147581

AI开发之基础快速入门主流深度学习框架介绍TensorFlow

AI开发之基础快速入门主流深度学习框架介绍TensorFlow

1)tensorFlow框架介绍

网上有很多了介绍包括它的官方,tensorflow简单来说,就是google的ai开发大工具箱。它是2015年开源的,到现在全世界都有海量的公司和程序员都在用它。不管是搞学术研究的写论文,还是各大互联网公司要在系统里上线ai功能,它都是顶梁柱之一。

下面还需要知道两个基本概念

张量tensor,流flow别被它两的名字吓到了!

tensor其实就是各种数字,矩阵或数据大礼包;

flow就是数据流转和计算的过程。

合起来就是:咱们把数据倒进去,它按照咱们设计的管道一路计算,最后流出咱们想要的结果,如预测网络流量是正常还是攻击

其他还有一个需要注意的地方,tf老版本中有一个session/会话(主要不要和web中的session搞混淆了哦)

经典的老版本tf1.x了有session这个概念。

新版本tf 2.x的纯底层写法,2.x引入动态图模式Eager Execution和梯度带tf.Gradienttape。在tf2.x中,官方已经彻底废弃了session,转用动态图模式它可以让咱们可以像写普通python一样去计算矩阵

https://github.com/tensorflow/tensorflow

https://www.tensorflow.org/

https://www.tensorflow.org/tutorials

2)深度学习需要用到的库安装

python3.9下载地址

https://www.python.org/downloads/

miniconda下载地址

https://repo.anaconda.com/miniconda/

pip install tensorflow -i https://mirrors.aliyun.com/pypi/simple/

3)代码案例

import numpy as npimport tensorflow as tfRED    = "\033[31m"GREEN  = "\033[32m"YELLOW = "\033[33m"BOLD   = "\033[1m"END    = "\033[0m"# =========================================================================# 1) Activation Functions# =========================================================================def relu(x):    """Rectified Linear Unit activation function."""    return tf.maximum(x, 0.0)def sigmoid(x):    """Stable Sigmoid activation function clipped to avoid numerical overflow."""    x = tf.clip_by_value(x, -500.0, 500.0)    return 1.0 / (1.0 + tf.exp(-x))# =========================================================================# 2) Neural Network Architecture via TensorFlow (Two-Layer Perceptron)#    Structure: Input Layer -> Hidden Layer(ReLU) -> Output Layer(Sigmoid)# =========================================================================class TensorFlowWAFNeuralNetwork:    def __init__(self, input_dim, hidden_dim, output_dim=1, lr=0.01):        """        Initializes the model architecture and trainable variables.        Uses He/Xavier-style random normal initialization to prevent gradient issues.        """        self.lr = lr        tf.random.set_seed(42)  # For reproducible results        # ----- Layer 1 Parameters (Input -> Hidden) -----        self.W1 = tf.Variable(            tf.random.normal([input_dim, hidden_dim]) * tf.sqrt(2.0 / input_dim),            name="W1", dtype=tf.float32        )        self.b1 = tf.Variable(tf.zeros([1, hidden_dim]), name="b1", dtype=tf.float32)        # ----- Layer 2 Parameters (Hidden -> Output) -----        self.W2 = tf.Variable(            tf.random.normal([hidden_dim, output_dim]) * tf.sqrt(2.0 / hidden_dim),            name="W2", dtype=tf.float32        )        self.b2 = tf.Variable(tf.zeros([1, output_dim]), name="b2", dtype=tf.float32)        # Optimizer responsible for updating weights based on gradients        self.optimizer = tf.keras.optimizers.SGD(learning_rate=self.lr)    def forward(self, X):        """        Forward propagation pass.        Computes the final risk probability (0.0 to 1.0) for the input traffic.        """        # Hidden layer: Z1 = X * W1 + b1 -> A1 = ReLU(Z1)        self.Z1 = tf.matmul(X, self.W1) + self.b1        self.A1 = relu(self.Z1)        # Output layer: Z2 = A1 * W2 + b2 -> A2 = Sigmoid(Z2)        self.Z2 = tf.matmul(self.A1, self.W2) + self.b2        self.A2 = sigmoid(self.Z2)        return self.A2    def compute_loss(self, y_true, y_pred):        """        Computes Binary Cross Entropy Loss between ground truth and predictions.        Includes epsilon (eps) to prevent log(0) undefined numerical errors.        """        y_true = tf.cast(y_true, tf.float32)        eps = 1e-15        loss = -tf.reduce_mean(            y_true * tf.math.log(y_pred + eps) +            (1.0 - y_true) * tf.math.log(1.0 - y_pred + eps)        )        return loss    def train_step(self, X, y_true):        """        Executes a single optimization step.        Records execution on GradientTape, calculates derivatives, and updates parameters.        """        # 2.0        with tf.GradientTape() as tape:            y_pred = self.forward(X)            loss = self.compute_loss(y_true, y_pred)        # Automatically calculate gradients for all trainable parameters        trainable_variables = [self.W1, self.b1, self.W2, self.b2]        gradients = tape.gradient(loss, trainable_variables)        # Apply computed gradients to shift parameters towards the global minimum        self.optimizer.apply_gradients(zip(gradients, trainable_variables))        return loss.numpy()    def fit(self, X, y, epochs=1000, verbose_interval=100):        """        Main training loop that iterates over multiple epochs to minimize the loss.        """        X = tf.convert_to_tensor(X, dtype=tf.float32)        y = tf.convert_to_tensor(y, dtype=tf.float32)        print(f"{BOLD}{YELLOW}===== AI Threat Perception System: Training TensorFlow Neural Network ====={END}")        for epoch in range(1, epochs + 1):            loss_val = self.train_step(X, y)            if epoch % verbose_interval == 0 or epoch == 1:                print(f"Epoch {epoch:04d}/{epochs} | Loss: {loss_val:.6f}")        print(f"\n{BOLD}{GREEN}Model training completed! Deep learning parameters convergence established.{END}\n")    def predict(self, X):        """        Inference method used to evaluate incoming runtime traffic data.        """        X = tf.convert_to_tensor(X, dtype=tf.float32)        proba = self.forward(X)        return proba.numpy().flatten()# =========================================================================# 3) Data Preprocessing, Training, and Evaluation#    Features: [Request Rate, 404 Frequency, Is UserAgent Bot, Packet Size MB]#    Labels: 0 = Normal User, 1 = Cyber Attack / Malicious Scanner# =========================================================================if __name__ == "__main__":    # Raw training dataset matrix    X_data = np.array([        [2.1,  0.0,  0, 0.1],   # Normal traffic        [95.4, 42.0, 1, 8.5],   # Vulnerability Scan (Attack)        [4.5,  1.0,  0, 0.5],   # Normal traffic        [1.2,  0.0,  0, 0.0],   # Normal traffic        [88.0, 35.0, 1, 12.1],  # Credential Stuffing (Attack)    ], dtype=np.float32)    # Expected true targets    y_data = np.array([[0], [1], [0], [0], [1]], dtype=np.float32)    visitor_names = ["User_A", "Attacker_B", "User_C", "User_D", "Attacker_E"]    # OPTIMIZATION: Feature scaling (Z-score normalization) to prevent gradient explosion    mean = X_data.mean(axis=0)    std = X_data.std(axis=0) + 1e-8    X_data_scaled = (X_data - mean) / std    # Instantiate model: 4 input dimensions, 8 hidden layer nodes, Learning Rate = 0.1    model = TensorFlowWAFNeuralNetwork(input_dim=4, hidden_dim=8, lr=0.1)    # Train the neural network model    model.fit(X_data_scaled, y_data, epochs=1000, verbose_interval=200)    # Run predictions over scaled data    probabilities = model.predict(X_data_scaled)    classification_threshold = 0.5    print("-" * 75)    print(f"{BOLD}{YELLOW}===== AI WAF Gateway: Real-Time Live Traffic Inspection Active ====={END}")    print("-" * 75)    for name, score in zip(visitor_names, probabilities):        if score > classification_threshold:            action = f"{BOLD}{RED}[BLOCK & DROP] - Intercepted by AI Neural Network! (Risk Prob: {score*100:.2f}%){END}"        else:            action = f"{BOLD}{GREEN}[PASS] - Clear Traffic. (Risk Prob: {score*100:.2f}%){END}"        print(f"Live Flow: {name:<12} -> Predicted: {action}")    print("-" * 75)

过去帮客户写的AI案例请看这里:

https://space.bilibili.com/1456034618/lists/3151496?type=season

相关学习资料