from keras import backend as K
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
# first model without input_dim prints an empty list
model = Sequential()
model.add(Dense(5, weights=[np.ones((3,5)),np.zeros(5)], activation='relu'))
print(model.get_weights())
# second model with input_dim prints the assigned weights
model1 = Sequential()
model1.add(Dense(5, weights=[np.ones((3,5)),np.zeros(5)],input_dim=3, activation='relu'))
model1.add(Dense(1, activation='sigmoid'))
print(model1.get_weights())
您需要指定输入到第一层的维度,否则它将给您一个空列表。比较两个打印结果的唯一区别在于输入形状的初始化。
@Chris_K给出的答案应该有效-
model.get_weights()
在调用fit之前打印正确的初始化权重。尝试运行此代码作为健全性检查-它应该打印两个非零的矩阵(对于两个层),然后打印两个零的矩阵:下面是我看到的结果:
只需在模型上使用
get_weights()
。例如:这将打印2x5权重矩阵和1x5偏差矩阵:
偏差为零,因为默认的偏差初始值设定项为零。
相关问题 更多 >
编程相关推荐