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

Neural network architecture

Now that we've defined the input and output, we can take a look at the code for the network.

from keras.layers import Input, Dense
from keras.models import Model
def
build_network(input_features=None):
inputs = Input(shape=(input_features,), name="input")
x = Dense(32, activation='relu', name="hidden")(inputs)
prediction = Dense(1, activation='linear', name="final")(x)
model = Model(inputs=inputs, outputs=prediction)
model.compile(optimizer='adam', loss='mean_absolute_error')
return model

That's all there is to it! We can then use this code to build a neural network instance suitable for our problem simply by calling it, as follows:

model = build_network(input_features=10)

Before we get to that, however, let's review a few interesting parts of the preceding code:

  • Every layer is chained to the layer above it. Every layer is callable and returns a tensor. For example, our hidden layer is tied to the input layer when the hidden layer calls it: 
        x = Dense(32, activation='relu', name="hidden")(inputs)
  • Our final layer's activation function is linear. This is the same as not using any activation, which is what we want for regression.
  • Keras models need to be compiled with .compile().
  • During the compile call, you need to define the cost function and optimizer you will use. I've used MAE for the cost function in this example, as we discussed. I used Adam with default parameters as my optimizer, which we covered a bit in chapter 1. It's likely that we will eventually want to tune Adam's learning rate. Doing so is quite simple: you just need to define a custom adam instance, and use that instead:
from keras.optimizers import Adam
adam_optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(optimizer=adam_optimizer, loss='mean_absolute_error')
主站蜘蛛池模板: 通山县| 郎溪县| 石河子市| 黎平县| 景宁| 武胜县| 太谷县| 吉水县| 青海省| 长岭县| 资中县| 儋州市| 宜昌市| 定州市| 息烽县| 通道| 玉门市| 石林| 神池县| 西青区| 肃南| 二手房| 晴隆县| 南充市| 仙桃市| 大名县| 当雄县| 大悟县| 旬阳县| 拉孜县| 湖州市| 贵德县| 宁明县| 嘉善县| 扶绥县| 喀喇沁旗| 桃园市| 澄江县| 江都市| 紫阳县| 鄂托克旗|