Skip to content

Instantly share code, notes, and snippets.

@zfedoran
Created June 29, 2017 21:44
Show Gist options
  • Save zfedoran/86b1f8e75f9a2991b6a8d8c125269d42 to your computer and use it in GitHub Desktop.
Save zfedoran/86b1f8e75f9a2991b6a8d8c125269d42 to your computer and use it in GitHub Desktop.
Basic DNN Example: Approximate sine function using tflearn
import tflearn
import numpy as np
import matplotlib.pyplot as plt
N = 1000
# Approximate sine function using tflearn
x = np.linspace(-np.pi, np.pi, N)
y = np.sin(x) + np.random.uniform(-0.5,0.5, N)
# Model
net = tflearn.input_data(shape=[None,1])
net = tflearn.fully_connected(net, 1)
net = tflearn.fully_connected(net, 32, activation='relu')
net = tflearn.fully_connected(net, 1)
net = tflearn.regression(net, optimizer='adam', loss='mean_square', metric='R2', learning_rate=0.01)
model = tflearn.DNN(net)
model.fit(x.reshape(-1, 1), y.reshape(-1, 1), batch_size=100, n_epoch=100, shuffle=True)
# Test the model
x0 = np.linspace(-np.pi,np.pi,100).reshape(-1, 1)
pred = model.predict(x0)
plt.plot(x0, pred, color="r")
plt.scatter(x, y, alpha=0.1)
plt.show()
@zfedoran
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment