0% found this document useful (0 votes)
2 views

Deep Learning Program

Uploaded by

ashikaapsara515
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)
2 views

Deep Learning Program

Uploaded by

ashikaapsara515
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/ 5

Autoencoder

n=int(input("Enter the number of digits :"))

print("n=",n)

i=0

sum=0

w=1

while (i<n-1):

w*=100

i+=1

i=0

while (i<n):

sum*=100

print("Enter the",i+1,"digit :")

x=int(input())

sum+=x

print("W",i+1,"is:",w)

w/=100

i+=1

print("Sum=",sum)

while (sum>0):

y=sum%100

print("the",n,"th digit is:",y)

sum=int(sum/100)

n-=1

GRU

import tensorflow as tf

from tensorflow import keras

from tensorflow.keras import layers

model = keras.Sequential()

model.add(layers.GRU(64, input_shape=(28, 28)))

model.add(layers.BatchNormalization())
model.add(layers.Dense(10))

print(model.summary())

mnist = keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train, x_test = x_train/255.0, x_test/255.0

x_validate, y_validate = x_test[:-10], y_test[:-10]

x_test, y_test = x_test[-10:], y_test[-10:]

model.compile(

loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),

optimizer="sgd",

metrics=["accuracy"],

model.fit(

x_train, y_train, validation_data=(x_validate, y_validate), batch_size=64, epochs=10

for i in range(10):

result = tf.argmax(model.predict(tf.expand_dims(x_test[i], 0)), axis=1)

print(result.numpy(), y_test[i])

Bidirectional LSTM

import numpy as np

from keras.models import Sequential

from keras.utils import pad_sequences

from keras.layers import Dropout

from keras.layers import Dense, Embedding, LSTM, Bidirectional

from keras.datasets import imdb

(x_train, y_train),(x_test, y_test) = imdb.load_data(num_words=10000)

max_len = 200

x_train= pad_sequences(x_train,padding='post', maxlen=max_len)

x_test = pad_sequences(x_test,padding='post', maxlen=max_len)

y_test = np.array(y_test)

y_train = np.array(y_train)
model = Sequential()

model.add(Embedding(10000, 128, input_length=max_len))

model.add(Bidirectional(LSTM(64)))

model.add(Dropout(0.5))

model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

history=model.fit(x_train, y_train,

batch_size=64,

epochs=4,

validation_data=[x_test, y_test])

print(history.history['loss'])

print(history.history['accuracy'])

from matplotlib import pyplot

pyplot.plot(history.history['loss'])

pyplot.plot(history.history['accuracy'])

pyplot.title('model loss vs accuracy')

pyplot.xlabel('epoch')

pyplot.legend(['loss', 'accuracy'], loc='upper right')

pyplot.show()

GRU Time Series

import numpy as np

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import GRU, Dense

data = np.array([[i for i in range(10)] for _ in range(100)])

X, y = data[:, :-1], data[:, -1]

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

model = Sequential()

model.add(GRU(64,activation='relu', input_shape=(9,1)))

model.add(Dense(1))

model.compile(optimizer='adam', loss='mse')

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


test_input = np.array([7, 8, 9, 10, 11, 12, 13, 14, 15])

test_input = test_input.reshape((1, 9, 1))

print(test_input)

predicted_value = model.predict(test_input, verbose=0)

print(f'Predicted value: {predicted_value[0][0]}')

Bidirectional GRU

import numpy as np

from keras.models import Sequential

from keras.utils import pad_sequences

from keras.layers import Dropout

from keras.layers import Dense, Embedding, GRU, Bidirectional

from keras.datasets import imdb

(x_train, y_train),(x_test, y_test) = imdb.load_data(num_words=10000)

max_len = 200

x_train= pad_sequences(x_train,padding='post', maxlen=max_len)

x_test = pad_sequences(x_test,padding='post', maxlen=max_len)

y_test = np.array(y_test)

y_train = np.array(y_train)

model = Sequential()

model.add(Embedding(10000, 128, input_length=max_len))

model.add(Bidirectional(GRU(64)))

model.add(Dropout(0.5))

model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

history=model.fit(x_train, y_train,

batch_size=64,

epochs=4,

validation_data=[x_test, y_test])

print(history.history['loss'])

print(history.history['accuracy'])

from matplotlib import pyplot


pyplot.plot(history.history['loss'])

pyplot.plot(history.history['accuracy'])

pyplot.title('model loss vs accuracy')

pyplot.xlabel('epoch')

pyplot.legend(['loss', 'accuracy'], loc='upper right')

pyplot.show()

You might also like