Regression: How can I approximate a multi-dimensional function?












2















I want to approximate a sinc function using neural networks. This is my code:



import tensorflow as tf
from keras.layers import Dense
from keras.models import Sequential


N = 10000

x1 = numpy.empty((N,))
x2 = numpy.empty((N,))
x3 = numpy.empty((N,))
z1 = numpy.empty((N,))
z2 = numpy.empty((N,))
y = numpy.empty((N,))


for i in range(N):
x1[i] = random.uniform(-10, 10)
x2[i] = random.uniform(-10, 10)
x3[i] = random.uniform(-10, 10)

z1 = x1 + x2 - x3
z2 = -x1 + x2 + x3

for i in range(N):
y[i] = (numpy.sin(z1[i])/z1[i])*(numpy.sin(z2[i])/z2[i])

y = y.reshape(-1, 1)

scaler = MinMaxScaler()

x1 = scalar.fit_transform(x1)
x2 = scalar.fit_transform(x2)
x3 = scalar.fit_transform(x3)

y = scalar.fit_transform(y)

model = Sequential()
model.add(Dense(50, activation='relu', input_shape=(3,)))
model.add(Dense(1, activation='relu'))

model.fit(X, y, epochs=50, verbose=1, batch_size=2)


I have seen similar codes where X and Y are two one-dimensional matrixes, but in my case, I should calculate y using three input values. What is X in the code above for my case? In other words, how should I create X using x1, x2 and x3?










share|improve this question




















  • 1





    normally X will be a Nx3 matrix, but it depends on how fit() works. What is create_model in your case? Something from a package?

    – Simon
    Nov 23 '18 at 23:52











  • @Simon I added the model in my code

    – Ahmad
    Nov 24 '18 at 3:04
















2















I want to approximate a sinc function using neural networks. This is my code:



import tensorflow as tf
from keras.layers import Dense
from keras.models import Sequential


N = 10000

x1 = numpy.empty((N,))
x2 = numpy.empty((N,))
x3 = numpy.empty((N,))
z1 = numpy.empty((N,))
z2 = numpy.empty((N,))
y = numpy.empty((N,))


for i in range(N):
x1[i] = random.uniform(-10, 10)
x2[i] = random.uniform(-10, 10)
x3[i] = random.uniform(-10, 10)

z1 = x1 + x2 - x3
z2 = -x1 + x2 + x3

for i in range(N):
y[i] = (numpy.sin(z1[i])/z1[i])*(numpy.sin(z2[i])/z2[i])

y = y.reshape(-1, 1)

scaler = MinMaxScaler()

x1 = scalar.fit_transform(x1)
x2 = scalar.fit_transform(x2)
x3 = scalar.fit_transform(x3)

y = scalar.fit_transform(y)

model = Sequential()
model.add(Dense(50, activation='relu', input_shape=(3,)))
model.add(Dense(1, activation='relu'))

model.fit(X, y, epochs=50, verbose=1, batch_size=2)


I have seen similar codes where X and Y are two one-dimensional matrixes, but in my case, I should calculate y using three input values. What is X in the code above for my case? In other words, how should I create X using x1, x2 and x3?










share|improve this question




















  • 1





    normally X will be a Nx3 matrix, but it depends on how fit() works. What is create_model in your case? Something from a package?

    – Simon
    Nov 23 '18 at 23:52











  • @Simon I added the model in my code

    – Ahmad
    Nov 24 '18 at 3:04














2












2








2








I want to approximate a sinc function using neural networks. This is my code:



import tensorflow as tf
from keras.layers import Dense
from keras.models import Sequential


N = 10000

x1 = numpy.empty((N,))
x2 = numpy.empty((N,))
x3 = numpy.empty((N,))
z1 = numpy.empty((N,))
z2 = numpy.empty((N,))
y = numpy.empty((N,))


for i in range(N):
x1[i] = random.uniform(-10, 10)
x2[i] = random.uniform(-10, 10)
x3[i] = random.uniform(-10, 10)

z1 = x1 + x2 - x3
z2 = -x1 + x2 + x3

for i in range(N):
y[i] = (numpy.sin(z1[i])/z1[i])*(numpy.sin(z2[i])/z2[i])

y = y.reshape(-1, 1)

scaler = MinMaxScaler()

