Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. from keras.models import Sequential
  2. import matplotlib.pyplot as plt
  3. from keras.layers import Dense
  4. import math
  5. import numpy as np
  6.  
  7.  
  8. def f1(x):
  9.     return x * math.sin(6*math.pi*x)*math.exp(math.pow(-x, 2))
  10.  
  11.  
  12. model = Sequential()
  13. model.add(Dense(200, input_dim=1, activation='sigmoid', weights=[100*np.random.randn(1, 200), 10*np.random.randn(200)]))
  14. model.add(Dense(1, activation='sigmoid'))
  15.  
  16. model.compile(optimizer='sgd', loss='mse')
  17.  
  18. draw_x = np.linspace(-1, 1, 10000)
  19. draw_y = np.array([f1(x) for x in draw_x])
  20.  
  21. data = np.random.random(10000)
  22. data = np.array([-x if i % 2 == 0 else x for i, x in enumerate(data)])
  23. results = np.array([f1(x) for x in data])
  24. model.fit(data[:7000], results[:7000], epochs=200, batch_size=32)
  25. print(model.evaluate(data[7000:], results[7000:]))
  26. predicted_results = model.predict(draw_x)
  27.  
  28. plt.plot(draw_x, draw_y)
  29. plt.plot(draw_x, predicted_results)
  30. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement