Skip to content

Instantly share code, notes, and snippets.

@silgon
Last active February 16, 2018 22:56
Show Gist options
  • Save silgon/500c106429a191960e2be44ffc31ba11 to your computer and use it in GitHub Desktop.
Save silgon/500c106429a191960e2be44ffc31ba11 to your computer and use it in GitHub Desktop.
Frequency Prediction
# code to solve https://stackoverflow.com/q/47932589/2237916
import numpy as np
import tflearn
from random import shuffle
# parameters
n_input=100
n_train=2000
n_test = 500
# generate data
xs=[]
ys=[]
frequencies = np.linspace(1,50,n_train+n_test)
shuffle(frequencies)
t=np.linspace(0,2*np.pi,n_input)
for freq in frequencies:
xs.append(np.sin(t*freq))
ys.append(freq)
xs_train = np.array(xs[:n_train])
ys_train = np.array(ys[:n_train]).reshape(-1,1)
xs_test = np.array(xs[n_train:])
ys_test = np.array(ys[n_train:]).reshape(-1,1)
# WARNING: either Deep Network or LSTM network can be use.
# Please comment the code that is not going to be used
# Deep network
net = tflearn.input_data(shape=[None, n_input])
net = tflearn.fully_connected(net, 100)
net = tflearn.fully_connected(net, 100)
net = tflearn.fully_connected(net, 1)
net = tflearn.regression(net, optimizer='adam',loss='mean_square')
model = tflearn.DNN(net)
model.fit(xs_train, ys_train)
print(np.hstack((model.predict(xs_test),ys_test))[:10])
xs_train=np.array(xs[:n_train]).reshape(n_train,n_input,1)
xs_test=np.array(xs[n_train:]).reshape(n_test,n_input,1)
# LSTM network prediction
net = tflearn.input_data(shape=[None, n_input, 1])
net = tflearn.lstm(net, 10)
net = tflearn.fully_connected(net, 100, activation="relu")
net = tflearn.fully_connected(net, 1)
net = tflearn.regression(net, optimizer='adam', loss='mean_square')
model = tflearn.DNN(net)
model.fit(xs_train, ys_train, n_epoch=100)
print(np.hstack((model.predict(xs_test),ys_test))[:10])
import numpy as np
import tensorflow as tf
from random import shuffle
# parameters
n_input=100
n_train=2000
n_test = 500
# generate data
xs=[]
ys=[]
frequencies = np.linspace(1,50,n_train+n_test)
shuffle(frequencies)
t=np.linspace(0,2*np.pi,n_input)
for freq in frequencies:
xs.append(np.sin(t*freq))
ys.append(freq)
xs_train = np.array(xs[:n_train])
ys_train = np.array(ys[:n_train]).reshape(-1,1)
xs_test = np.array(xs[n_train:])
ys_test = np.array(ys[n_train:]).reshape(-1,1)
# WARNING: either Deep Network or LSTM network can be use.
# Please comment the code that is not going to be used
# Deep network
x_ = tf.placeholder(shape=[None, n_input], dtype=tf.float32, name="input")
y_ = tf.placeholder(shape=[None, 1], dtype=tf.float32, name="output")
x = tf.layers.dense(x_, 100)
x = tf.layers.dense(x, 100)
logits = tf.layers.dense(x, 1)
def mse(logits, outputs):
mse = tf.reduce_mean(tf.pow(logits-outputs, 2.0))
return mse
loss = mse(logits, y_)
trainer = tf.train.AdamOptimizer(learning_rate=0.001)
# trainer=tf.contrib.keras.optimizers.Adam()
updateModel = trainer.minimize(loss)
n_epochs = 100
sess=tf.Session()
sess.run(tf.initialize_all_variables())
for i in range(n_epochs):
sess.run(updateModel, feed_dict={x_:xs_train, y_: ys_train})
ys_test_ = sess.run(logits, feed_dict={x_:xs_test})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment