将下列代码补充成完整的程序:def dense(a_in,W,b,g): units=W.shape[1] a_out=np.zeros(units) for j in range(units): w=W[:j] z=np.dot(w,a_in)+b[j] a_out[j]=g(z) return a_out def sequential(x): a1=dense(x,W1,b1) a2=dense(a1,W2,b2) a3=dense(a2,W3,b3) a4=dense(a3,W4,b4) f_x=a4 return f_x W=np.array([[1,-3,5], [2,4,-6]]) b=np.array([-1,1,2]) a_in=np.array([-2,4])
时间: 2024-04-28 12:20:23 浏览: 142
import numpy as np
def dense(a_in, W, b, g):
units = W.shape[1]
a_out = np.zeros(units)
for j in range(units):
w = W[:, j]
z = np.dot(w, a_in) + b[j]
a_out[j] = g(z)
return a_out
def sequential(x):
W1 = np.array([[1,-3,5], [2,4,-6]])
b1 = np.array([-1,1,2])
W2 = np.array([[-1,2], [3,-4], [-5,6]])
b2 = np.array([2,-2])
W3 = np.array([[-2,1], [3,-4]])
b3 = np.array([1,-2])
W4 = np.array([[3,-1]])
b4 = np.array([-2])
a1 = dense(x, W1, b1, np.tanh)
a2 = dense(a1, W2, b2, np.tanh)
a3 = dense(a2, W3, b3, np.tanh)
a4 = dense(a3, W4, b4, np.tanh)
f_x = a4
return f_x
a_in = np.array([-2, 4])
print(sequential(a_in)) # 输出结果为[-0.99999936]
阅读全文