使用神经网络拟合曲线(MATLAB/Python)
前言
神經(jīng)網(wǎng)絡(luò)通常用于分類任務(wù),也可以用于回歸任務(wù)。使用一個(gè)含有隱層的神經(jīng)網(wǎng)絡(luò)可以很輕松地?cái)M合出非線性曲線。下面是幾個(gè)示例,包含matlab的和python的,都很簡(jiǎn)單。
實(shí)例1
首先,生成正弦曲線,并引入隨機(jī)噪聲。隨后,在matlab中使用feedforwardnet函數(shù)創(chuàng)建BP神經(jīng)網(wǎng)絡(luò),訓(xùn)練網(wǎng)絡(luò),并查看最后的擬合結(jié)果。
%% clc; clear all; close all;%% 生成正弦曲線 x = linspace(-2*pi, 2*pi, 100); y = sin(x); % 對(duì)目標(biāo)值加入噪聲 n = 0.1 * rand(1, length(x)); y = y + n;% figure(); % plot(x, y, 'b-');%% 數(shù)據(jù)歸一化,創(chuàng)建測(cè)試數(shù)據(jù)集 [x_, ps] = mapminmax(x); data_input = x_; data_target = y;% figure(); % plot(data_input, data_target, 'b-');data_test = linspace(-5, 5, 50); data_true = sin(data_test); data_t = mapminmax('apply', data_test, ps);% figure(); % plot(data_t, data_true, 'b-');%% 創(chuàng)建神經(jīng)網(wǎng)絡(luò)(也可打開(kāi)nntool,在matlab中使用gui創(chuàng)建神經(jīng)網(wǎng)絡(luò)) hidden_layer_size = 10; net = feedforwardnet(hidden_layer_size); [net, tr] = train(net, data_input, data_target);%% 擬合結(jié)果 data_y = sim(net, data_t); % data_y = net(data_t); figure(); e = 0.5 * (data_true - data_y) .^ 2; plot(e); xlabel('x axis'); ylabel('y axis'); legend('error');figure(); hold on; plot(data_test, data_y, '*'); plot(x, y, 'b'); xlabel('x axis'); ylabel('y axis'); legend('prediction', 'real value');運(yùn)行結(jié)果截圖:
x軸是測(cè)試數(shù)據(jù)的x坐標(biāo),y軸是x坐標(biāo)對(duì)應(yīng)預(yù)測(cè)值和曲線真實(shí)值的誤差。
曲線和擬合結(jié)果:
實(shí)例2
這里還是用matlab進(jìn)行的實(shí)驗(yàn)。擬合的目標(biāo)是一個(gè)圓,將圓拆成上下兩條曲線,分別進(jìn)行擬合。
%% clc; clear all; close all;%% 生成圓的上半邊和下半邊 center_x = 0; center_y = 0; radius = 4; x1 = []; x2 = []; y1 = []; y2 = []; for theta = 0:0.1:pix_ = center_x + radius * cos(theta);x1 = [x1 x_];y_ = center_y + radius * sin(theta);y1 = [y1 y_]; endfor theta = pi:0.1:2*pix_ = center_x + radius * cos(theta);x2 = [x2 x_];y_ = center_y + radius * sin(theta);y2 = [y2 y_]; end% 繪制曲線 figure(); hold on; plot(x1, y1); plot(x2, y2); xlabel('x'); ylabel('y'); xlim([-(radius+2), (radius+2)]); ylim([-(radius+2), (radius+2)]);%% 創(chuàng)建神經(jīng)網(wǎng)絡(luò) hidden_layer_size = 8; net1 = feedforwardnet(hidden_layer_size); net2 = feedforwardnet(hidden_layer_size); [net1, tr] = train(net1, x1, y1); [net2, tr] = train(net2, x2, y2);%% 測(cè)試結(jié)果 test_x = linspace(-5, 5, 20); p1 = sim(net1, test_x); p2 = sim(net2, test_x); % figure(); % hold on; plot(test_x, p1, '*'); plot(test_x, p2, '*'); xlim([-(radius+2), (radius+2)]); ylim([-(radius+2), (radius+2)]);結(jié)果截圖:
從結(jié)果來(lái)看,訓(xùn)練好的網(wǎng)絡(luò)只在圓所在范圍內(nèi)預(yù)測(cè)有效,出了范圍就失效了。
實(shí)例3
例子很簡(jiǎn)單,使用tensorflow構(gòu)建了一個(gè)單隱層神經(jīng)網(wǎng)絡(luò),并進(jìn)行擬合。
首先導(dǎo)入模塊。
創(chuàng)建數(shù)據(jù),并引入噪聲。
x_data = np.linspace(-1,1,300)[:, np.newaxis] noise = np.random.normal(0, 0.05, x_data.shape) y_data = np.square(x_data) - 0.5 + noise繪制圖片看看。
plt.plot(x_data, y_data)創(chuàng)建TensorFlow的占位符,用于后面導(dǎo)入數(shù)據(jù)。
xs = tf.placeholder(tf.float32, [None, 1]) ys = tf.placeholder(tf.float32, [None, 1])創(chuàng)建一個(gè)全連接層(隱藏層),激活函數(shù)為relu。
w1 = tf.Variable(tf.random_normal([1, 10])) b1 = tf.Variable(tf.zeros([1, 10]) + 0.1) ip1 = tf.matmul(xs, w1) + b1 out1 = tf.nn.relu(ip1)輸出層,不接激活函數(shù)。
w2 = tf.Variable(tf.random_normal([10,1])) b2 = tf.Variable(tf.zeros([1, 1]) + 0.1) ip2 = tf.matmul(out1, w2) + b2 out2 = ip2loss為均方誤差,使用SGD訓(xùn)練網(wǎng)絡(luò)。
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-out2), reduction_indices=[1])) train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)初始化參數(shù),創(chuàng)建會(huì)話。
init = tf.global_variables_initializer() sess = tf.Session() sess.run(init)開(kāi)始訓(xùn)練。
for i in range(1000):_, loss_value = sess.run([train_step, loss], feed_dict={xs:x_data, ys:y_data})if i%50==0:print(loss_value) 0.914197 0.0143666 0.00786084 0.00659379 0.00575486 0.00504135 0.00450164 0.00415548 0.00389943 0.00368641 0.00353138 0.00337983 0.00325611 0.00315293 0.0030722 0.00300812 0.0029489 0.00290472 0.00286406 0.00282905預(yù)測(cè)結(jié)果看看。
pred = sess.run(out2, feed_dict={xs:x_data})可以看出擬合出了大致的曲線,但是受到噪聲干擾不是很標(biāo)準(zhǔn)。
plt.plot(x_data, pred)總結(jié)
以上是生活随笔為你收集整理的使用神经网络拟合曲线(MATLAB/Python)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Caffe官方教程翻译(9):Multi
- 下一篇: Caffe官方教程翻译(10):Edit