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

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')
主站蜘蛛池模板: 四川省| 瓮安县| 沂水县| 云霄县| 汽车| 揭东县| 台中市| 彩票| 乌拉特后旗| 高碑店市| 金山区| 腾冲县| 米脂县| 资兴市| 临猗县| 彰武县| 登封市| 平顶山市| 吴川市| 安西县| 贵定县| 新津县| 寿宁县| 山阳县| 遵义县| 南岸区| 顺昌县| 漯河市| 朔州市| 靖边县| 正阳县| 英超| 纳雍县| 克什克腾旗| 霸州市| 奎屯市| 太湖县| 平顺县| 海南省| 苏尼特左旗| 彭州市|