Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RNN #6823

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add RNN #6823

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions code/artificial_intelligence/src/recurrent_neural_network/rnn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense

# Generate dummy sequential data
def generate_data(seq_length=50, total_sequences=1000):
X = []
y = []
for _ in range(total_sequences):
# Create random sequences of length 'seq_length'
sequence = np.random.rand(seq_length)
target = np.sum(sequence) # The target could be the sum of the sequence
X.append(sequence)
y.append(target)
return np.array(X), np.array(y)

# Prepare the dataset
seq_length = 10
X, y = generate_data(seq_length=seq_length)

# Reshape input to be [samples, time steps, features] for RNN input
X = X.reshape((X.shape[0], seq_length, 1))

# Build the RNN model
model = Sequential()
model.add(SimpleRNN(50, activation='relu', input_shape=(seq_length, 1)))
model.add(Dense(1))

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

# Train the model
model.fit(X, y, epochs=10, batch_size=32)

# Predict on new data
test_data = np.random.rand(1, seq_length).reshape(1, seq_length, 1)
prediction = model.predict(test_data)

print(f"Prediction: {prediction}")