x1 = scalar.fit_transform(x1)
x2 = scalar.fit_transform(x2)
x3 = scalar.fit_transform(x3)

y = scalar.fit_transform(y)

model = Sequential()
model.add(Dense(50, activation='relu', input_shape=(3,)))
model.add(Dense(1, activation='relu'))

model.fit(X, y, epochs=50, verbose=1, batch_size=2)


I have seen similar codes where X and Y are two one-dimensional matrixes, but in my case, I should calculate y using three input values. What is X in the code above for my case? In other words, how should I create X using x1, x2 and x3?










share|improve this question
















I want to approximate a sinc function using neural networks. This is my code:



import tensorflow as tf
from keras.layers import Dense
from keras.models import Sequential


N = 10000

x1 = numpy.empty((N,))
x2 = numpy.empty((N,))
x3 = numpy.empty((N,))
z1 = numpy.empty((N,))
z2 = numpy.empty((N,))
y = numpy.empty((N,))


for i in range(N):
x1[i] = random.uniform(-10, 10)
x2[i] = random.uniform(-10, 10)
x3[i] = random.uniform(-10, 10)

z1 = x1 + x2 - x3
z2 = -x1 + x2 + x3

for i in range(N):
y[i] = (numpy.sin(z1[i])/z1[i])*(numpy.sin(z2[i])/z2[i])

y = y.reshape(-1, 1)

scaler = MinMaxScaler()

x1 = scalar.fit_transform(x1)
x2 = scalar.fit_transform(x2)
x3 = scalar.fit_transform(x3)

y = scalar.fit_transform(y)

model = Sequential()
model.add(Dense(50, activation='relu', input_shape=(3,)))
model.add(Dense(1, activation='relu'))

model.fit(X, y, epochs=50, verbose=1, batch_size=2)


I have seen similar codes where X and Y are two one-dimensional matrixes, but in my case, I should calculate y using three input values. What is X in the code above for my case? In other words, how should I create X using x1, x2 and x3?







python neural-network regression






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 3:03







Ahmad

















asked Nov 23 '18 at 20:43









AhmadAhmad

2,76833058




2,76833058








  • 1





    normally X will be a Nx3 matrix, but it depends on how fit() works. What is create_model in your case? Something from a package?

    – Simon
    Nov 23 '18 at 23:52











  • @Simon I added the model in my code

    – Ahmad
    Nov 24 '18 at 3:04














  • 1





    normally X will be a Nx3 matrix, but it depends on how fit() works. What is create_model in your case? Something from a package?

    – Simon
    Nov 23 '18 at 23:52











  • @Simon I added the model in my code

    – Ahmad
    Nov 24 '18 at 3:04








1




1





normally X will be a Nx3 matrix, but it depends on how fit() works. What is create_model in your case? Something from a package?

– Simon
Nov 23 '18 at 23:52





normally X will be a Nx3 matrix, but it depends on how fit() works. What is create_model in your case? Something from a package?

– Simon
Nov 23 '18 at 23:52













@Simon I added the model in my code

– Ahmad
Nov 24 '18 at 3:04





@Simon I added the model in my code

– Ahmad
Nov 24 '18 at 3:04












1 Answer
1






active

oldest

votes


















1














Here is a little solution with tensorflow:



#-*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt

batch_size = 16


def generate_input(N=10000):
x = rd.uniform(low=-10, high=10, size=(3, N))
z1 = x[0] + x[1] - x[2]
z2 = -x[0] + x[1] + x[2]
return [np.expand_dims(np.stack((z1, z2), axis=0), axis=2) for _ in range(batch_size)]


def target_func(data):
return [(np.sin(x[0, :]) / x[0, :]) * (np.sin(x[1, :]) / x[1, :]) for x in data]


def main():
x = tf.placeholder(tf.float32, shape=(batch_size, 2, None, 1), name='inputs')
y = tf.placeholder(tf.float32, shape=(batch_size, None, 1), name='target')

x_sum = tf.reduce_sum(x, axis=1)
fc1 = tf.layers.dense(inputs=x_sum, units=32, activation=tf.nn.relu, name="fc1")
fc2 = tf.layers.dense(inputs=fc1, units=16, activation=tf.nn.relu, name="fc2")
output = tf.layers.dense(inputs=fc2, units=1, activation=None, name="output")
predict = output

