python-3.x - 编辑图层中的各个权重和偏差

标签 python-3.x tensorflow keras

我正在开展一个项目,需要编辑个人权重和偏差。

是否有任何方法可以实际访问图层权重和偏差,以便我可以手动编辑它们? 来自tf.layers.dense()

到目前为止,我已经创建了自己的模型并将偏差存储在外部,如下所示:

for _ in range(population_size):
    hidden_layer.append(tf.Variable(tf.truncated_normal([11, 20])))
    output_layer.append(tf.Variable(tf.truncated_normal([20, 9])))
population.append([hidden_layer, output_layer])

然后我尝试使用 feed dict 将人口输入到模型中。事实证明这是一个真正的 hell ,因为我无法将它们输入模型,因为变量的形状不一样。

是否有任何 native 支持从致密层获取权重?

最佳答案

From the keras doc:

All Keras layers have a number of methods in common:

layer.get_weights(): returns the weights of the layer as a list of Numpy arrays.
layer.set_weights(weights): sets the weights of the layer from a list of Numpy arrays (with the same shapes as the output of get_weights).

您可以使用 yourmodel.layers 轻松访问模型内​​的所有层。

关于python-3.x - 编辑图层中的各个权重和偏差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51380363/

相关文章:

python - 不一致的理解语法?

python - 如何计算损失梯度 w.r.t 以模拟 Keras 模型中的输入?

python - Keras 中的 Rank 是多少?

python - 按钮的像素宽度和高度?

python-3.x - 有没有办法使用 playwright 连接到我现有的浏览器 session

python - 加载 TensorFlow 嵌入模型

python - LSTM 单词预测模型仅预测最常见的单词,或者对于不平衡数据使用哪种损失

python - 我们可以在训练和测试数据中建立具有不同输入向量大小的模型吗?

python - Pandas 从字符串中提取数字

tensorflow - 如何正确使用tf.metrics.accuracy?