181 lines
4.9 KiB
Plaintext
181 lines
4.9 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "8ab45695",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#\n",
|
|
"# An example of the minimzer usage in tensor flow\n",
|
|
"# the loss function is plotted and the result in terms of a line\n",
|
|
"#"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "270932f3",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"import tensorflow as tf"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "77cd99a8",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Define the training data\n",
|
|
"train_X = np.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,\n",
|
|
" 7.042,10.791,5.313,7.997,5.654,9.27,3.1])\n",
|
|
"train_Y = np.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,\n",
|
|
" 2.827,3.465,1.65,2.904,2.42,2.94,1.3])\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f39cbcd9",
|
|
"metadata": {},
|
|
"source": [
|
|
"The input to the model is represented by the train_X \n",
|
|
"Y_train represents the target or the truth values for the training data\n",
|
|
"The model will recieve train_X and make predictions on the weights\n",
|
|
"The difference between these predictions and the actual target values\n",
|
|
"train_Y will be used to update the weights and minimize the loss function."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ed8449c3",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Define the model to a simple linear regression with only one dense layer and\n",
|
|
"# no activation function for the first layer all train_X points are input\n",
|
|
"\n",
|
|
"# model = tf.keras.models.Sequential([\n",
|
|
"# tf.keras.layers.Dense(1, input_shape=[1])\n",
|
|
"#])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "71e072b4",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# This model has 2 dense layers the first with relu activation\n",
|
|
"# and the 2nd layer has 1 output unit and uses the default\n",
|
|
"# linear activation function.\n",
|
|
"\n",
|
|
"model = tf.keras.models.Sequential([\n",
|
|
" tf.keras.layers.Dense(17, activation='relu',input_shape=[1]),\n",
|
|
" tf.keras.layers.Dense(1)\n",
|
|
"])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "5fabf184",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# different optimizer methods can be enabled\n",
|
|
"\n",
|
|
"model.compile(optimizer=tf.keras.optimizers.Adam(0.01), loss='mean_squared_error')\n",
|
|
"#model.compile(optimizer=tf.keras.optimizers.SGD(0.01), loss='mean_squared_error')\n",
|
|
"#model.compile(optimizer=tf.keras.optimizers.Adagrad(learning_rate=0.01), loss='mean_squared_error')\n",
|
|
"#model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.01), loss='mean_squared_error')\n",
|
|
"#model.compile(optimizer=tf.keras.optimizers.Ftrl(learning_rate=0.015), loss='mean_squared_error')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "22c4124f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Train the model and access training parameters\n",
|
|
"history = model.fit(train_X, train_Y, epochs=60)\n",
|
|
"print(history.params)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "46615960",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Get the weights of the Dense layer\n",
|
|
"weights = model.layers[0].get_weights()\n",
|
|
"# Print the weight matrix and bias vector\n",
|
|
"print('Weight matrix shape:', weights[0].shape)\n",
|
|
"print('Bias vector shape:', weights[1].shape)\n",
|
|
"print (weights[0])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "da12fc5b",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Plot the loss function\n",
|
|
"plt.plot(history.history['loss'])\n",
|
|
"plt.title(\"Loss Function\")\n",
|
|
"plt.xlabel(\"Epoch\")\n",
|
|
"plt.ylabel(\"Loss\")\n",
|
|
"plt.show()\n",
|
|
"\n",
|
|
"# Plot the input data and the predicted values\n",
|
|
"plt.plot(train_X, train_Y, 'ro', label=\"Original Data\")\n",
|
|
"plt.plot(train_X, model.predict(train_X), label=\"Predicted\")\n",
|
|
"plt.legend()\n",
|
|
"plt.show()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "60417d5f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.8.16"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|