losses = tf.reduce_sum(tf.square(y - predict, name="loss"))

train_step = tf.train.AdamOptimizer().minimize(losses)

saver = tf.train.Saver()
max_train_iter = 500
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

for i in range(max_train_iter):
inp = generate_input()
target = target_func(inp)
sess.run(train_step, feed_dict={x: inp, y: target})
if i % (max_train_iter // 10) == 0:
val_inp = generate_input()
loss_val = sess.run(losses, feed_dict={x: val_inp, y: target_func(val_inp)})
print ('Step:%d, Loss:%f' % (i, loss_val))

saver.save(sess, 'sin/model', global_step=i)
test_inp = generate_input(50)
truth = target_func(test_inp)
pred = sess.run(predict, feed_dict={x: test_inp})
plt.figure()
rang = np.linspace(-10, 10, num=50)
plt.plot(rang, truth[0], label='target')
plt.plot(rang, pred[0], label='prediction')
plt.legend()
plt.savefig('sin/'+'graph_'+str(i)+'.png')


if __name__ == '__main__':
main()


It doesn't produce a great prediction. You would have to play with architecture and parameters to improve on that. But it works. The test example after training is shown below.



enter image description here






share|improve this answer
























  • Thank you, couldn't you write the program using keras.layers and scaling data, the way I write in my origina l code?

    – Ahmad
    Nov 24 '18 at 7:04











  • It seems it needs normalization! Moreover, it has three features, I wonder how did you plot it

    – Ahmad
    Nov 24 '18 at 10:32











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452745%2fregression-how-can-i-approximate-a-multi-dimensional-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Here is a little solution with tensorflow:



#-*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt

batch_size = 16


def generate_input(N=10000):
x = rd.uniform(low=-10, high=10, size=(3, N))
z1 = x[0] + x[1] - x[2]
z2 = -x[0] + x[1] + x[2]
return [np.expand_dims(np.stack((z1, z2), axis=0), axis=2) for _ in range(batch_size)]


def target_func(data):
return [(np.sin(x[0, :]) / x[0, :]) * (np.sin(x[1, :]) / x[1, :]) for x in data]


def main():
x = tf.placeholder(tf.float32, shape=(batch_size, 2, None, 1), name='inputs')
y = tf.placeholder(tf.float32, shape=(batch_size, None, 1), name='target')

x_sum = tf.reduce_sum(x, axis=1)
fc1 = tf.layers.dense(inputs=x_sum, units=32, activation=tf.nn.relu, name="fc1")
fc2 = tf.layers.dense(inputs=fc1, units=16, activation=tf.nn.relu, name="fc2")
output = tf.layers.dense(inputs=fc2, units=1, activation=None, name="output")
predict = output

losses = tf.reduce_sum(tf.square(y - predict, name="loss"))

train_step = tf.train.AdamOptimizer().minimize(losses)

saver = tf.train.Saver()
max_train_iter = 500
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

for i in range(max_train_iter):
inp = generate_input()
target = target_func(inp)
sess.run(train_step, feed_dict={x: inp, y: target})
if i % (max_train_iter // 10) == 0:
val_inp = generate_input()
loss_val = sess.run(losses, feed_dict={x: val_inp, y: target_func(val_inp)})
print ('Step:%d, Loss:%f' % (i, loss_val))

saver.save(sess, 'sin/model', global_step=i)
test_inp = generate_input(50)
truth = target_func(test_inp)
pred = sess.run(predict, feed_dict={x: test_inp})
plt.figure()
rang = np.linspace(-10, 10, num=50)
plt.plot(rang, truth[0], label='target')
plt.plot(rang, pred[0], label='prediction')
plt.legend()
plt.savefig('sin/'+'graph_'+str(i)+'.png')


if __name__ == '__main__':
main()


It doesn't produce a great prediction. You would have to play with architecture and parameters to improve on that. But it works. The test example after training is shown below.



enter image description here






share|improve this answer
























  • Thank you, couldn't you write the program using keras.layers and scaling data, the way I write in my origina l code?

    – Ahmad
    Nov 24 '18 at 7:04











  • It seems it needs normalization! Moreover, it has three features, I wonder how did you plot it

    – Ahmad
    Nov 24 '18 at 10:32
















1














Here is a little solution with tensorflow:



#-*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt

batch_size = 16


def generate_input(N=10000):
x = rd.uniform(low=-10, high=10, size=(3, N))
z1 = x[0] + x[1] - x[2]
z2 = -x[0] + x[1] + x[2]
return [np.expand_dims(np.stack((z1, z2), axis=0), axis=2) for _ in range(batch_size)]


def target_func(data):
return [(np.sin(x[0, :]) / x[0, :]) * (np.sin(x[1, :]) / x[1, :]) for x in data]


def main():
x = tf.placeholder(tf.float32, shape=(batch_size, 2, None, 1), name='inputs')
y = tf.placeholder(tf.float32, shape=(batch_size, None, 1), name='target')

x_sum = tf.reduce_sum(x, axis=1)
fc1 = tf.layers.dense(inputs=x_sum, units=32, activation=tf.nn.relu, name="fc1")
fc2 = tf.layers.dense(inputs=fc1, units=16, activation=tf.nn.relu, name="fc2")
output = tf.layers.dense(inputs=fc2, units=1, activation=None, name="output")
predict = output

losses = tf.reduce_sum(tf.square(y - predict, name="loss"))

train_step = tf.train.AdamOptimizer().minimize(losses)

saver = tf.train.Saver()
max_train_iter = 500
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

for i in range(max_train_iter):
inp = generate_input()
target = target_func(inp)
sess.run(train_step, feed_dict={x: inp, y: target})
if i % (max_train_iter // 10) == 0:
val_inp = generate_input()
loss_val = sess.run(losses, feed_dict={x: val_inp, y: target_func(val_inp)})
print ('Step:%d, Loss:%f' % (i, loss_val))

saver.save(sess, 'sin/model', global_step=i)
test_inp = generate_input(50)
truth = target_func(test_inp)
pred = sess.run(predict, feed_dict={x: test_inp})
plt.figure()
rang = np.linspace(-10, 10, num=50)
plt.plot(rang, truth[0], label='target')
plt.plot(rang, pred[0], label='prediction')
plt.legend()
plt.savefig('sin/'+'graph_'+str(i)+'.png')


if __name__ == '__main__':
main()


It doesn't produce a great prediction. You would have to play with architecture and parameters to improve on that. But it works. The test example after training is shown below.



enter image description here






share|improve this answer
























  • Thank you, couldn't you write the program using keras.layers and scaling data, the way I write in my origina l code?

    – Ahmad
    Nov 24 '18 at 7:04











  • It seems it needs normalization! Moreover, it has three features, I wonder how did you plot it

    – Ahmad
    Nov 24 '18 at 10:32














1












1








1







Here is a little solution with tensorflow:



#-*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt

batch_size = 16


def generate_input(N=10000):
x = rd.uniform(low=-10, high=10, size=(3, N))
z1 = x[0] + x[1] - x[2]
z2 = -x[0] + x[1] + x[2]
return [np.expand_dims(np.stack((z1, z2), axis=0), axis=2) for _ in range(batch_size)]


def target_func(data):
return [(np.sin(x[0, :]) / x[0, :]) * (np.sin(x[1, :]) / x[1, :]) for x in data]


def main():
x = tf.placeholder(tf.float32, shape=(batch_size, 2, None, 1), name='inputs')
y = tf.placeholder(tf.float32, shape=(batch_size, None, 1), name='target')

x_sum = tf.reduce_sum(x, axis=1)
fc1 = tf.layers.dense(inputs=x_sum, units=32, activation=tf.nn.relu, name="fc1")
fc2 = tf.layers.dense(inputs=fc1, units=16, activation=tf.nn.relu, name="fc2")
output = tf.layers.dense(inputs=fc2, units=1, activation=None, name="output")
predict = output

losses = tf.reduce_sum(tf.square(y - predict, name="loss"))

train_step = tf.train.AdamOptimizer().minimize(losses)

saver = tf.train.Saver()
max_train_iter = 500
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

for i in range(max_train_iter):
inp = generate_input()
target = target_func(inp)
sess.run(train_step, feed_dict={x: inp, y: target})
if i % (max_train_iter // 10) == 0:
val_inp = generate_input()
loss_val = sess.run(losses, feed_dict={x: val_inp, y: target_func(val_inp)})
print ('Step:%d, Loss:%f' % (i, loss_val))

saver.save(sess, 'sin/model', global_step=i)
test_inp = generate_input(50)
truth = target_func(test_inp)
pred = sess.run(predict, feed_dict={x: test_inp})
plt.figure()
rang = np.linspace(-10, 10, num=50)
plt.plot(rang, truth[0], label='target')
plt.plot(rang, pred[0], label='prediction')
plt.legend()
plt.savefig('sin/'+'graph_'+str(i)+'.png')


if __name__ == '__main__':
main()


It doesn't produce a great prediction. You would have to play with architecture and parameters to improve on that. But it works. The test example after training is shown below.



enter image description here






share|improve this answer













Here is a little solution with tensorflow:



#-*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt

batch_size = 16


def generate_input(N=10000):
x = rd.uniform(low=-10, high=10, size=(3, N))
z1 = x[0] + x[1] - x[2]
z2 = -x[0] + x[1] + x[2]
return [np.expand_dims(np.stack((z1, z2), axis=0), axis=2) for _ in range(batch_size)]


def target_func(data):
return [(np.sin(x[0, :]) / x[0, :]) * (np.sin(x[1, :]) / x[1, :]) for x in data]


def main():
x = tf.placeholder(tf.float32, shape=(batch_size, 2, None, 1), name='inputs')
y = tf.placeholder(tf.float32, shape=(batch_size, None, 1), name='target')

x_sum = tf.reduce_sum(x, axis=1)
fc1 = tf.layers.dense(inputs=x_sum, units=32, activation=tf.nn.relu, name="fc1")
fc2 = tf.layers.dense(inputs=fc1, units=16, activation=tf.nn.relu, name="fc2")
output = tf.layers.dense(inputs=fc2, units=1, activation=None, name="output")
predict = output

losses = tf.reduce_sum(tf.square(y - predict, name="loss"))

train_step = tf.train.AdamOptimizer().minimize(losses)

saver = tf.train.Saver()
max_train_iter = 500
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

for i in range(max_train_iter):
inp = generate_input()
target = target_func(inp)
sess.run(train_step, feed_dict={x: inp, y: target})
if i % (max_train_iter // 10) == 0:
val_inp = generate_input()
loss_val = sess.run(losses, feed_dict={x: val_inp, y: target_func(val_inp)})
print ('Step:%d, Loss:%f' % (i, loss_val))

saver.save(sess, 'sin/model', global_step=i)
test_inp = generate_input(50)
truth = target_func(test_inp)
pred = sess.run(predict, feed_dict={x: test_inp})
plt.figure()
rang = np.linspace(-10, 10, num=50)
plt.plot(rang, truth[0], label='target')
plt.plot(rang, pred[0], label='prediction')
plt.legend()
plt.savefig('sin/'+'graph_'+str(i)+'.png')


if __name__ == '__main__':
main()


It doesn't produce a great prediction. You would have to play with architecture and parameters to improve on that. But it works. The test example after training is shown below.



enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 23:07









dsalajdsalaj

6441027




6441027













  • Thank you, couldn't you write the program using keras.layers and scaling data, the way I write in my origina l code?

    – Ahmad
    Nov 24 '18 at 7:04











  • It seems it needs normalization! Moreover, it has three features, I wonder how did you plot it

    – Ahmad
    Nov 24 '18 at 10:32



















  • Thank you, couldn't you write the program using keras.layers and scaling data, the way I write in my origina l code?

    – Ahmad
    Nov 24 '18 at 7:04











  • It seems it needs normalization! Moreover, it has three features, I wonder how did you plot it

    – Ahmad
    Nov 24 '18 at 10:32

















Thank you, couldn't you write the program using keras.layers and scaling data, the way I write in my origina l code?

– Ahmad
Nov 24 '18 at 7:04





Thank you, couldn't you write the program using keras.layers and scaling data, the way I write in my origina l code?

– Ahmad
Nov 24 '18 at 7:04













It seems it needs normalization! Moreover, it has three features, I wonder how did you plot it

– Ahmad
Nov 24 '18 at 10:32





It seems it needs normalization! Moreover, it has three features, I wonder how did you plot it

– Ahmad
Nov 24 '18 at 10:32


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452745%2fregression-how-can-i-approximate-a-multi-dimensional-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Sphinx de Gizeh

Dijon

Determine an Integral..