我已经提取了隐藏的Tensorflow全连接层(3个隐藏的ReLU,1个输出的NoActivation)的权重和偏差,如下所示:
trainable_variables = tf.trainable_variables()
kernels_list = [trainable_variable for trainable_variable in trainable_variables if "DQN" in trainable_variable.name and "kernel" in trainable_variable.name]
biases_list = [trainable_variable for trainable_variable in trainable_variables if "DQN" in trainable_variable.name and "bias" in trainable_variable.name]
sess.run(tf.global_variables_initializer())
kval_list = [sess.run(kernels_list[i]) for i in range(len(kernels_list))]
bval_list = [sess.run(biases_list[i]) for i in range(len(biases_list))]
鉴于该模型已经过全面训练,我想使用这些权重和偏差来运行“ numpy预测”。我有输入数组,该数组转到第一个隐藏层,然后numpy使用这些权重和偏差来查找第一个隐藏层的输出,等等。这是我用来尝试运行这些计算的代码:
x = state # state is my 1 dimensional input array
for i in range(0, len(kernels_list)):
weights = kval_list[i]
biases = bval_list[i]
w = x.dot(weights)
w += biases
out = np.maximum(w, 0) # ReLU
if i == len(kernels_list) - 1: # If output layer, no ReLU
out = w
x = out # Input to the next layer
但是,当我使用Tensorflow预测将最终x
值(输出层)与输出层进行比较时,每个输出节点得到的值却不同……我的矩阵计算做错了吗?输入层有1140个节点,因此最初是x.shape = (1140,)
,而第一个隐藏层有600个节点,因此在第一次迭代中是weights.shape = (1140, 600)
和biases.shape = (600,)
。