Merge pull request #447 from gsundeep-tech/chapter_12/multi_layer
Chapter 12: multi output custom layer examplemain
commit
c6cc45733c
|
@ -1855,13 +1855,14 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 144,
|
"execution_count": 2,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class MyMultiLayer(keras.layers.Layer):\n",
|
"class MyMultiLayer(keras.layers.Layer):\n",
|
||||||
" def call(self, X):\n",
|
" def call(self, X):\n",
|
||||||
" X1, X2 = X\n",
|
" X1, X2 = X\n",
|
||||||
|
" print(\"X1.shape: \", X1.shape ,\" X2.shape: \", X2.shape) # Debugging of custom layer\n",
|
||||||
" return X1 + X2, X1 * X2\n",
|
" return X1 + X2, X1 * X2\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def compute_output_shape(self, batch_input_shape):\n",
|
" def compute_output_shape(self, batch_input_shape):\n",
|
||||||
|
@ -1871,7 +1872,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 145,
|
"execution_count": 3,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
@ -1882,7 +1883,26 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 146,
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def split_data(data):\n",
|
||||||
|
" columns_count = data.shape[-1]\n",
|
||||||
|
" half = columns_count // 2\n",
|
||||||
|
" return data[:, :half], data[:, half:]\n",
|
||||||
|
"\n",
|
||||||
|
"X_train_scaled_A, X_train_scaled_B = split_data(X_train_scaled)\n",
|
||||||
|
"X_valid_scaled_A, X_valid_scaled_B = split_data(X_valid_scaled)\n",
|
||||||
|
"X_test_scaled_A, X_test_scaled_B = split_data(X_test_scaled)\n",
|
||||||
|
"\n",
|
||||||
|
"# Printing the splitted data shapes\n",
|
||||||
|
"X_train_scaled_A.shape, X_train_scaled_B.shape"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
@ -1891,6 +1911,49 @@
|
||||||
"outputs1, outputs2 = MyMultiLayer()((inputs1, inputs2))"
|
"outputs1, outputs2 = MyMultiLayer()((inputs1, inputs2))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"outputs1, outputs2 = MyMultiLayer()((X_train_scaled_A, X_train_scaled_B))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"input_A = keras.layers.Input(shape=X_train_scaled_A.shape[-1])\n",
|
||||||
|
"input_B = keras.layers.Input(shape=X_train_scaled_B.shape[-1])\n",
|
||||||
|
"hidden_A, hidden_B = MyMultiLayer()((input_A, input_B))\n",
|
||||||
|
"hidden_A = keras.layers.Dense(30, activation='selu')(hidden_A)\n",
|
||||||
|
"hidden_B = keras.layers.Dense(30, activation='selu')(hidden_B)\n",
|
||||||
|
"concat = keras.layers.Concatenate()((hidden_A, hidden_B))\n",
|
||||||
|
"output = keras.layers.Dense(1)(concat)\n",
|
||||||
|
"model = keras.Model(inputs=[input_A, input_B], outputs=[output])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"model.compile(loss='mse', optimizer='nadam')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"model.fit((X_train_scaled_A, X_train_scaled_B), y_train, epochs=2, validation_data=((X_valid_scaled_A, X_valid_scaled_B), y_valid))"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
@ -3883,7 +3946,7 @@
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.7.10"
|
"version": "3.8.10"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
Loading…
Reference in New Issue