mystic123 / tensorflow-yolo-v3

Implementation of YOLO v3 object detector in Tensorflow (TF-Slim)
https://medium.com/@pawekapica_31302/implementing-yolo-v3-in-tensorflow-tf-slim-c3c55ff59dbe
Apache License 2.0
890 stars 353 forks source link

Issue in RECURRENT NEURAL NETWORK using Tensorflow 2.0 #102

Closed ashishsme14 closed 3 years ago

ashishsme14 commented 3 years ago

Please let us know anything wrong in below code, not getting desire result -

## **RECURRENT NEURAL NETWORK using Tensorflow 2.0**
In this scenario of hands-on, you will be performing Recurrent Neural Network using Tensorflow 2.0.

**Note** - Finally restart and run all the cells after the completion of the challenge

**Run the below cell to import the neccessary packages**

from numpy import sqrt
from numpy import asarray
from pandas import read_csv
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
import tensorflow as tf
from sklearn import metrics
from sklearn.model_selection import train_test_split

- Assign the value as 40 to the variabel **RANDOM_SEED** which will be the seed value.
- Set the random seed value using the value stored in the variable **RANDOM_SEED**.

RANDOM_SEED = 40
tf.random.set_seed(RANDOM_SEED)

**Run the below cell to split the univariate sequence into samples**

# split a univariate sequence into samples
def split_sequence(sequence, n_steps):
    X, y = list(), list()
    for i in range(len(sequence)):
        # find the end of this pattern
        end_ix = i + n_steps
        # check if we are beyond the sequence
        if end_ix > len(sequence)-1:
            break
        # gather input and output parts of the pattern
        seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
        X.append(seq_x)
        y.append(seq_y)
    return asarray(X), asarray(y)

- Read the dataset **airline-passengers.csv** and give parameter index_col as 0 and save it in variable df.

# load the dataset
df = read_csv("airline-passengers.csv", index_col=0)

- Convert the data type of the values dataframe **df** to float32 and save it in variable **values**.
- Assign the value 5 to the variable **n_steps** which is the window size.
- Split the samples using the function **split_sequence** and pass the parameters **values** and **n_steps** and save it in variables **X** and **y**

# retrieve the values
values = df.values.astype('float32')

# specify the window size
n_steps = 5

# split into samples
X, y = split_sequence(values, n_steps)

- Split the data **X**,**y** with the train_test_split function of sklearn with parameters test_size=0.33 and random_state=RANDOM_SEED.

# split into train and test datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=RANDOM_SEED)

### Define Model

Construct a fully-connected network structure defined using dense class
- Create a sequential model
- Add a LSTM layer which has 200 nodes with activation function as relu and input shape as (n_steps,1).
- The first hidden layer has 100 nodes and uses the relu activation function.
- The second hidden layer has 50 nodes and uses the relu activation function.
- The output layer has 1 node.

# define model
model = Sequential()

model.add(LSTM(200, activation='relu',  input_shape=(n_steps,1)))

model.add(Dense(100, activation='relu'))

model.add(Dense(50, activation='relu'))

model.add(Dense(1))

### Compile Model
- While comipling the model pass the following parameters -

           -optimizer as Adam
           -loss as mse 
           -metrics as mae

# compile the model
model.compile(optimizer='Adam', loss='mse', metrics=['mae'])

### Fit the model
- fit the model with X_train, y_train, epochs=350, batch_size=32,verbose=0.

# fit the model
model.fit(X_train, y_train, epochs=350, batch_size=32, verbose=0)

### Predict model
- Perform prediction on the test data (i.e) on **X_test** and save the predictions in the variable **y_pred**.

row = ([X_test])
y_pred = model.predict(row)

- Calculate the mean squared error on the variables **y_test** and **y_pred** using the mean_squared_error function in sklearn metrics and save it in variable **MSE**.

- Calculate the Root mean squared error on the variables **y_test** and **y_pred** by performing square root on the above result and save it in variable **RMSE**.

- Calculate the mean absolute error on the variables **y_test** and **y_pred** using the mean_absolute_error function in sklearn metrics and save it in variable **MAE**.

# evaluate the model
MSE  = metrics.mean_squared_error(y_test,y_pred)

RMSE = sqrt(metrics.mean_squared_error(y_test,y_pred))

MAE  = metrics.mean_absolute_error(y_test,y_pred)
print('MSE: %.3f, RMSE: %.3f, MAE: %.3f' % (MSE, RMSE,MAE))

### Run the below cells to save the results for validation.

with open("MSE.txt", "w") as text_file:
        MSE=str(MSE)
        text_file.write(MSE)
with open("RMSE.txt", "w") as text_file:
        RMSE=str(RMSE)
        text_file.write(RMSE)
with open("MAE.txt", "w") as text_file:
        MAE=str(MAE)
        text_file.write(MAE)
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)