python - 更改 NN 中的单个权重

标签 python tensorflow keras

import tensorflow as tf
from tensorflow import keras
import numpy as np

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(2,)),
    keras.layers.Dense(20, activation=tf.nn.relu),
    keras.layers.Dense(20, activation=tf.nn.relu),
    keras.layers.Dense(1)
])


# an individual weight is like:
print(model.weights[4][0])
# returns tf.Tensor([0.3985532], shape=(1,), dtype=float32)

我正在做一个(有些愚蠢的)实验,想手动停用网络中的某些神经元。根据我的阅读,最好的方法是使用 mask 或调整重量。对于后者,我可以打印单个神经元的值,但现在我想“设置”它。问题是我不能说 tensor = 0.0 因为它也包含形状和类型。有什么想法吗?

最佳答案

您可以使用“assign”来更改值,如下所示:

import tensorflow as tf
from tensorflow import keras
import numpy as np

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(2,)),
    keras.layers.Dense(20, activation=tf.nn.relu),
    keras.layers.Dense(20, activation=tf.nn.relu),
    keras.layers.Dense(1)
])


# an individual weight is like:
print(model.weights[4][0])

weights=model.weights[4].numpy() # get the kernel weights of the layer as numpy array
weights[0]=0 #set tensor model.weights[4][0] to zero
model.weights[4].assign(weights)
print(model.weights[4][0]) # displays tf.Tensor([0.], shape=(1,), dtype=float32)

您还可以使用“assign”和“tensor_scatter_nd_update”,如下所示:

# an individual weight is like:
print(model.weights[4][0])

indices = [[0,0]] #indices to modify 
model.weights[4].assign(tf.tensor_scatter_nd_update(model.weights[4], indices, [0]))

print(model.weights[4][0]) # displays tf.Tensor([0.], shape=(1,), dtype=float32)

关于python - 更改 NN 中的单个权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62906562/

相关文章:

Python:如何使用 IDLE 调试器在 mac 上设置断点?

python - 在 Python 中计算 BLEU 分数

python TensorFlow 导入失败

python - TensorFlow 服务 : How to get prediction, ModelSpec 缺失

python - 如何将 X-Ray 与 SNS 一起使用以获得一个大链而不是许多小链?

python - Ansible Django 模块替代 Python 解释器

c++ - 在 Ubuntu 17.10 中从 C/C++ 源构建 Tensorflow 1.4.0

python - 反射填充 Conv2D

c++ - 如何从 C++ 使用 Keras SavedModel

python - 具有非方形图像的 CNN 自动编码器