百度网盘提取码:lala
二、代码运行环境: Tensorflow-gpu==2.4.0 Python==3.7 三、训练代码如下所示:import tensorflow as tf import os import pandas as pd import matplotlib.pyplot as plt # 环境变量配置 os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices' os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true' # 数据的读取 data = pd.read_csv(r'dataset/getter.csv') # 数据的展示 plt.scatter(data.Education, data.Income) plt.show() # 模型的构建 x = data.Education y = data.Income model = tf.keras.Sequential() model.add(tf.keras.layers.Dense(1, input_shape=(1,))) # 模型的相关配置 model.compile( optimizer='adam', loss='mse' ) # 模型的训练 history = model.fit(x, y, epochs=60000, batch_size=20) # 模型的预测 pre_y = model.predict(x) pre_y = pre_y.flatten() # 预测结果的展示 plt.scatter(x, y) plt.plot(x, pre_y, 'red') plt.show() # 模型的保存 model.save(r'model_data/model.h5')四、预测代码如下所示:
import tensorflow as tf import os import pandas as pd import matplotlib.pyplot as plt # 环境变量配置 os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices' os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true' # 数据的读取 data = pd.read_csv(r'dataset/getter.csv') x = data.Education y = data.Income # 模型的加载 pre_model = tf.keras.models.load_model(r'model_data/model.h5') # 结果的预测 pre_y = pre_model.predict(x) # 预测结果的展示 plt.scatter(x, y) plt.plot(x, pre_y, 'red') plt.show()五、预测结果展示
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)