keras
keras
Installing Keras, Tensorflow and Pytorch libraries and making use of them
Aim:
To install Keras, Tensorflow and Pytorch libraries and making use of them.
Installation:
Aim:
To write a python program in Tensorflow & Keras.
Algorithm
1. Import numpy for data manipulation.
2. Import necessary classes from tensorflow.kerasm for building and training the neural
network.
3. Create a dataset with 100 samples, each having 8 features. The features are generated as
random numbers between 0 and 1.
4. Create binary target values (0 or 1) corresponding to each sample.
5. Create an empty neural network model using the Sequential API.
6. Add a Dense layer with 12 neurons, using the ReLU (Rectified Linear Unit) activation
function. This layer will take input with 8 features.
7. Add another Dense layer with 8 neurons, using the ReLU activation function.
8. Add an output Dense layer with 1 neuron, using the Sigmoid activation function to
produce a probability value for binary classification.
9. Set the optimizer to Adam, which adjusts the learning rate during training.
10. Use binary cross-entropy as the loss function, appropriate for binary classification tasks.
11. Specify accuracy as the evaluation metric to monitor the model's performance.
12. Fit the model on the generated data for 10 epochs with a batch size of 10.
13. During training, the model will learn to map features to the binary target values.
14. Generate 10 new samples with 8 features each for which predictions will be made.
15. Use the trained model to predict the probabilities of the new samples belonging to the
positive class (1).
16. Print the predicted probabilities for the new data.
Program:
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Generate random data (replace with your own data)
X = np.random.rand(100, 8)
y = np.random.randint(2, size=100)
Output:
Epoch 1/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 14s 6ms/step - accuracy: 0.8588 - loss: 0.4773
Epoch 2/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 20s 6ms/step - accuracy: 0.9564 - loss: 0.1512
Epoch 3/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 19s 5ms/step - accuracy: 0.9672 - loss: 0.1078
Epoch 4/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 9s 5ms/step - accuracy: 0.9728 - loss: 0.0886
Epoch 5/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 8s 4ms/step - accuracy: 0.9766 - loss: 0.0740
313/313 - 1s - 3ms/step - accuracy: 0.9768 - loss: 0.0797
[0.07969701290130615, 0.9768000245094299]
PyTorch Program:
Aim:
To write a python program in PyTorch.
Algorithm:
Program:
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.datasets as datasets
import torchvision.transforms as transforms
Output:
Result:
Thus the installation of the Keras, TensorFlow, and PyTorch libraries was completed, and they were
put to use.