官术网_书友最值得收藏!

Training the Keras model

Now that our network has been built and compiled, all that's left is to train it. Much like in Python's scikit-learn, you can do that by calling .fit() on the model instance, as shown in the following code:

model.fit(x=data["train_X"], y=data["train_y"], batch_size=32, epochs=200, verbose=1, validation_data=(data["val_X"], data["val_y"]))

Let us walk through a few of the important arguments the Keras fit method takes. I will assume that you're familiar with mini-batch gradient descent and training epochs but if you aren't, please check Chapter 1The Building Blocks of Deep Learning, for an overview. The important arguments in the Keras fit model are as follows:

  • batch_size: Keras defaults to a batch size of 32. The batch size is the size of the mini-batch Keras will use. Of course, this means that Keras assumes you want to use mini-batch gradient descent. If, for some reason, you don't want to use mini-batch gradient, you can set batch_size=None.
  • epochs: An epoch is just a single pass over the entire training set. In practice, you'll need to monitor your network as it trains to learn when the network has converged, so epochs is a somewhat learnable hyperparameter. Later, we will see that it's possible to save the weights of our model every epoch, or even every epoch that's better than the last. Once we know how to do that, we can choose the epoch we think is best and implement a sort of human-based early stopping.
  • validation_data: Here, we are specifying our validation set. At the end of every epoch, Keras will test the model on the validation set and output the results using the loss function and any other metrics you've specified. Alternatively, you can set validation_split to a float value specifying the percentage of the train set you'd like to use for validation. Both options work fine, but I prefer to be very explicit when it comes to dataset splits.
  • verbose: This is somewhat self-explanatory; however, it merits a quick mention. verbose=1 outputs a progress bar that shows the status of the current epoch and, at the end of the epoch, Keras will output training and validation loss. verbose can also be set to 2, which outputs loss information every mini-batch, and 0, which makes Keras silent.
主站蜘蛛池模板: 朝阳县| 大理市| 永宁县| 左云县| 麻江县| 奉节县| 商都县| 扶风县| 启东市| 阳曲县| 紫云| 淅川县| 隆回县| 东乌珠穆沁旗| 铁岭县| 博罗县| 图木舒克市| 龙海市| 广平县| 尚志市| 临江市| 隆化县| 高碑店市| 正蓝旗| 正蓝旗| 东乌珠穆沁旗| 温州市| 郧西县| 进贤县| 富裕县| 保定市| 大姚县| 成都市| 龙游县| 扬州市| 鹿泉市| 会泽县| 商丘市| 平谷区| 秭归县| 两当县|