📦 安装TensorFlow
- 使用pip安装:
pip install tensorflow
- 验证安装:
import tensorflow as tf print(tf.__version__)
📘 基本概念
- 张量(Tensor):数据的基本载体,可以是标量、向量或矩阵
- 计算图(Graph):定义运算流程的结构化表示
- 会话(Session):执行计算图的运行环境
💻 代码示例
# 导入库
import tensorflow as tf
# 定义模型
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation='relu', input_shape=(None, 5)),
tf.keras.layers.Dense(1)
])
# 编译模型
model.compile(optimizer='adam', loss='mean_squared_error')
# 训练模型
model.fit(X_train, y_train, epochs=10)
🧠 模型训练流程
💾 保存与加载模型
# 保存模型
model.save('my_model.h5')
# 加载模型
loaded_model = tf.keras.models.load_model('my_model.h5')
📚 扩展阅读
点击了解更多Keras实战技巧 → /community/tech/tutorials/keras_tutorial
📌 附:TensorFlow官方文档
访问TensorFlow中文社区 → /community/tech/tutorials/tensorflow_documentation