Spaces:
Runtime error
Runtime error
File size: 1,871 Bytes
b97220d e324a3c b97220d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | # π½ STEP 1: Install Required Packages
!pip install tensorflow huggingface_hub
# π½ STEP 2: Import Libraries
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense
from tensorflow.keras.optimizers import Adam
from huggingface_hub import notebook_login, HfApi, create_repo
# π½ STEP 3: Prepare Dataset
def generate_sequence():
X, y = [], []
for i in range(1, 100):
X.append([i, i+1, i+2])
y.append(i+3)
X = np.array(X).reshape(-1, 3, 1)
y = np.array(y)
return X, y
X, y = generate_sequence()
# π½ STEP 4: Build the RNN Model
model = Sequential([
SimpleRNN(32, activation='relu', input_shape=(3, 1)),
Dense(1)
])
model.compile(optimizer=Adam(), loss='mse')
model.summary()
# π½ STEP 5: Train the Model
model.fit(X, y, epochs=200, verbose=0)
# π½ STEP 6: Save the Model
model.save("rnn_next_number.h5")
# π½ STEP 7: Login to Hugging Face
notebook_login() # Paste your token when asked
# π½ STEP 8: Create Repo on Hugging Face
repo_id = "your-username/predict-next-number-rnn" # Replace with your actual username
create_repo(name="predict-next-number-rnn", repo_type="model", private=False)
# π½ STEP 9: Upload Model to Hugging Face
api = HfApi()
api.upload_file(
path_or_fileobj="rnn_next_number.h5",
path_in_repo="rnn_next_number.h5",
repo_id=repo_id,
repo_type="model"
)
# π’ Predict Next Number - RNN Model
This is a simple RNN model trained using Keras to predict the next number in a sequence.
## Example
Input: [4, 5, 6] β Output: ~7.0
### Usage
```python
from tensorflow.keras.models import load_model
import numpy as np
model = load_model("rnn_next_number.h5")
x_input = np.array([4, 5, 6]).reshape(1, 3, 1)
prediction = model.predict(x_input)
print(f"Predicted next number: {prediction[0][0]:.2f}")
|