Spaces:
Sleeping
Sleeping
import streamlit as st | |
import json | |
from nn import train, predict, save_model, sigmoid | |
# INPUTS = [[0,0],[0,1],[1,0],[1,1]] | |
# OUTPUTS = [[0],[1],[1],[0]] | |
# EPOCHS = 1000000 | |
# ALPHAS = 20 | |
INPUTS = [[0,0],[0,1],[1,0],[1,1]] | |
OUTPUTS = [[0],[1],[1],[0]] | |
def runNN(epoch, alpha): | |
# Train model | |
modelo = train(epochs=epoch, alpha=alpha) | |
print(modelo) | |
# Save model to file | |
# save_model(modelo, "modelo.json") | |
st.download_button( | |
label="Download model", | |
data=json.dumps(modelo), | |
file_name="modelo.json", | |
mime="application/json", | |
) | |
for i in range(4): | |
result = predict(INPUTS[i][0],INPUTS[i][1], activation=sigmoid) | |
st.write("for input", INPUTS[i], "expected", OUTPUTS[i][0], "predicted", f"{result:4.4}", "which is", "correct" if round(result)==OUTPUTS[i][0] else "incorrect") | |
def sidebar(): | |
# Neural network controls | |
st.sidebar.header('Neural Network Controls') | |
st.sidebar.text('Number of epochs') | |
epochs = st.sidebar.slider('Epochs', 1000, 1000000, 100000) | |
st.sidebar.text('Learning rate') | |
alphas = st.sidebar.slider('Alphas', 1, 100, 20) | |
if st.sidebar.button('Run Neural Network'): | |
runNN(epochs, alphas) | |
def app(): | |
st.title('Simple Neural Network App') | |
st.write('This is the Neural Network image we are trying to implement!') | |
st.image('nn.png', width=500) | |
sidebar() | |
st.markdown(''' | |
### References | |
* https://www.codingame.com/playgrounds/59631/neural-network-xor-example-from-scratch-no-libs | |
''') | |
if __name__ == '__main__': | |
app() | |