Tensorflow 2.6

1、安装

  1. conda create --name tf2.6 python=3.8
  2. activate tf2.6
  3. pip install tensorflow==2.6.0
  4. pip install keras==2.6.0
  5. pip install matplotlib
  6. pip install jupyter
  7. # 安装主题
  8. pip install jupyterthemes
  9. # 查看主题
  10. jt -l
  11. # 主题切换
  12. jt -t chesterish
  13. # 启动
  14. jupyter notebook --ip=127.0.0.1 --port=8000
  • Test
  1. import tensorflow as tf
  2. from tensorflow.keras.datasets import mnist
  3. import matplotlib.pyplot as plt
  4. if __name__ == '__main__':
  5. x_data = [1, 2, 3, 4, 5, 6, 7, 8] # X轴坐标点
  6. y_data = [1, 2, 3, 4, 5, 6, 7, 8] # Y轴坐标点
  7. plt.plot(x_data, y_data)
  8. plt.show()
  9. # 1. 导入TensorFlow
  10. (x_train, y_train), (x_test, y_test) = mnist.load_data()
  11. print(x_train)
  12. x_train, x_test = x_train / 255.0, x_test / 255.0
  13. # 4. 搭建模型,选择优化器和损失函数
  14. model = tf.keras.models.Sequential([
  15. tf.keras.layers.Flatten(input_shape=(28, 28)),
  16. tf.keras.layers.Dense(128, activation='relu'),
  17. tf.keras.layers.Dropout(0.2),
  18. tf.keras.layers.Dense(10, activation='softmax')
  19. ])
  20. # 5. 模型编译
  21. model.compile(optimizer='adam',
  22. loss='sparse_categorical_crossentropy',
  23. metrics=['accuracy'])
  24. # 6. 训练并验证模型
  25. model.fit(x_train, y_train, epochs=5)
  26. # 7. 评估模型
  27. model.evaluate(x_test, y_test, verbose=2)