My First AI Model
If you will check the below table, then you will get to know that Y is dependent on X and the relationship is. y = 2x - 1
You can also draw the relationship. Below I draw the relationship? in a graph.?
Now I will use TensorFlow to predict the value of y by training the model using the above data set. I will use the Keras library over TensorFlow.
Neural Network Layer?
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
units=1 represents the total number of neurons going to be used to predict the model, and input_shape=[1] represents the number of inputs. Here we are going to?input the value of X, and the model will predict the value of Y.
Compile the model.?
model.compile(optimizer='sgd', loss='mean_squared_error')
What is a loss function??
The loss function calculates the difference between the predicted values and the actual value.?
The mean squared error (MSE) loss function, also known as L2 loss or quadratic loss, is a function that measures the average of the squared differences between predicted and actual values.?
Optimizer
In machine learning, an optimizer is an algorithm that adjusts a model's parameters to reduce a loss function and improve its performance.
Stochastic gradient descent (SGD) is an optimization algorithm that's used to train models in machine learning and artificial intelligence.
Train the model.?
model.fit(xs, ys, epochs=500)
The fit function will make the guess 500 times, and the loss function and optimizer will work on making the guess correct.?
Use the model?
print(model.predict(np.array([10.0])))
Output
[[18.98005]]
Let’s discuss other keras library components used in the above code.?
Sequential
The Sequential class in Keras is a fundamental building block for creating neural networks, particularly those with a linear stack of layers. It offers a straightforward and intuitive way to define the architecture of your model.?
Key Concepts:
Linear Stack of Layers: In a Sequential model, layers are arranged in a linear sequence. The output of one layer becomes the input to the next layer.
Simplicity: The Sequential API is designed for simplicity, making it ideal for beginners and quick prototyping.
Flexibility: While it's well-suited for simple architectures, it can still be used for more complex models with the help of advanced techniques like functional API or model subclassing.
The Dense layer is one of the most fundamental layers in neural networks, and it's extensively used in Keras to construct deep learning models. It's often referred to as a fully connected layer because every neuron in the layer is connected to every neuron in the preceding layer.
Complete code in Google Colab.?