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

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')
主站蜘蛛池模板: 息烽县| 旺苍县| 黎城县| 麻栗坡县| 建始县| 工布江达县| 滁州市| 文昌市| 贵定县| 张北县| 张北县| 乳源| 乌海市| 大石桥市| 大石桥市| 西城区| 万全县| 夏河县| 龙海市| 朝阳市| 和田市| 大新县| 郁南县| 明光市| 礼泉县| 沂源县| 司法| 监利县| 济阳县| 财经| 保康县| 彭阳县| 抚远县| 镶黄旗| 伊川县| 灵石县| 崇阳县| 永平县| 高阳县| 武安市| 盘山县|