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