Created
April 29, 2021 16:21
-
-
Save vipul43/0fe0ffd30ef3d3be1fed0aa88333dcc4 to your computer and use it in GitHub Desktop.
plan to implement rnn from scratch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# class RNN: | |
# def __init__(self, input_shape, hidden_shape, output_shape): | |
# self.input_shape = input_shape | |
# self.hidden_shape = hidden_shape | |
# self.output_shape = output_shape | |
# self.initialise() | |
# self.bptt_truncate = 2 | |
# def initialise(self): | |
# self.Whx = np.random.normal(0, 0.01, shape=(self.hidden_shape, self.input_shape)) | |
# self.Whh = np.random.normal(0, 0.01, shape=(self.hidden_shape, self.hidden_shape)) | |
# self.bh = np.random.normal(0, 0.01, shape=(self.hidden_shape)) | |
# self.Wqh = np.random.normal(0, 0.01, shape=(self.output_shape, self.hidden_shape)) | |
# self.bq = np.random.normal(0, 0.01, shape=(self.output_shape)) | |
# def tanh(Z): | |
# return (np.exp(Z)-np.exp(-Z))/(np.exp(Z)-np.exp(-Z)) | |
# def softmax(Z): | |
# e_x = np.exp(Z - np.max(Z)) | |
# return e_x / e_x.sum(axis=0) | |
# def forward(self, x): | |
# prev_memory = np.zeros(shape=(self.hidden_shape,1)) | |
# cache = {} | |
# for t in range(x.shape[0]): | |
# WX = np.dot(self.Whx, x[i]) | |
# UH = np.dot(self.Whh, prev_memory) | |
# H = WX + UH + self.bh | |
# H_act = self.tanh(H) | |
# Q = np.dot(self.Wqh, H_act) + self.bq | |
# Q_act = self.softmax(Q) | |
# prev_memory = H_act | |
# cache["WX" + str(t)] = WX | |
# cache["UH" + str(t)] = UH | |
# cache["H" + str(t)] = H | |
# cache["H_act" + str(t)] = H_act | |
# cache["Q" + str(t)] = Q | |
# cache["Q_act" + str(t)] = Q_act | |
# return cache | |
# def loss(ypred, ytrue): | |
# def backward(): | |
# def train(self, TrainX, TrainY, ValX, ValY, epochs=1, lr=0.001): | |
# assert(epochs>=0) | |
# for e in range(epochs): | |
# #for training | |
# Tcache = self.forward(TrainX) | |
# weights = self.backward(Tcache, TrainX, TrainY) | |
# self.update_weights(weights) | |
# #for metrics | |
# Tcache = self.forward(TrainX) | |
# TL, TA = self.metrics(Tcache, TrainY) | |
# Vcache = self.forward(ValX) | |
# VL, VA = self.metrics(Vcache, ValY) | |
# print("Epoch: %d | Train loss: %.4f | Train acc: %.4f | Val loss: %.4f | Val acc: %.4f" % (e, TL, TA, VL, VA)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment