Chapter 12 Multi Layer Example
parent
fd4c180f93
commit
89cd351fe9
|
@ -1855,13 +1855,29 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 144,
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from sklearn.datasets import fetch_california_housing\n",
|
||||
"from sklearn.model_selection import train_test_split\n",
|
||||
"from sklearn.preprocessing import StandardScaler\n",
|
||||
"\n",
|
||||
"housing = fetch_california_housing()\n",
|
||||
"X_train_full, X_test, y_train_full, y_test = train_test_split(\n",
|
||||
" housing.data, housing.target.reshape(-1, 1), random_state=42)\n",
|
||||
"X_train, X_valid, y_train, y_valid = train_test_split(\n",
|
||||
" X_train_full, y_train_full, random_state=42)\n",
|
||||
"\n",
|
||||
"scaler = StandardScaler()\n",
|
||||
"X_train_scaled = scaler.fit_transform(X_train)\n",
|
||||
"X_valid_scaled = scaler.transform(X_valid)\n",
|
||||
"X_test_scaled = scaler.transform(X_test)\n",
|
||||
"\n",
|
||||
"class MyMultiLayer(keras.layers.Layer):\n",
|
||||
" def call(self, 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",
|
||||
"\n",
|
||||
" def compute_output_shape(self, batch_input_shape):\n",
|
||||
|
@ -1871,7 +1887,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 145,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
|
@ -1882,7 +1898,26 @@
|
|||
},
|
||||
{
|
||||
"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": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
|
@ -1891,6 +1926,49 @@
|
|||
"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",
|
||||
"metadata": {},
|
||||
|
@ -3883,7 +3961,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.7.10"
|
||||
"version": "3.8.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
Loading…
Reference in New Issue