这是我尝试过的:
def loss(W):
weightsList1 = [np.zeros((25,20)), np.zeros(20)]
for i in range(20):
for j in range(25):
weightsList1[0][j,i] = W[i][j]
weightsList1[1][i] = W[i][25]
model.layers[0].set_weights(weightsList1)
weightsList2 = [np.zeros((20,1)), np.zeros(1)]
for i in range(20):
weightsList2[0][i,0] = W[0][i]
weightsList2[1][0] = W[0][20]
model.layers[1].set_weights(weightsList2)
preds = model.predict(X)
mse = np.sum(np.square(np.subtract(preds,Y)))/len(X[:,0])
return mse
V = np.random.randn(20,26)*0.01
res = minimize(loss, x0=V, method = 'BFGS', options={'eps': 1e-6,'disp': True})
但是出现以下错误:
IndexError:标量变量的索引无效。