Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. import numpy as np
  2. from keras.utils import np_utils
  3. import tensorflow as tf
  4. tf.python.control_flow_ops = tf
  5.  
  6. # Set random seed
  7. np.random.seed(42)
  8.  
  9. # Our data
  10. X = np.array([[0,0],[0,1],[1,0],[1,1]]).astype('float32')
  11. y = np.array([[0],[1],[1],[0]]).astype('float32')
  12.  
  13. # Initial Setup for Keras
  14. from keras.models import Sequential
  15. from keras.layers.core import Dense, Activation
  16.  
  17. # Building the model
  18. xor = Sequential()
  19.  
  20. # Add required layers
  21. xor.add(Dense(32, input_dim=2))
  22. xor.add(Activation("tanh"))
  23. xor.add(Dense(1))
  24. xor.add(Activation("sigmoid"))
  25.  
  26. # Specify loss as "binary_crossentropy", optimizer as "adam",
  27. # and add the accuracy metric
  28. xor.compile(loss="binary_crossentropy", optimizer="adam", metrics = ["accuracy"])
  29.  
  30. # Uncomment this line to print the model architecture
  31. xor.summary()
  32.  
  33. # Fitting the model
  34. history = xor.fit(X, y, nb_epoch=200, verbose=0)
  35.  
  36. # Scoring the model
  37. score = xor.evaluate(X, y)
  38. print("\nAccuracy: ", score[-1])
  39.  
  40. # Checking the predictions
  41. print("\nPredictions:")
  42. print(xor.predict_proba(X))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement