Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """Example of building a model to solve an XOR problem in Keras."""
  3.  
  4. import keras
  5. import numpy as np
  6.  
  7. # XOR data.
  8. x = np.array([
  9. [0, 1],
  10. [1, 0],
  11. [0, 0],
  12. [1, 1],
  13. ])
  14. y = np.array([
  15. [1],
  16. [1],
  17. [0],
  18. [0],
  19. ])
  20.  
  21. # Builds the model.
  22. input_var = keras.layers.Input(shape=(2,), dtype='float32')
  23. hidden = keras.layers.Dense(5, activation='tanh')(input_var)
  24. hidden = keras.layers.Dense(5, activation='tanh')(hidden)
  25. output_var = keras.layers.Dense(1, activation='sigmoid')(hidden)
  26.  
  27. model = keras.models.Model([input_var], [output_var])
  28. model.compile(loss='mean_squared_error', optimizer='sgd')
  29.  
  30. # Train the model.
  31. model.fit([x], [y], nb_epoch=10000)
  32.  
  33. # Show the predictions.
  34. preds = model.predict([x])
  35.  
  36. print preds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement