當前位置:
首頁 >
使用神经网络拟合曲线(MATLAB/Python)
發布時間:2025/3/21
55
豆豆
生活随笔
收集整理的這篇文章主要介紹了
使用神经网络拟合曲线(MATLAB/Python)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
神經網絡通常用于分類任務,也可以用于回歸任務。使用一個含有隱層的神經網絡可以很輕松地擬合出非線性曲線。下面是幾個示例,包含matlab的和python的,都很簡單。
實例1
首先,生成正弦曲線,并引入隨機噪聲。隨后,在matlab中使用feedforwardnet函數創建BP神經網絡,訓練網絡,并查看最后的擬合結果。
%% clc; clear all; close all;%% 生成正弦曲線 x = linspace(-2*pi, 2*pi, 100); y = sin(x); % 對目標值加入噪聲 n = 0.1 * rand(1, length(x)); y = y + n;% figure(); % plot(x, y, 'b-');%% 數據歸一化,創建測試數據集 [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-');%% 創建神經網絡(也可打開nntool,在matlab中使用gui創建神經網絡) hidden_layer_size = 10; net = feedforwardnet(hidden_layer_size); [net, tr] = train(net, data_input, data_target);%% 擬合結果 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');運行結果截圖:
x軸是測試數據的x坐標,y軸是x坐標對應預測值和曲線真實值的誤差。
曲線和擬合結果:
實例2
這里還是用matlab進行的實驗。擬合的目標是一個圓,將圓拆成上下兩條曲線,分別進行擬合。
%% 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)]);%% 創建神經網絡 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);%% 測試結果 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)]);結果截圖:
從結果來看,訓練好的網絡只在圓所在范圍內預測有效,出了范圍就失效了。
實例3
例子很簡單,使用tensorflow構建了一個單隱層神經網絡,并進行擬合。
首先導入模塊。
創建數據,并引入噪聲。
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)創建TensorFlow的占位符,用于后面導入數據。
xs = tf.placeholder(tf.float32, [None, 1]) ys = tf.placeholder(tf.float32, [None, 1])創建一個全連接層(隱藏層),激活函數為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)輸出層,不接激活函數。
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訓練網絡。
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-out2), reduction_indices=[1])) train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)初始化參數,創建會話。
init = tf.global_variables_initializer() sess = tf.Session() sess.run(init)開始訓練。
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預測結果看看。
pred = sess.run(out2, feed_dict={xs:x_data})可以看出擬合出了大致的曲線,但是受到噪聲干擾不是很標準。
plt.plot(x_data, pred)總結
以上是生活随笔為你收集整理的使用神经网络拟合曲线(MATLAB/Python)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Caffe官方教程翻译(9):Multi
- 下一篇: 基于Python编写的简易翻译器