归一化后,如何使用Tensorflow训练和构建模型?
关于鲍鱼数据集的模型训练和构建可以使用'compile'和'fit'方法分别完成。'fit'方法也以epoch数作为参数。
阅读更多: 什么是TensorFlow以及Keras如何与TensorFlow协作创建神经网络?
我们将使用鲍鱼数据集,其中包含一组鲍鱼的测量数据。鲍鱼是一种海蜗牛。目标是根据其他测量结果预测年龄。
我们使用Google Colaboratory运行以下代码。Google Colab或Colaboratory帮助通过浏览器运行Python代码,无需任何配置,并且可以免费访问GPU(图形处理单元)。Colaboratory构建在Jupyter Notebook之上。
print("The model is being compiled") norm_abalone_model.compile(loss = tf.losses.MeanSquaredError(),optimizer = tf.optimizers.Adam()) print("The model is being fit to the data") norm_abalone_model.fit(abalone_features, abalone_labels, epochs=8)
代码来源:https://tensorflowcn.cn/tutorials/load_data/csv
输出
The model is being compiled The model is being fit to the data Epoch 1/8 104/104 [==============================] - 0s 989us/step - loss: 98.3651 Epoch 2/8 104/104 [==============================] - 0s 945us/step - loss: 65.4568 Epoch 3/8 104/104 [==============================] - 0s 922us/step - loss: 21.7297 Epoch 4/8 104/104 [==============================] - 0s 912us/step - loss: 6.3429 Epoch 5/8 104/104 [==============================] - 0s 988us/step - loss: 5.0949 Epoch 6/8 104/104 [==============================] - 0s 958us/step - loss: 4.9868 Epoch 7/8 104/104 [==============================] - 0s 1ms/step - loss: 4.8982 Epoch 8/8 104/104 [==============================] - 0s 1ms/step - loss: 4.7936 <tensorflow.python.keras.callbacks.History at 0x7fda8213c898>
解释
- 构建归一化层后,使用训练数据训练模型。
- 训练完成后,使用'Model.fit'方法将特征和标签传递给数据。
广告