Advertisement
Ko4la

Neural Network.py

Mar 29th, 2023
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. import keras
  2. import numpy as np
  3.  
  4. def create_neural_network(input_shape, output_shape):
  5.   model = keras.Sequential()
  6.   model.add(keras.layers.Dense(output_shape, activation='softmax'))
  7.  
  8.   return model
  9.  
  10. def train_neural_network(model, data):
  11.   model.compile(loss='categorical_crossentropy', optimizer='adam')
  12.   model.fit(data, epochs=10)
  13.  
  14. def predict_neural_network(model, data):
  15.   predictions = model.predict(data)
  16.   return predictions
  17.  
  18. def main():
  19.   # Create the neural network
  20.   model = create_neural_network(input_shape=(784,), output_shape=(10,))
  21.  
  22.   # Train the neural network
  23.   data = keras.datasets.mnist.load_data()
  24.   x_train = data['x_train']
  25.   y_train = data['y_train']
  26.  
  27.   model.fit(x_train, y_train, epochs=10)
  28.  
  29.   # Predict the labels of the test data
  30.   x_test = data['x_test']
  31.   y_test_predictions = model.predict(x_test)
  32.  
  33.   # Evaluate the model
  34.   accuracy = accuracy_score(y_test, y_test_predictions)
  35.  
  36.   print('Accuracy:', accuracy)
  37.  
  38. if __name__ == '__main__':
  39.   main()
  40. # 3/30/2023 -Root
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement