Advertisement
Guest User

Keras

a guest
Dec 11th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. from keras import Sequential
  2. from keras.layers import Dense
  3. import random
  4. import numpy as np
  5.  
  6.  
  7. model = Sequential()
  8. model.add(Dense(32, activation='relu', input_dim=100))
  9. model.add(Dense(1, activation='sigmoid'))
  10. model.compile(optimizer='rmsprop',
  11.               loss='binary_crossentropy',
  12.               metrics=['accuracy'])
  13.  
  14. def get_random_power(power, samples=100, interval=(0.5,2)):
  15.     m = random.random() * (interval[1]-interval[0]) + interval[0]
  16.     return [x**power * m for x in range(samples)]
  17.  
  18. def np_get_random_power(power, samples=100, interval=(0.5,2)):
  19.     m = random.random() * (interval[1]-interval[0]) + interval[0]
  20.     return np.array(x**power * m for x in range(samples))
  21.  
  22. def sinus(offset, samples = 100):
  23.     import math
  24.     return [math.sin(offset*x/samples) for x in range(samples)]
  25.  
  26. def get_data_set(size):
  27.     X = []
  28.     Y = []
  29.     choice = [0,1]
  30.     for i in range(size):
  31.         val = random.choice([0,1])
  32.         if val == 0:
  33.             X.append(get_random_power(val))
  34.         else:
  35.             X.append(sinus(val,))
  36.         Y.append([val])
  37.     return X,Y
  38.  
  39. X,Y = get_data_set(1000)
  40. print(np.array(X).shape, np.array(Y).shape)
  41.  
  42. # Generate dummy data
  43. data = np.random.random((1000, 100))
  44. print("Shape:", data.shape)
  45. labels = np.random.randint(2, size=(1000, 1))
  46. print("Shape:", labels.shape)
  47.  
  48. # Train the model, iterating on the data in batches of 32 samples
  49. model.fit(np.array(X), np.array(Y), epochs=10, batch_size=32)
  50.  
  51. X,Y = get_data_set(1000)
  52. result = model.predict(np.array(X))
  53. for i in range(len(Y)):
  54.     print(int(result[i]*1000)/1000,Y[i])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement