0% found this document useful (0 votes)
9 views6 pages

Practical

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Practical

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical- 5

Write a program to build auto-encoder in keras

Code:-
import numpy as np
from keras.layers import Input, Dense
from keras.models import Model

# Generate some random data for demonstration


data = np.random.rand(1000, 50)

# Define the dimensions of the input and encoding layers


input_dim = data.shape[1]
encoding_dim = 10 # Choose an arbitrary size for the encoding layer

# Define the input layer


input_layer = Input(shape=(input_dim,))

# Define the encoding layer


encoded = Dense(encoding_dim, activation='relu')(input_layer)

# Define the decoding layer


decoded = Dense(input_dim, activation='sigmoid')(encoded)

# Create the autoencoder model


autoencoder = Model(input_layer, decoded)

# Compile the model


autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

# Train the autoencoder


autoencoder.fit(data, data, epochs=50, batch_size=32, shuffle=True)

# Once trained, you can use the encoder part to get the encoded
representation of the input data
encoder = Model(input_layer, encoded)
encoded_data = encoder.predict(data)

# You can also use the decoder part to reconstruct the input data from the
encoded representation
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
decoder = Model(encoded_input, decoder_layer(encoded_input))
reconstructed_data = decoder.predict(encoded_data)

output:-
Practical - 6
Write a program to implement basic reinforcement learning algorithm to teach a
bot to reach its destination.
Code:-
import numpy as np

# Define the grid world


GRID_SIZE = 5
START_STATE = (0, 0)
END_STATE = (4, 4)

# Define actions
ACTIONS = ['UP', 'DOWN', 'LEFT', 'RIGHT']

# Define rewards
REWARDS = {
(3, 4): 100, # Reward for reaching the destination
(1, 1): -10, # Penalty for entering a specific state
(2, 2): -5 # Penalty for entering a specific state
}

# Initialize Q-values
Q_values = np.zeros((GRID_SIZE, GRID_SIZE, len(ACTIONS)))

# Define parameters
LEARNING_RATE = 0.1
DISCOUNT_FACTOR = 0.9
EPISODES = 1000
EPSILON = 0.1

# Function to choose action using epsilon-greedy policy


def choose_action(state):
if np.random.uniform(0, 1) < EPSILON:
return np.random.choice(ACTIONS)
else:
return ACTIONS[np.argmax(Q_values[state[0]][state[1]])]

# Function to update Q-values using Q-learning


def update_Q_values(state, action, reward, next_state):
max_next_reward = np.max(Q_values[next_state[0]][next_state[1]])
Q_values[state[0]][state[1]][ACTIONS.index(action)] += \
LEARNING_RATE * (reward + DISCOUNT_FACTOR * max_next_reward -
Q_values[state[0]][state[1]][ACTIONS.index(action)])

# Function to perform one episode of training


def run_episode():
state = START_STATE
while state != END_STATE:
action = choose_action(state)
next_state = state

if action == 'UP':
next_state = (max(state[0] - 1, 0), state[1])
elif action == 'DOWN':
next_state = (min(state[0] + 1, GRID_SIZE - 1), state[1])
elif action == 'LEFT':
next_state = (state[0], max(state[1] - 1, 0))
elif action == 'RIGHT':
next_state = (state[0], min(state[1] + 1, GRID_SIZE - 1))

reward = 0
if next_state in REWARDS:
reward = REWARDS[next_state]

update_Q_values(state, action, reward, next_state)


state = next_state

# Train the agent


for _ in range(EPISODES):
run_episode()

# Function to get the optimal path


def get_optimal_path():
path = []
state = START_STATE
while state != END_STATE:
action = ACTIONS[np.argmax(Q_values[state[0]][state[1]])]
path.append((state, action))
if action == 'UP':
state = (max(state[0] - 1, 0), state[1])
elif action == 'DOWN':
state = (min(state[0] + 1, GRID_SIZE - 1), state[1])
elif action == 'LEFT':
state = (state[0], max(state[1] - 1, 0))
elif action == 'RIGHT':
state = (state[0], min(state[1] + 1, GRID_SIZE - 1))
path.append((state, 'GOAL'))
return path

# Print the optimal path


optimal_path = get_optimal_path()
for step in optimal_path:
print(step)
Practical – 7

(a) Write a program to implement a recurrent neural network.


Code:-
import numpy as np

# Define the sigmoid activation function


def sigmoid(x):
return 1 / (1 + np.exp(-x))

# Define the RNN class


class RNN:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size

self.Wxh = np.random.randn(hidden_size, input_size)


self.Whh = np.random.randn(hidden_size, hidden_size)
self.Why = np.random.randn(output_size, hidden_size)
self.bh = np.zeros((hidden_size, 1))
self.by = np.zeros((output_size, 1))

def forward(self, inputs):


h = np.zeros((self.hidden_size, 1))
for x in inputs:
h = np.tanh(np.dot(self.Wxh, x) + np.dot(self.Whh, h) +
self.bh)
output = np.dot(self.Why, h) + self.by
return output

# Example usage
input_size = 3
hidden_size = 4
output_size = 2

rnn = RNN(input_size, hidden_size, output_size)


inputs = [np.random.randn(input_size, 1) for _ in range(5)]
output = rnn.forward(inputs)
print(output)

(b) Write a program to implement LSTM and perform time series


analysis using LSTM.
Code:-
import numpy as np
from keras.models import Sequential
from keras.layers import LSTM, Dense

# Generate some random data for demonstration


data = np.random.randn(1000, 1)
# Prepare the data for LSTM
def prepare_data(data, n_steps):
X, y = [], []
for i in range(len(data)):
end_ix = i + n_steps
if end_ix > len(data)-1:
break
X.append(data[i:end_ix, 0])
y.append(data[end_ix, 0])
return np.array(X), np.array(y)

n_steps = 3
X, y = prepare_data(data, n_steps)

# Reshape data for LSTM [samples, timesteps, features]


X = X.reshape(X.shape[0], X.shape[1], 1)

# Define the LSTM model


model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_steps, 1)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')

# Fit the model


model.fit(X, y, epochs=200, verbose=0)

# Make predictions
predictions = model.predict(X, verbose=0)

You might also like