Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. from keras.models import Sequential
  2. from keras.layers import Dense
  3. import numpy as np
  4. import matplotlib.pylab as plt
  5.  
  6. # Create dataset
  7. x = np.arange(0, np.pi * 2, 0.1)
  8. y = np.sin(x)
  9.  
  10. # Some parameters
  11. ACTIVE_FUN = 'tanh'
  12. BATCH_SIZE = 1
  13. VERBOSE=0
  14.  
  15. # Create the model
  16. model = Sequential()
  17. model.add(Dense(5, input_shape=(1,), activation=ACTIVE_FUN))
  18. model.add(Dense(5, activation=ACTIVE_FUN))
  19. model.add(Dense(1, activation='linear'))
  20.  
  21. # Compile the model
  22. model.compile(loss='mean_squared_error', optimizer='sgd', metrics=['mean_squared_error'])
  23.  
  24. # Fit the model
  25. model.fit(x, y, epochs=1000, batch_size=BATCH_SIZE, verbose=VERBOSE)
  26.  
  27. # Evaluate the model
  28. scores = model.evaluate(x, y, verbose=VERBOSE)
  29. print('%s: %.2f%%' % (model.metrics_names[1], scores[1] * 100))
  30.  
  31. # Make predictions
  32. y_pred = model.predict(x)
  33.  
  34. # Plot
  35. plt.plot(x, y, color='blue', linewidth=1, markersize='1')
  36. plt.plot(x, y_pred, color='green', linewidth=1, markersize='1')
  37. plt.xlabel('Angle [rad]')
  38. plt.ylabel('sin(x)')
  39. plt.axis('tight')
  40. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement