# Simulating A Function And Optimization

## Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

{
"cells": [
{
"cell_type": "markdown",
"source": [
"# HW1-1 Simulate a Function"
]
},
{
"cell_type": "code",
"execution_count": 1,
"outputs": [],
"source": [
"import os\n",
"import tensorflow as tf\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sklearn import datasets\n",
"from sklearn.preprocessing import MinMaxScaler\n",
"from sklearn.decomposition import PCA\n",
"\n",
"class DeepNeuralNetwork:\n",
"    \"\"\"\n",
"    A class that models multilayer Perceptron - Deep neural network.\n",
"    Input parameters:\n",
"    units: a list that defines the number of neurons for each hidden layer\n",
"    \"\"\"\n",
"    def __init__(self, units: list, learning_rate: float):\n",
"\n",
"       self.units = units\n",
"       self.learning_rate = learning_rate\n",
"       \n",
"       # initiate a graph \n",
"       self.g = tf.Graph()\n",
"       with self.g.as_default():\n",
"            # make a placeholders for input values and targets\n",
"            self.tf_x = tf.compat.v1.placeholder(\n",
"                tf.float32, shape=(None, 1), name=\"tf_x\"\n",
"            )\n",
"            self.tf_y = tf.compat.v1.placeholder(tf.float32, (None), name=\"tf_y\")\n",
"            self.build_model() # build a model\n",
"\n",
"    def make_fc_layer(\n",
"       self, input_tensor, n_output_units: int, activation_fn=None, name=\"\"\n",
"    ):\n",
"       \"A function that creates fully connected layer\"\n",
"       \n",
"       input_shape = input_tensor.get_shape().as_list()[1:] # get input shape of the input tensor\n",
"       n_input_units = np.prod(input_shape)\n",
"\n",
"       weights_shape = [n_input_units, n_output_units]\n",
"       \n",
"       # initialize weights for a layer, use uniform initialization\n",
"       weights = tf.Variable(\n",
"            tf.random.uniform(weights_shape, minval=-0.5, maxval=0.5),\n",
"            name=f\"{name}_fc_weights\",\n",
"            shape=weights_shape,\n",
"            trainable=True,\n",
"       )\n",
"       # initialze bias\n",
"       bias = tf.Variable(\n",
"            tf.zeros(shape=[n_output_units], name=f\"{name}_fc_bias\"), trainable=True\n",
"       )\n",
"       \n",
"       # make output tensor by multiplying input tensor and added weights and add bias\n",
"       layer = tf.matmul(input_tensor, weights)\n",
"       layer = tf.nn.bias_add(layer, bias, name=\"net_pre_activation\")\n",
"       \n",
"       if activation_fn is not None:\n",
"            # function activation ( add non-linearity to the model )\n",
"            layer = activation_fn(layer, name=\"activation\")\n",
"\n",
"       return layer\n",
"\n",
"    def build_model(self):\n",
"       \"\"\"\n",
"       A function that builds fully connected deep neural network\n",
"       \"\"\"\n",
"\n",
"       self.layers = [self.tf_x] # list of all layers\n",
"       input_tensor = self.tf_x\n",
"       \n",
"       for i, unit in enumerate(self.units):\n",
"            if i + 1 == len(self.units):\n",
"                fc_layer = self.make_fc_layer(input_tensor, 1, name=\"output_layer\")\n",
"                self.layers.append(fc_layer)\n",
"                continue\n",
"\n",
"            fc_layer = self.make_fc_layer(\n",
"                input_tensor, unit...
\$150.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

### Find A Tutor

View available Computer Science - Other Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.