【问题标题】:Reassignment of weights in tensorflow 2/keras在 tensorflow 2/keras 中重新分配权重
【发布时间】:2020-11-24 07:25:08
【问题描述】:

我目前正在 Keras 中测试一些修改版本的 dropout,其中之一涉及在自定义密集层的训练期间调整权重。然而,我还没有能够在没有错误的情况下运行它。我怀疑这与急切执行有关,但我不确定。

class Linear(keras.layers.Layer):
def __init__(self, units, **kwargs):
    super(Linear, self).__init__(**kwargs)
    self.units = units

def build(self, input_shape):
    self.w = self.add_weight(
        shape=(input_shape[-1], self.units),
        initializer="random_normal",
        trainable=True,
    )
    self.b = self.add_weight(
        shape=(self.units,), initializer="random_normal", trainable=True
    )

def call(self, inputs, training=False):
    prob = 0.0/10
    if training:
        w = np.matrix(self.w)
        # w = self.w
        shape = w.shape
        size = shape[0] * shape[1]

        arr = np.random.choice([0,1], size=size, p=[prob, 1 - prob]) #random array of 1's and 0's
        arr = arr.reshape(shape) #reshape it to same dimensions as weights
        new_weights = np.multiply(arr, w) #element wise multiplication
        self.w = new_weights
    return tf.matmul(inputs, self.w) + self.b
model = models.Sequential()

model.add(layers.Conv2D(32, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D())

model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D())

model.add(layers.Conv2D(128, (3, 3), activation='relu',padding='same'))
model.add(layers.MaxPooling2D())

model.add(layers.Conv2D(4, (3, 3), activation='relu',padding='same'))
model.add(layers.MaxPooling2D())

model.add(layers.Flatten())
model.add(Linear(3)) #Custom layer
model.add(layers.Dense(10, activation='softmax'))

model.compile(loss = 'CategoricalCrossentropy',
              optimizer = 'adam',
              metrics=['accuracy'])

epochs = 1
history = model.fit(train_dataset, validation_data=validation_dataset, epochs=epochs)

错误:TypeError:预期的二进制或 unicode 字符串,得到

【问题讨论】:

    标签: python tensorflow keras neural-network tensorflow2.0


    【解决方案1】:

    self.w 必须是 tensorflow.Variable。然而,在call() 中相乘后,它变为tensorflow.Tensor。只需在call() 中找到另一种方法来做同样的事情 试试这个代码:

        def call(self, inputs, training=False):
            prob = 0.0/10
            if training:
                w = np.matrix(self.w)
                shape = w.shape
                size = shape[0] * shape[1]
    
                arr = np.random.choice([0,1], size=size, p=[prob, 1 - prob]) #random array of 1's and 0's
                arr = arr.reshape(shape) #reshape it to same dimensions as weights
    # CHANGED 3 LINES BELOW:
                arr = tf.convert_to_tensor(arr, dtype=tf.float32)
                new_weights = tf.multiply(arr, self.w)
                self.w.assign(new_weights)  # Assign preserves tf.Variable
    
            return tf.matmul(inputs, self.w) + self.b
    

    【讨论】:

    • 有时这会给我一个“NotImplementedError:numpy() 仅在启用急切执行时可用。”错误。知道是什么原因造成的吗? (注意我正在运行 TF 2)
    • 如果你想在你的函数中使用 numpy - 坚持急切的执行。如果需要非急切 - 只需用纯 TF 重写函数的上部(在其中生成新权重)。
    猜你喜欢
    • 2021-08-16
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 2018-11-14
    • 2017-04-06
    相关资源
    最近更新 更多