📚 1. 环境准备
安装 TensorFlow
pip install tensorflow
tensorflow_logo验证安装
运行以下代码检查版本:import tensorflow as tf print(tf.__version__)
🧠 2. 核心概念
张量(Tensor)
数据的多维数组形式,可理解为数据的容器 📦机器学习流程计算图(Graph)
操作(Operations)的有向图结构,用于定义模型逻辑 🗺️会话(Session)
执行计算图的上下文环境,通过tf.Session()
启动 🔄
📈 3. 实战示例
步骤 1: 导入库
import tensorflow as tf
步骤 2: 构建模型
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation='relu', input_shape=(5,)),
tf.keras.layers.Dense(1)
])
步骤 3: 编译模型
model.compile(optimizer='adam', loss='mean_squared_error')
步骤 4: 训练模型
model.fit(x_train, y_train, epochs=10)
🌐 扩展阅读
代码运行