###function approximation f(x)=sin(x) ###2018.08.14 ###激活函数用的是sigmoid import numpy as np import math import matplotlib.pyplot as plt x = np.linspace(-3, 3, 600) # print(x) # print(x[1]) x_size = x.size y = np.zeros((x_size, 1)) # print(y.size) for i in range(x_size): y[i] = math.sin(2*math.pi*0.4*x[i])+ math.sin(2*math.pi*0.1*x[i]) + math.sin(2*math.pi*0.9*x[i]) # print(y) hidesize = 10 W1 = np.random.random((hidesize, 1)) # 输入层与隐层之间的权重 B1 = np.random.random((hidesize, 1)) # 隐含层神经元的阈值 W2 = np.random.random((1, hidesize)) # 隐含层与输出层之间的权重 B2 = np.random.random((1, 1)) # 输出层神经元的阈值 threshold = 0.005 max_steps = 1001 def sigmoid(x_): y_ = 1 / (1 + math.exp(-x_)) return y_ E = np.zeros((max_steps, 1)) # 误差随迭代次数的变化 Y = np.zeros((x_size, 1)) # 模型的输出结果 for k in range(max_steps): temp = 0 for i in range(x_size): hide_in = np.dot(x[i], W1) - B1 # 隐含层输入数据 # print(x[i]) hide_out = np.zeros((hidesize, 1)) # 隐含层的输出数据 for j in range(hidesize): # print("第{}个的值是{}".format(j,hide_in[j])) # print(j,sigmoid(j)) hide_out[j] = sigmoid(hide_in[j]) # print("第{}个的值是{}".format(j, hide_out[j])) # print(hide_out[3]) y_out = np.dot(W2, hide_out) - B2 # 模型输出 # print(y_out) Y[i] = y_out # print(i,Y[i]) e = y_out - y[i] # 模型输出减去实际结果。得出误差 ##反馈,修改参数 dB2 = -1 * threshold * e dW2 = e * threshold * np.transpose(hide_out) dB1 = np.zeros((hidesize, 1)) for j in range(hidesize): dB1[j] = np.dot(np.dot(W2[0][j], sigmoid(hide_in[j])), (1 - sigmoid(hide_in[j])) * (-1) * e * threshold) dW1 = np.zeros((hidesize, 1)) for j in range(hidesize): dW1[j] = np.dot(np.dot(W2[0][j], sigmoid(hide_in[j])), (1 - sigmoid(hide_in[j])) * x[i] * e * threshold) W1 = W1 - dW1 B1 = B1 - dB1 W2 = W2 - dW2 B2 = B2 - dB2 temp = temp + abs(e) E[k] = temp if k % 100 == 0: print(k) plt.figure() plt.plot(x, Y) plt.plot(x, Y, color='red', linestyle='--') plt.show()这个程序如何每迭代100次就输出一次图片
时间: 2024-04-08 13:31:00 浏览: 129
function-approximation.zip_bp 函数逼近
要每迭代100次输出一次图片,你需要在代码中添加相应的逻辑。以下是修改后的代码示例:
```python
import numpy as np
import math
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 600)
x_size = x.size
y = np.zeros((x_size, 1))
for i in range(x_size):
y[i] = math.sin(2*math.pi*0.4*x[i]) + math.sin(2*math.pi*0.1*x[i]) + math.sin(2*math.pi*0.9*x[i])
hidesize = 10
W1 = np.random.random((hidesize, 1))
B1 = np.random.random((hidesize, 1))
W2 = np.random.random((1, hidesize))
B2 = np.random.random((1, 1))
threshold = 0.005
max_steps = 1001
def sigmoid(x_):
y_ = 1 / (1 + math.exp(-x_))
return y_
E = np.zeros((max_steps, 1))
Y = np.zeros((x_size, 1))
for k in range(max_steps):
temp = 0
for i in range(x_size):
hide_in = np.dot(x[i], W1) - B1
hide_out = np.zeros((hidesize, 1))
for j in range(hidesize):
hide_out[j] = sigmoid(hide_in[j])
y_out = np.dot(W2, hide_out) - B2
Y[i] = y_out
e = y_out - y[i]
dB2 = -1 * threshold * e
dW2 = e * threshold * np.transpose(hide_out)
dB1 = np.zeros((hidesize, 1))
for j in range(hidesize):
dB1[j] = np.dot(np.dot(W2[0][j], sigmoid(hide_in[j])), (1 - sigmoid(hide_in[j])) * (-1) * e * threshold)
dW1 = np.zeros((hidesize, 1))
for j in range(hidesize):
dW1[j] = np.dot(np.dot(W2[0][j], sigmoid(hide_in[j])), (1 - sigmoid(hide_in[j])) * x[i] * e * threshold)
W1 = W1 - dW1
B1 = B1 - dB1
W2 = W2 - dW2
B2 = B2 - dB2
temp = temp + abs(e)
E[k] = temp
if k % 100 == 0:
plt.figure()
plt.plot(x, Y)
plt.plot(x, Y, color='red', linestyle='--')
plt.savefig(f'iteration_{k}.png')
plt.close()
plt.figure()
plt.plot(x, Y)
plt.plot(x, Y, color='red', linestyle='--')
plt.show()
```
在上述示例中,我添加了一个条件判断语句`if k % 100 == 0`来判断是否达到每100次迭代的条件。如果满足条件,则在该迭代结束后绘制并保存图片,文件名为`iteration_k.png`,其中`k`表示当前迭代次数。
请根据你的具体需求和编程环境进行相应的调整。
阅读全文