Tensorflow 2.6
1、安装
conda create --name tf2.6 python=3.8activate tf2.6pip install tensorflow==2.6.0pip install keras==2.6.0pip install matplotlibpip install jupyter# 安装主题pip install jupyterthemes# 查看主题jt -l# 主题切换jt -t chesterish# 启动jupyter notebook --ip=127.0.0.1 --port=8000
import tensorflow as tffrom tensorflow.keras.datasets import mnistimport matplotlib.pyplot as pltif __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)